You are on page 1of 7

12/4/2011

4. GAUSS SIEDEL METHOD

VIPUL GUPTA
2k8/EE/889

#include<iostream.h>
#include<conio.h>
#include<math.h>
void main()
{clrscr();
float a[5][5],b[5][5];
float x,y,z;
int i,j,p,q,r;
int max=0;
int n[4];
cout<<"enter coefficients of equation ax+by+cz=d \n";
for(i=1;i<4;i++)
{
cout<<"\nenter coefficients of "<< i <<"equation\n";
for(j=1;j<5;j++)
{ cin>>a[i][j];
// cout<<"\t";
}
cout<<"\n";
}
for(i=1;i<4;i++)
{ max=0;
for(j=1;j<4;j++)
{if (a[i][j]>max)
{max=a[i][j];
n[i]=j;
}
}
cout<<" max term in "<< i<<" equation is "<<max<<" at the position "<<n[i]<<"\n" ;
}
y=z=0;
for(int m=0;m<2000;m++)
{
for ( p=1;p<4;p++)
{ if(n[p]==1)
{ x=((a[p][4]-(z*a[p][3])-(y*a[p][2]))/a[p][1]);

for ( q=1;q<4;q++)
{ if(n[q]==2)
{
y=((a[q][4]-(x*a[q][1])-(z*a[q][3]))/a[q][2]);
for ( r=1;r<4;r++)
{ if(n[r]==3)
{
z=((a[r][4]-(x*a[r][1])-(y*a[r][2]))/a[r][3]);
}}}}}}}
cout<<"\n value of x : "<<x;
cout<<"\n value of y : "<<y;
cout<<"\n value of z :"<<z;
getch();
}
OUTPUT:

19/4/2011

5. EULERs METHOD

// Write a program to solve First order initial value problem


// A First Order differential dy/dx=1+xy2 0x1 and y(0)=1
#include<iostream>
#include<conio.h>
#include<cmath>
using namespace std;
float fdash(float x,float y)
{
float temp;
temp= 1+x*y*y;
return temp;
}
int main()
{
float x=0,h;
float y[60];
int i;
h=0.02;
y[0]=1;
for(i=1;i<50;i++)
{
y[i]=y[i-1]+fdash(x,y[i-1])*h;
x=x+h;
cout<<" Value of F(x,y) at "<<x<<" is "<<y[i]<<endl;
}
system("pause");
}
OUTPUT
Value of F(x,y) at 0.02 is 1.02
Value of F(x,y) at 0.04 is 1.04042
Value of F(x,y) at 0.06 is 1.06128
Value of F(x,y) at 0.08 is 1.08263
Value of F(x,y) at 0.1 is 1.10451
Value of F(x,y) at 0.12 is 1.12695
Value of F(x,y) at 0.14 is 1.15
Value of F(x,y) at 0.16 is 1.1737
Value of F(x,y) at 0.18 is 1.19811
Value of F(x,y) at 0.2 is 1.22328
Value of F(x,y) at 0.22 is 1.24926
Value of F(x,y) at 0.24 is 1.27613
Value of F(x,y) at 0.26 is 1.30395
Value of F(x,y) at 0.28 is 1.33279
Value of F(x,y) at 0.3 is 1.36273

VIPUL GUPTA
2k8/EE/889

Value of F(x,y) at 0.32 is 1.39388


Value of F(x,y) at 0.34 is 1.42631
Value of F(x,y) at 0.36 is 1.46014
Value of F(x,y) at 0.38 is 1.49549
Value of F(x,y) at 0.4 is 1.53249
Value of F(x,y) at 0.42 is 1.57128
Value of F(x,y) at 0.44 is 1.61202
Value of F(x,y) at 0.46 is 1.65489
Value of F(x,y) at 0.48 is 1.70008
Value of F(x,y) at 0.5 is 1.74783
Value of F(x,y) at 0.52 is 1.79838
Value of F(x,y) at 0.54 is 1.85201
Value of F(x,y) at 0.56 is 1.90906
Value of F(x,y) at 0.58 is 1.96988
Value of F(x,y) at 0.6 is 2.03489
Value of F(x,y) at 0.62 is 2.10458
Value of F(x,y) at 0.64 is 2.1795
Value of F(x,y) at 0.66 is 2.2603
Value of F(x,y) at 0.68 is 2.34774
Value of F(x,y) at 0.7 is 2.4427
Value of F(x,y) at 0.72 is 2.54624
Value of F(x,y) at 0.74 is 2.6596
Value of F(x,y) at 0.76 is 2.78429
Value of F(x,y) at 0.78 is 2.92212
Value of F(x,y) at 0.8 is 3.07532
Value of F(x,y) at 0.82 is 3.24665
Value of F(x,y) at 0.84 is 3.43951
Value of F(x,y) at 0.86 is 3.65826
Value of F(x,y) at 0.88 is 3.90845
Value of F(x,y) at 0.9 is 4.1973
Value of F(x,y) at 0.92 is 4.53442
Value of F(x,y) at 0.94 is 4.93274
Value of F(x,y) at 0.96 is 5.41018
Value of F(x,y) at 0.98 is 5.99216
Press any key to continue . . .
Graph of the Function obtained in the above Problem.
6
5.5
5
4.5
4
3.5
3
2.5
2
1.5
1
0

0.1

0.2

0.3

0.4

0.5

0.6

0.7

0.8

0.9

26/4/2011

6. SHOOTING METHOD

VIPUL GUPTA
2k8/EE/889
// Write a Program to solve a 2nd order Linear differential equation by SHOOTING METHOD
/* A Boundary Value Problem is given
d2y/d2x= 2y + 8x(9 x)
y(0) = 0 and y(9) = 0
*/
#include<iostream>
#include<conio.h>
#include<cmath>
using namespace std;
float y[4],z[4],fv=0,h=3,x;
int i,j;
float fdd(float x,float y)
{
float temp=2*y+8*x*(9-x);
return temp;
}
void solve(float d)
{
x=0;
z[0]=d;
for(i=1;i<4;i++)
{
y[i]=y[i-1]+z[i-1]*h;
z[i]=z[i-1]+fdd(x,y[i-1])*h;
x=x+h;
}
}
int main()
{
y[0]=0;
float l,m,q,k;
solve(4);
// Finding an interval in which Final value of function Lies
if(y[3]>fv)
{
for( i=1 , j=-1; ;i++,j--)
{
solve(i);

if(y[3]<=fv)
break;
solve(j);
if(y[3]<=fv)
break;
}
solve(i);
l=y[3];
solve(j);
m=y[3];
if(y[3]<=fv)
k=i;
else
k=j;
if(l<fv&&m<fv)
{
if(l>m)
k=i;
else
k=j;
}
}
else
{
for( i=1 , j=-1; ;i++,j--)
{
solve(i);
if(y[3]>=fv)
break;
solve(j);
if(y[3]>=fv)
break;
}
solve(i);
l=y[3];
solve(j);
m=y[3];
if(y[3]>=fv)
k=i;
else
k=j;
if(l>fv&&m>fv)
{
if(l>m)

k=j;
else
k=i;
}
}
solve(0);
l=y[3];
solve(k);
m=y[3];
q=fv;
// Interpolation
k=k+((k-0)/(m-l))*(q-m);
cout<<endl<<k;
solve(k);
cout<<"Final Value at "<<k<<" is "<<y[3]<<endl;
// Solution to equation
x=0;
for(i=0;i<4;i++)
{
cout<<"Value of function at "<<x<<" is "<<y[i]<<endl;
x= x+h;
}
system("pause");
}
/*
OUTPUT
-20.5714
Final Value at -20.5714 is -1.52588e-005
Value of function at 0 is 0
Value of function at 3 is -61.7143
Value of function at 6 is -123.429
Value of function at 9 is -1.52588e-005
Press any key to continue . . .
/*
Following GRAPH is obtained by solving above 2nd order Differential equation
0

-20

-40

-60

-80

-100

-120

-140

You might also like