Sep 10, 2009

jacobi mehod

#include
#include
#include
#include
void main()
{
float a[20][20],b,c,x[20],y[20];
int i,j,k,no,flag;
clrscr();
cout<<"how many eq. u want to enter?\n";
cin>>no;
for(i=1;i<=no;i++)
{
for(j=1;j<=no+1;j++)
{
printf("enter val. of a[%d][%d]",i,j);
cin>>a[i][j];
}
}
cout<<"matrix is\n";
for(i=1;i<=no;i++)
{
for(j=1;j<=no+1;j++)
{
cout<
cout<<"\t";
}
cout<<"\n";
}
for(k=1;k<=no;k++)
y[k]=0.0;
 for( k=1;k<=no;k++)
x[k]=0.0;
for(;;)
{
for(i=1;i<=no;i++)//i start
{
float temp=0.0;
for(j=1;j<=no;j++)
 {
if(i!=j)
{
temp+=a[i][j]*y[j];
x[i]=(a[i][no+1]-temp)/a[i][i];
y[i]=x[i];
}//if ends
 } //j ends
}   //i ends
int n=1;
for(int m=1;m<=no;m++)
{
if(x[m]-y[m]<=0.001)
n++;
}
if(n==no)
break;
}
for(j=1;j<=no;j++)
{
printf("\nx%d is\n",j);
cout<
}
cout<<"\n";
getch();
}

No comments:

Post a Comment