I came to the following main.
Problem, it runs but it shows error even though its not positive definite
Code:
void nrerror(char error_text[])
/* Numerical Recipes standard error handler */
{
fprintf(stderr,"Numerical Recipes run-time error...\n");
fprintf(stderr,"%s\n",error_text);
fprintf(stderr,"...now exiting to system...\n");
exit(1);
}
float choldc(float **a, int n ,float p[])
{
void nrerror(char error_text[]);
int i,j,k;
float sum;
for (i=1;i<=n;i++) {
for (j=i;j<=n;j++) {
for (sum=a[i][j],k=i-1;k>=1;k--) sum -= a[i][k]*a[j][k];
if (i == j) {
if (sum <= 0.0)
nrerror("choldc failed");
p[i]=sqrt(sum);
} else a[j][i]=sum/p[i];
}
}
return 1;
}
float cholsl(float **a, int n, float p[], float b[], float x[])
{
int i,k;
float sum;
for (i=1;i<=n;i++) {
for (sum=b[i],k=i-1;k>=1;k--) sum -= a[i][k]*x[k];
x[i]=sum/p[i];
}
for (i=n;i>=1;i--) {
for (sum=x[i],k=i+1;k<=n;k++) sum -= a[k][i]*x[k];
x[i]=sum/p[i];
}
return 1;
}
int main() {
int n = 4;
float L, T;
float p[n], x[n];
int zeile =4;
int spalte =4;
float a[zeile][spalte];
float b[n] = {4, -1,0,0};
a[1][1]=1; a[1][2]= -0.2; a[1][3]=0; a[1][4]=0;
a[2][1]= -0.2; a[2][2]=2; a[2][3]= -0.3; a[2][4]=0;
a[3][1]=0; a[3][2]= -0.3; a[3][3]=3; a[3][4]= -0.4;
a[4][1]=0; a[4][2]=0; a[4][3]= -0.4; a[4][4]=4;
L = choldc(a, n, p);
printf(" Untere Dreiecksmatrix L %\n ", L);
T = cholsl( a, n, p, b, x)
printf(" Lösungsvektor T%\n ", T)
return 0;
}