void prob2(void)
{
/* In void prob2(voud) you are asked to solve the system of
linear equations:
a[i][0]*x[0]+a[i][1]*x[1]+...+a[i][4]*x[4] = b[i]
for i=0, 1, ..., 4 (2a)
using the Jacobi iterative method, where a[5][5] and b[5]
are defined in prob2().
In the method, one starts with an approximate x0[] and
obtains an improved solution x1[]:
x1[i] =1/a[i][i] *(b[i] -(sum on j, except j=i)a[i][j]*x0[j])
for i=0,...,4. (2b)
Perform 20 iterations to find an improved approximate solution
x1[] starting from the initial guess x0[0]=x0[1]=...=x0[4]=0.0.
After each iteration, print the values of x1[] by using %15.4e.
Finally, compute the residue of x1[]: residue[]=(b[]-a[][]*x1[])
and print residue using %12.4e.*/
double a[5][5]={ 5., 0., 1., 0., -1.,
0., 4., 3., -1., 0.,
0., 0., 7., 1., -5.,
1., -1., 0., 7., 0.,
1., 1., 0., 0., -8.};
double b[5]={8., -3., -4., 2.,-1.}, x0[5]={0.}, x1[5], sig;
int i, j, k;
/* Put prob2 statements here */
for(k=0; k<20; k++)
{
for(i=0; i<5; i++)
{
sig=0.;
for(j = 0; j <5; j++)
{
if(j!=i)
{
sig = a[i][j] * x0[j];
}
}
x1[i] = 1/a[i][i] *(b[i] - sig);
printf("x1[%d] = %15.4e\n", i, x1[i]);
}
for(j=0; j<5; j++){
x0[j]=x1[j];
}
printf("\n");
}
return;
}
void prob3(void)
{
using the Jacobi iterative method, where a[5][5] and b[5]
are defined in prob2().
In the method, one starts with an approximate x0[] and
obtains an improved solution x1[]:
x1[i] =1/a[i][i] *(b[i] -(sum on j, except j=i)a[i][j]*x0[j])
for i=0,...,4. (2b)
Perform 20 iterations to find an improved approximate solution
x1[] starting from the initial guess x0[0]=x0[1]=...=x0[4]=0.0.
After each iteration, print the values of x1[] by using %15.4e.
Finally, compute the residue of x1[]: residue[]=(b[]-a[][]*x1[])
and print residue using %12.4e.*/
double a[5][5]={ 5., 0., 1., 0., -1.,
0., 4., 3., -1., 0.,
0., 0., 7., 1., -5.,
1., -1., 0., 7., 0.,
1., 1., 0., 0., -8.};
double b[5]={8., -3., -4., 2.,-1.}, x0[5]={0.}, x1[5], sig;
int i, j, k;
/* Put prob2 statements here */
for(k=0; k<20; k++)
{
for(i=0; i<5; i++)
{
sig=0.;
for(j = 0; j <5; j++)
{
if(j!=i)
{
sig = a[i][j] * x0[j];
}
}
x1[i] = 1/a[i][i] *(b[i] - sig);
printf("x1[%d] = %15.4e\n", i, x1[i]);
}
for(j=0; j<5; j++){
x0[j]=x1[j];
}
printf("\n");
}
return;
}