Docsity

Gauss Elimination Technique: Solving Linear Systems using C - Prof. Petru Andrei, Study notes of Electrical and Electronics Engineering

Toggle sidebar
bg1
This file can be downloaded from http://www.eng.fsu.edu/~pandrei/Courses/2008/EEE4450
#include <stdlib.h>
#include <math.h>
#include <stdio.h>
//This is a sample program that solves linear system A * X = rhs by using Gauss
elimination technique
//The main() function is at the end of the file. The function that solves the linear
system of equations if Solve(...)
//This function is used by Solve. Do not modify or remove this function.
void lubksb(double **a, int n, int *indx, double b[])
{
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>=0)
for (j=ii;j<=i-1;j++) sum -= a[i][j]*b[j];
else if (sum) ii=i-1;
b[i]=sum;
}
for (i=n-1;i>=0;i--) {
sum=b[i];
for (j=i+1;j<n;j++) sum -= a[i][j]*b[j];
b[i]=sum/a[i][i];
}
}
//This function is used by Solve. Do not modify or remove this function.
void ludcmp(double **a, int n, int *indx, double *d)
{
int i,imax,j,k;
double big,dum,sum,temp;
double *vv;
vv=new double[n];
*d=1.0;
for (i=0;i<n;i++) {
big=0.0;
for (j=0;j<n;j++)
if ((temp=fabs(a[i][j])) > big) big=temp;
if (big == 0.0) printf("Singular matrix in routine ludcmp");
vv[i]=1.0/big;
}
for (j=0;j<n;j++) {
for (i=0;i<j;i++) {
sum=a[i][j];
for (k=0;k<i;k++) sum -= a[i][k]*a[k][j];
a[i][j]=sum;
}
big=0.0;
1
pf3

Partial preview of the text

Download Gauss Elimination Technique: Solving Linear Systems using C - Prof. Petru Andrei and more Study notes Electrical and Electronics Engineering in PDF only on Docsity!

#include <stdlib.h> #include <math.h> #include <stdio.h> //This is a sample program that solves linear system A * X = rhs by using Gauss elimination technique //The main() function is at the end of the file. The function that solves the linear system of equations if Solve(...) //This function is used by Solve. Do not modify or remove this function. void lubksb(double *a, int n, int indx, double b[]) { 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>=0) for (j=ii;j<=i-1;j++) sum -= a[i][j]b[j]; else if (sum) ii=i-1; b[i]=sum; } for (i=n-1;i>=0;i--) { sum=b[i]; for (j=i+1;j<n;j++) sum -= a[i][j]b[j]; b[i]=sum/a[i][i]; } } //This function is used by Solve. Do not modify or remove this function. void ludcmp(double **a, int n, int *indx, double *d) { int i,imax,j,k; double big,dum,sum,temp; double *vv; vv=new double[n]; d=1.0; for (i=0;i<n;i++) { big=0.0; for (j=0;j<n;j++) if ((temp=fabs(a[i][j])) > big) big=temp; if (big == 0.0) printf("Singular matrix in routine ludcmp"); vv[i]=1.0/big; } for (j=0;j<n;j++) { for (i=0;i<j;i++) { sum=a[i][j]; for (k=0;k<i;k++) 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 -= a[i][k]a[k][j]; a[i][j]=sum; if ( (dum=vv[i]fabs(sum)) >= 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.0) a[j][j]=1e-12; if (j != n-1) { dum=1.0/(a[j][j]); for (i=j+1;i<n;i++) a[i][j] = dum; } } delete[]vv; } //This is the function that you should call to solve linear system AX = rhs by using Gauss elimination technique // A - input matrix // rhs - input right-hand-side // X is the output vector // n is the dimension of the matrix which should be equal to the dimension of the rhs void Solve(double **A, double *rhs, double *X, int n) { int *_indx; int i, j; double d; _indx = new int[n]; ludcmp(A, n, _indx, &d); lubksb(A, n, _indx, rhs); for (i=0;i<n;i++) X[i] = rhs[i]; delete _indx; }