Nmlab Assignment 5

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

Question 1(i)

#include<stdio.h>
#include<math.h>
#define f1(x,y,z) (2*z-y-11)/8
#define f2(x,y,z) (22-2*x-z)/9
#define f3(x,y,z) (x+2*y-15)/11

int main()
{
float x0=0, y0=0, z0=0, x1, y1, z1, e1, e2, e3, e;
int count=1;
e=0.0001;

printf("\nCount\tx\ty\tz\n");
do
{
x1 = f1(x0,y0,z0);
y1 = f2(x1,y0,z0);
z1 = f3(x1,y1,z0);
printf("%d\t%0.4f\t%0.4f\t%0.4f\n",count, x1,y1,z1);
e1 = fabs(x0-x1);
e2 = fabs(y0-y1);
e3 = fabs(z0-z1);
count++;
x0 = x1;
y0 = y1;
z0 = z1;
}while(e1>e && e2>e && e3>e);
printf("\nSolution: x=%0.3f, y=%0.3f and z = %0.3f\n",x1,y1,z1);
return 0;
}
Question 1(ii)
#include<stdio.h>
#include<math.h>
#define f1(x,y,z) (10-y-z)/2
#define f2(x,y,z) (18-3*x-3*z)/2
#define f3(x,y,z) (16-x-4*y)/9

int main()
{
float x0=0, y0=0, z0=0, x1, y1, z1, e1, e2, e3, e;
int count=1;
e=0.0001;

printf("\nCount\tx\ty\tz\n");
int i,k=0;
for(i=1; i<=100; i++)
{
x1 = f1(x0,y0,z0);
y1 = f2(x1,y0,z0);
z1 = f3(x1,y1,z0);
printf("%d\t%0.4f\t%0.4f\t%0.4f\n",count, x1,y1,z1);
e1 = fabs(x0-x1);
e2 = fabs(y0-y1);
e3 = fabs(z0-z1);
count++;
x0 = x1;
y0 = y1;
z0 = z1;
if(!(e1>e && e2>e && e3>e))
{
break;
k=1;
}
}
if(k==0)
{
printf("Roots not converging!");
exit(0);
}
printf("\nSolution: x=%0.3f, y=%0.3f and z = %0.3f\n",x1,y1,z1);
return 0;
}
Output:

Question 1(iii)
#include<stdio.h>
#include<math.h>
#define f1(x,y,z) (8-y-2*z)/3
#define f2(x,y,z) (9-2*x-z)/3
#define f3(x,y,z) (6-x-2*y)/3

int main()
{
float x0=0, y0=0, z0=0, x1, y1, z1, e1, e2, e3, e;
int count=1;
e=0.0001;

printf("\nCount\tx\ty\tz\n");
do
{
x1 = f1(x0,y0,z0);
y1 = f2(x1,y0,z0);
z1 = f3(x1,y1,z0);
printf("%d\t%0.4f\t%0.4f\t%0.4f\n",count, x1,y1,z1);
e1 = fabs(x0-x1);
e2 = fabs(y0-y1);
e3 = fabs(z0-z1);
count++;
x0 = x1;
y0 = y1;
z0 = z1;
}while(e1>e && e2>e && e3>e);
printf("\nSolution: x=%0.3f, y=%0.3f and z = %0.3f\n",x1,y1,z1);
return 0;
}

You might also like