0% found this document useful (0 votes)
14 views3 pages

Lab 7

The document discusses two methods for solving systems of linear equations: Gauss elimination and Gauss-Jordan elimination. It provides code samples and example outputs for each method applied to sample systems of equations.

Uploaded by

Pawan Subedi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views3 pages

Lab 7

The document discusses two methods for solving systems of linear equations: Gauss elimination and Gauss-Jordan elimination. It provides code samples and example outputs for each method applied to sample systems of equations.

Uploaded by

Pawan Subedi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 3

Problem:

1. 10x1+x2+x3=12 2. x1+x2+4x4=0
2x1+10x2+x3=13 2x1-x2+5x3=2
x1+x2+5x3=7 5x1+2x2+x3+2x4=5
-3x1+2x3+6x4=-2
1. Gauss Elimination method
#include<stdio.h> //header files
#include<conio.h>
#include<math.h>
void main() //Main program start
{
float a[10][10],b[10],x[10],sum,u;
int i,j,k,n;
clrscr();
printf("Enter the size of system:");
scanf("%d",&n);
printf("Enter the coefficient of matrix:\n");
for(i=1;i<=n;i++) //Reads matrix
{for(j=1;j<=n+1;j++)
{ printf("a%d%d=",i,j);
scanf("%f",&a[i][j]);
}
}
for(k=1;k<=n-1;k++) //Making Upper triangular Matrix
{for(i=k+1;i<=n;i++)
{
u=a[i][k]/a[k][k];
for(j=k;j<=n+1;j++)
a[i][j]=a[i][j]-u*a[k][j];
}
}
x[n]=a[n][n+1]/a[n][n]; //For back Substitution
for(i=n-1;i>0;i--)
{
sum=0;
for(j=i+1;j<=n;j++)
{
sum=sum+a[i][j]*x[j];
}
x[i]=(a[i][n+1]-sum)/a[i][i];
}
printf("The values are:\n");
for(i=1;i<=n;i++)
printf(" x%d=%f\n",i,x[i]);
getch(); //End of main program
}
Outputs:
1. Enter the size of system:3 2. Enter the size of system:4
Enter the coefficient of matrix: Enter the coefficient of matrix:
a11=10 a11=1
a12=1 a12=1
a13=1 a13=0
a14=12 a14=4
a21=2 a15=0
a22=10 a21=2
a23=1 a22=-1
a24=13 a23=5
a31=1 a24=0
a32=1 a25=2
a33=5 a31=5
a34=7 a32=2
The values are: a33=1
x1=1.000000 a34=2
x2=1.000000 a35=5
x3=1.000000 a41=-3
a42=0
a43=2
a44=6
a45=-2
The values are:
x1= -0.066667
x2= 2.866667
x3= 1.000000
2. Gauss Jordan method x4= -0.700000
#include<stdio.h> //header files
#include<conio.h>
#include<math.h>
void main() // Main program start
{
float a[10][10],b[10],x[10],sum,u;
int i,j,k,n;
clrscr();
printf("Enter the size of system:");
scanf("%d",&n);
printf("Enter the coefficient of matrix:\n");
for(i=1;i<=n;i++) //Reads matrix
{for(j=1;j<=n+1;j++)
{ printf("a%d%d=",i,j);
scanf("%f",&a[i][j]);
}
}
for(k=1;k<=n-1;k++) //Making Upper triangular Matrix
{ for(i=k+1;i<=n;i++)
{ u=a[i][k]/a[k][k];
for(j=k;j<=n+1;j++)
a[i][j]=a[i][j]-u*a[k][j];
}
}
for(k=n;k>=2;k--) //Making Diagonal Matrix
{ for(i=k-1;i>=1;i--)
{ u=a[i][k]/a[k][k];

for(j=k;j<=n+1;j++)
a[i][j]=a[i][j]-u*a[k][j];
}
}
for(i=1;i<=n;i++) //Making Unity Matrix
{ x[i]=a[i][n+1]/a[i][i];
}
printf("The values are:\n");
for(i=1;i<=n;i++)
printf(" x%d=%f\n",i,x[i]);
getch(); //End of main program
}

Outputs:

1. Enter the size of system:3 2. Enter the size of system:4


Enter the coefficient of matrix: Enter the coefficient of matrix:
a11=10 a11=1
a12=1 a12=1
a13=1 a13=0
a14=12 a14=4
a21=2 a15=0
a22=10 a21=2
a23=1 a22=-1
a24=13 a23=5
a31=1 a24=0
a32=1 a25=2
a33=5 a31=5
a34=7 a32=2
The values are: a33=1
x1=1.000000 a34=2
x2=1.000000 a35=5
x3=1.000000 a41=-3
a42=0
a43=2
a44=6
a45=-2
The values are:
x1= -0.066667
x2= 2.866667
x3= 1.000000
x4= -0.700000

You might also like