0
$\begingroup$

I struggle to implement functions for my main programm.

I need to solve an linear Equation (Ax=b) with Cholesky decomposition i have choldc (composition), cholsl (back and forward substitution)

void choldc(float **a, float p[], int n)
{
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];
    }
}

and want to use it and print the lower L matrix of the cholesky decomposition. Then apply cholsl to solve it and print solution for a 4x4 matrix example

int main() {

int n=4;
float L,a,p;
static float A[n+1][n+1]=
    {3,-2,0,0
    -2,3,6,0
    0,6,3,-2
    0,0,-2,3
    };

static float b [n+1]= {4,-1,0,0}; 

\\ there is missing the choldc for A\\

L = choldc(&a, n, p[]);
printf(" lower matrix L %\n ", L);

return 0;

I dont know how to use coldc on this Matrix A and then it tells me the Line with L= choldc... cause error at p[] when i want to print it. What's wrong here?

$\endgroup$
  • $\begingroup$ The subroutine wants to address elements with indices 1,...,4 but your main program uses 0,...,3 for the array b, and something too horrible to describe in the array a. $\endgroup$ – kimchi lover Nov 26 '17 at 20:30
  • 1
    $\begingroup$ I guess it would be worth first to read something about arrays in C. $\endgroup$ – Algebraic Pavel Nov 27 '17 at 9:32
  • $\begingroup$ well im just starting with c and try to work with functions. do i need to give the array a maximum like #define MAX 100 ... function(feld, MAX) $\endgroup$ – Joseph S. Nov 27 '17 at 10:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.