Tuesday, April 30, 2013

LAB REPORT FOR NUMERICAL METHODS 4


Gauss siedel iteration method

#include<iostream.h>
#include<conio.h>
#include<iomanip.h>
#include<math.h>
#define MAXIT 50
#define EPS 0.000001
void gaseid(int n, float a[10][10], float b[10], float x[10], int *count, int *status);
void main()
{ float a[10][10], b[10], x[10];
int i, j, n, count, status;
cout<<"** SOLUTION BY GUASS SEIDEL ITERATION METHOD **"<<endl;
cout<<"input the size of the system:"<<endl;
cin>>n;
cout<<"input coefficients, a(i.j)"<<endl;
cout<<"one row on each line"<<endl;
for(i=1; i<=n; i++)
for(j=1; j<=n; j++)
cin>>a[i][j];
cout<<"input vector b:"<<endl;
for(i=1; i<=n; i++)
cin>>b[i];
gaseid(n, a, b, x, &count, &status);
if(status==2)
{ cout<<"no CONVERGENCE in "<<MAXIT<<" iterations."<<endl<<endl<<endl;
}
else
{ cout<<"SOLUTION VECTOR X"<<endl;
for(i=1; i<=n; i++)
cout<<setw(15.6)<<x[i]<<endl;
cout<<"iterations= "<<count;
}
getch();
}
void gaseid(int n, float a[10][10], float b[10], float x[10], int *count, int *status)
{ int i, j, key;
float sum, x0[10];
for(i=1; i<=n; i++)
x0[i]=b[i]/a[i][i];
*count=1;
begin:
key=0;
for(i=1; i<=n; i++)
{ sum=b[i];
for(j=1; j<=n; j++)
{ if(i==j)
continue;
sum=sum-a[i][j]*x0[j];
}
x[i]=sum/a[i][i];
if(key==0)
{ if(fabs((x[i]-x0[i])/x[i])>EPS)
key=1;
}
}
if(key==1)
{ if(*count==MAXIT)
{ *status=2;
return;
}
else
{ *status=1;
for(i=1; i<=n; i++)
x0[i]=x[i];
}
*count=*count+1;
goto begin;
}
return;
}



Euler’s Method
#include<iostream.h>
#include<conio.h>
#include<math.h>
#include<iomanip.h>
float func(float x, float y);
void main()
{ clrscr();
int i, n;
float x, y, xp, h, dy;
float func(float, float);
cout<<endl;
cout<<"SOLUTION BY THE EULER's METHOD"<<endl;
cout<<"input the initial values of x and y:"<<endl;
cin>>x>>y;
cout<<"input the x at which y is required:"<<endl;
cin>>xp;
cout<<"input the step size, h:"<<endl;
cin>>h;
n = (int) ((xp-x)/h + 0.5);
for(i=1; i<=n; i++)
{ dy = h * func(x, y);
x = x + h;
y = y + dy;
cout<<setw(5)<<i<<setw(10.6)<<x<<setw(10.6)<<y<<endl;
}
cout<<"the value of y at x = "<<x<<" is "<<y<<endl;
getch();
clrscr();
}
float func(float x, float y)
{ float f;
f = x + y + x*y;
return (f);
}




Heun’s Method
#include<iostream.h>
#include<conio.h>
#include<math.h>
#include<iomanip.h>
float func(float x, float y);
void main()
{ clrscr();
int i, n;
float x, y, xp, h, m1, m2;
float func(float, float);
cout<<endl;
cout<<"SOLUTION BY THE HEUN's METHOD"<<endl;
cout<<endl;
cout<<"input initial values of x and y:"<<endl;
cin>>x>>y;
cout<<"input x at which y is required:"<<endl;
cin>>xp;
cout<<"input the step size, h:"<<endl;
cin>>h;
n = (int) ((xp - x)/h + 0.5);
for(i=1; i<=n; i++)
{ m1 = func(x, y);
m2 = func(x+h, y+m1*h);
x = x + h;
y = y + 0.5 * h * (m1 + m2);
cout<<setw(7)<<i<<setw(15.9)<<x<<setw(15.9)<<y<<endl;
}
cout<<"the value of y at x = "<<x<<" is "<<y;
getch();
clrscr();
}
float func(float x, float y)
{ float f;
f = -y/(2*y + 1);
return(f);
}


Runge-Kutta Method
#include<iostream.h>
#include<conio.h>
#include<math.h>
#include<iomanip.h>
float func(float x, float y);
void main()
{ clrscr();
int i, n;
float x, y, xp, h, m1, m2, m3, m4;
float func(float, float);
cout<<endl;
cout<<"******************************************"<<endl;
cout<<"SOLUTION BY 4th ORDER RUNGE- KUTTA METHOD:"<<endl;
cout<<"******************************************"<<endl;
cout<<"input initial values of x and y:"<<endl;
cin>>x>>y;
cout<<"input x at which y is required:"<<endl;
cin>>xp;
cout<<"input step size, h:"<<endl;
cin>>h;
n = (int) ((xp - x)/h + 0.5);
cout<<endl;
cout<<"------------------------------------"<<endl;
cout<<setw(5)<<"STEP"<<setw(15.9)<<"X"<<setw(15.9)<<"Y"<<endl;
cout<<"------------------------------------"<<endl;
for(i=1; i<=n; i++)
{ m1 = func(x, y);
m2 = func(x + 0.5*h, y + 0.5*m1*h);
m3 = func(x + 0.5*h, y + 0.5*m2*h);
m4 = func(x+h, y + m3*h);
x = x + h;
y = y + (m1 + 2.0*m2 + 2.0*m3 + m4) * h/6.0;
cout<<setw(5)<<i<<setw(15.9)<<x<<setw(15.9)<<y<<endl;
}
cout<<"the value of y at x = "<<x<<" is "<<y<<endl;
getch();
clrscr();
}
float func(float x, float y)
{ float f;
f = y + sqrt(y);
return(f);
}

No comments:

Post a Comment