Reputation: 13
I am trying to write a function that should have as result a matrix. The function is showed below:
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
};
However, every time I try to run it, I receive the Segmentation error message. I guess it is because I am allocating the matrices elements wrong. Can someone help me?
Running script.
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);
}
Upvotes: 1
Views: 49
Reputation: 144685
The code does not compile, but it would have undefined behavior anyway, since neither J
nor y
are ever allocated, storing values to y[k]
or J[i][j]
are major problems.
Upvotes: 1