Write a MATHCAD program to solve systems of linear equations using the iterative
ID: 2971217 • Letter: W
Question
Write a MATHCAD program to solve systems of linear equations using the iterative Jacobi Method. The method is explained in the file named "Jacobi Method", pay attention to the convergence condition. The program should be able to solve systems with n equations and n unknowns (nxn), given that coefficient matrix is diagonally dominant. Check your program with the following systems of equations ( picture attached).
System of Equations to check with..
My teacher's notes on "iterative solutions".
I posted them just in case they were helpful.
Explanation / Answer
#include<stdio.h>
#include<conio.h>
#include<math.h>
#include<stdlib.h>
void main()
{
float a[20][20],x[20],e,big,temp,relerror,sum;
int n,i,j,maxit,itr;
char ch;
clrscr();
printf(" ENTER THE SIZE OF THE EQUATION :: ");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
printf(" Enter the coefficints of equation %d and RHS ",i);
for(j=1;j<=n+1;j++)
scanf("%f",&a[i][j]);
}
printf(" Enter relative error and number of iteration :: ");
scanf("%f%d",&e,&maxit);
for(i=1;i<=n;i++)
x[i]=0;
for(itr=1;itr<=maxit;itr++)
{
big=0;
for(i=1;i<=n;i++)
{
sum=0;
for(j=1;j<=n;j++)
{
if(i!=j
)
sum=sum+a[i][j]*x[j];
}
temp=(a[i][n+1]-sum)/a[i][i];
relerror=fabs((x[i]-temp)/temp);
if(relerror>big)
big=relerror;
x[i]=temp;
}
if(big<=e)
{
printf("Converges to a solution in %d iterations ",itr);
for(i=1;i<=n;i++)
printf(" %.4f ",x[i]);
getch();
exit(1);
}
}
printf("does not converge in %d iteration ",maxit);
getch();
}
this program have an option to choose no of iterations , and relative error
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.