Tuesday, September 30, 2008

Solution of linear equations.

First input the coefficient matrix and the constant matrix.
-> In this program array a is the coefficient matrix which is a 2 D matrix.
-> Constant matrix is array b which is a single collumn matrix.
-> Input the value of n from the user.
-> Take another array X which is also a single collumn matrix and is used to store the values of variables X,Y,Z and so on.


//This loop is for making upper triangular
for(k=0; k < n-1; k++)
{
for(p=k+1; p < n; p++)
{
if(a[k][k]==0 )
{
for(x=0; x < n;x++)
{
t=a[k][x];
a[k][x]=a[k+1][x];
a[k+1][x]=t;
}
continue;
}
}
i=0;
for(i=k+i; i < n-1;i++)
{
for(j=0; j < n-k;j++)
{
t[0]=a[k][k];
t[1]=a[i+1][k];
a[i+1][j+k]=(t[0]*a[i+1][j+k])-(t[1]*a[k][j+k]);
b[i+1][0]=(t[0]*b[i+1][0])-(t[1]*b[k][0]);
}
}
}

//Now assign value in the variables x[0][0],x[1][0],x[2][0]

for(i=n-1; i >= 0;i--)
{
num=b[i][0];
dem=a[i][i];
sum=0;
for(j=0; j < n;j++)
{
if(i!=j)
num-=a[i][j]*x[j][0];

else
continue;
}
sum=num/dem;
x[i][0]=sum;
}

Thus write another loop to print the values of variables.

1 comment:

Unknown said...

wah bhai rohan....maje aagaye....tumne to saari tension hi solve kardi .....maje aagaye..thanks bhai....