#include<stdio.h>
#include<math.h>
#include<stdlib.h>
#define PI 3.14159265359
#define TINY 0.000000001;
double LUBKSB(double *b, double **a, int N, int *indx)
{
int i, ii, ip, j;
double sum;
ii = 0;
for(i=0;i<=N;i++)
{
ip = indx[i];
sum = b[ip];
b[ip] = b[i];
if (ii)
{
for(j = ii;j<i;j++)
{
sum = sum - a[i][j] * b[j];
}
}
else if(sum)
{
ii = i;
}
b[i] = sum;
}
for(i=N;i>=0;i--)
{
sum = b[i];
for (j = (i+1); j<=N;j++)
{
sum = sum - a[i][j] * b[j];
}
b[i] = sum/a[i][i];
}
for (i=0;i<N;i++)
{
printf("b[%d]: %lf",i,b[i]);
}
}
double ludecmp(double **a, int N)
{
int i, imax, j, k;
double big, dum, sum, temp, d;
double *vv = (double *) malloc(N * sizeof(double));
int *indx = (int *) malloc(N * sizeof(double));
double *b = (double *) malloc(N * sizeof(double));
d = 1.0;
for (i=0;i<N;i++)
{
printf("Enter b[%d]",i);
scanf("%lf",&b[i]);
}
for(i=0;i<=N;i++)
{
big = 0.0;
for(j=0;j<=N;j++)
{
temp = abs(a[i][j]);
if (temp > big)
{
big = temp;
}
}
if (big == 0.0)
{
printf("Singular matrix\n");
exit;
}
vv[i] = 1.0/big;
}
for(j=0;j<=N;j++)
{
for(i=0;i<j;i++)
{
sum = a[i][j];
for(int k=0;k<i;k++)
{
sum = sum - a[i][k] * a[k][j];
}
a[i][j] = sum;
}
big = 0.0;
for(i=j;i<=N;i++)
{
sum = a[i][j];
for(k=0;k<j;k++)
{
sum = sum - a[i][k] * a[k][j];
}
a[i][j] =sum;
dum = vv[i] * abs(a[i][j]);
if(dum >= big)
{
big = dum;
imax = i;
}
}
if(j != imax)
{
for(k=0;k<=N;k++)
{
dum = a[imax][k];
a[imax][k] = a[j][k];
a[j][k] = dum;
}
d = -d;
vv[imax] = vv[j];
}
indx[j] = imax;
if (a[j][j] == 0)
{
a[j][j] = TINY;
}
if (j != N)
{
dum = 1.0/a[j][j];
for(i = (j+1); i<=N; i++)
{
a[i][j] = a[i][j] * dum;
}
}
}
LUBKSB(b,a,N,indx);
for (i=0;i<N;i++)
{
printf("b[%d]: %lf",i,b[i]);
}
}
int main()
{
int N, i, j;
printf("Enter the number of rows and columns: \n");
scanf("%d",&N);
double **a = (double **) malloc(N * sizeof(double*));
for (i = 0; i < N; i++)
a[i] = (double *) malloc(N * sizeof(double));
printf("Enter the values of matrix\n");
for(i = 0; i < N;i++)
{
for(j = 0;j < N; j++)
{
printf("Enter a[%d][%d]",i,j);
scanf("%lf",&a[i][j]);
}
}
ludecmp(a,N);
}
I am using these algorithms to find LU decomposition of matrix and trying to find solution Ax = b
Given a N ×N matrix A denoted as {a}N,Ni,j=1, the routine replaces it by the LU decomposition of a rowwise permutation of itself. “a” and “N” are input. “a” is also output, modified to apply the LU decomposition; {indxi}N i=1 is an output vector that records the row permutation effected by the partial pivoting; “d” is output and adopts ±1 depending on whether the number of row interchanges was even or odd. This routine is used in combination with algorithm 2 to solve linear equations or invert a matrix.
Solves the set of N linear equations A. x = b. Matrix {a} N,N i,j=1 is actually the LU decomposition of the original matrix A, obtained from algorithm 1. Vector {indxi} N i=1 is input as the permutation vector returned by algorithm 1. Vector {bi} N i=1 is input as the righthand side vector B but returns with the solution vector X. Inputs {a} N,N i,j=1, N, and {indxi} N i=1 are not modified in this algorithm.
There are a number of problems with your code:
i <= N
should be i < N
and i = N
should be i = N - 1
.fabs
, not abs
. exit
should be exit(1)
or exit(EXIT_FALILURE)
.return
statement. You should also free the memory you have allocated with the function free
. When you compile a C program you should also enable all warnings.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.