我正在尝试编写一个应该具有矩阵的函数。 功能如下所示:
double **jacobian(double *Xn, double *Clvi, double *c, double *Vinf,
int *Np, double *mu, double *dx) {
/*Calculating the Fn values*/
int position = *Np / 2;
int k;
double *y;
double **J;
double *Gama = malloc(position * sizeof(double));
double *Dalpha = malloc(position * sizeof(double));
memcpy(Gama, Xn, position * sizeof(Gama));
memcpy(Dalpha, Xn + position, position * sizeof(Gama));
for (k = 1; k <= (position - 2); k++) {
y[k] = Dalpha[k] - Clvi[k] / (2 * pi) + Gama[k] / (pi * *Vinf * *c) -
*mu * (Dalpha[k - 1] - 2 * Dalpha[k] + Dalpha[k + 1]); // n values listed as F1, F2,...Fn - rows
}
int i;
int j;
//Gamma values
for (i = 1; i <= (position - 2); i++) {
for (j = 1; j < position - 2; j++) {
if (i == j) {
J[i][j] = ((Dalpha[j] - Clvi[j] / (2 * pi) +
(Gama[j] + *dx) / (pi * *Vinf * *c) -
*mu * (Dalpha[j - 1] - 2 * Dalpha[j] + Dalpha[j + 1])) - y[i]) / (*dx);
} else {
J[i][j] = 0;
}
}
}
for (j = position; j < *Np; j++) {
if (abs(i - j) > 3) {
J[i][j] = 0;
} else {
J[i][j] = (((Dalpha[j] + *dx) -
Clvi[j] / (2 * pi) + Gama[j] / (pi * *Vinf * *c) -
*mu * (Dalpha[j - 1] - 2 * (Dalpha[j] + *dx) +
Dalpha[j + 1])) - y[i]) / (*dx);
}
}
}
return J
};
但是,每次我尝试运行它时,都会收到Segmentation错误消息。 我想这是因为我错误地分配了矩阵元素。 有人能帮助我吗?
运行脚本。
double main() {
double Xn[12] = {1.0,2.0,3.0,4.0,5.0,6.0,0.1,0.2,0.3,0.4,0.5,0.6};
double Clvi[6] = {0.2,0.3,0.35,0.4,0.5,0.6};
double alpha[6] = {1.0,2.0,3.0,4.0,5.0,6.0};
double c = 1.0;
double Vinf = 300.0;
double mu = 0.2;
int Np1 = 12;
double dx = 0.1;
**jacobian(Xn, Clvi, &c, &Vinf, &Np1, &mu, &dx);
}
答案 0 :(得分:1)
代码无法编译,但无论如何都会有未定义的行为,因为J
和y
都未分配,因此将值存储到y[k]
或J[i][j]
是主要的问题。