Tuesday, April 30, 2013

LAB REPORT FOR NUMERICAL METHODS 1



Bisection Method

#include<iostream.h>
#include<conio.h>
#include<math.h>
#define EPS 0.000001
#define F(x) log(x)-cos(x)
void bim(float*a, float*b, float*root, float*s, int*count);
void main()
{ int count;
float a, b, root, s;
cout<<"SOLUTION BY BISECTION METHOD:"<<endl;
cout<<"input starting values:"<<endl;
cin>>a>>b;
bim(&a, &b, &root, &s, &count);
if (s==0)
{ cout<<"starting points donot bracket any root"<<endl;
cout<<"check whether they bracket EVEN roots."<<endl;
}
else
{ cout<<"root="<<root<<endl;
float fun = F(root);
cout<<"f(root)="<<fun<<endl;
cout<<"Iterations="<<count<<endl;
}
getch();
clrscr();
}
void bim(float*a, float*b, float*root, float*s, int*count)
{ float x1, x2, x0, f0, f1, f2;
x1=*a;
x2=*b;
f1=F(x1);
f2=F(x2);
if(f1*f2> 0)
{ *s=0;
return;
}
else
{ *count=0;
begin:
x0=(x1+x2)/2.0;
f0=F(x0);
if(f0==0)
{ *s=1;
*root=x0;
return;
}
if(f1*f0<0)
{ x2=x0;
}
else
{ x1=x0;
f1=f0;
}
if(fabs((x2-x1)/x2) < EPS)
{ *s=1;
*root=(x1+x2)/2.0;
return;
}
else
{ *count=*count+1;
goto begin;
}
}
}




Secant Method

#include<iostream.h>
#include<conio.h>
#include<math.h>
#define EPS 0.000001
#define MAXIT 50
#define F(x) exp(x)-x-2
int sec(float*a, float*b, float*x1, float*x2, float*root, int*count, int*status);
void main()
{ float a, b, root, x1, x2, fr;
int count, status;
clrscr();
cout<<"SOLUTION BY SECANT METHOD"<<endl;
cout<<"Input two starting points:"<<endl;
cin>>a>>b;
sec(&a, &b, &x1, &x2, &root, &count, &status);
if(status==1)
{ cout<<"DIVISION BY ZERO"<<endl;
cout<<"last x1="<<x1<<endl<<"last x2="<<x2<<endl;
cout<<"number of iterations="<<count<<endl;
}
if(status==2) csitnepal
Source: www.csitnepal.com Page 6
{ cout<<"NO CONVERGENCE IN"<<MAXIT<<" ITERATIONS."<<endl;
}
else
{ cout<<"root="<<root<<endl;
fr=F(root);
cout<<"function Value at root="<<fr<<endl;
cout<<"no. of iterations="<<count<<endl;
}
getch();
}
int sec(float*a, float*b, float*x1, float*x2, float*root, int*count, int*status)
{ float x3, f1, f2, error;
*x1=*a;
*x2=*b;
f1=F(*a);
f2=F(*b);
*count=1;
begin:
if (fabs(f1-f2)<=1.E-10)
{ *status=1;
return 0;
}
x3=*x2-f2*(*x2-*x1)/(f2-f1);
error=fabs((x3-*x2)/x3);
if (error>EPS)
{ if (*count==MAXIT)
{ *status=2;
return 0;
}
else
{ *x1=*x2;
}
*x2=x3;
f1=f2;
f2=F(x3);
*count=*count+1;
goto begin;
}
else
{ *root=x3;
*status=3;
return 0;
}
}



Newton-Raphson Method

#include<iostream.h>
#include<conio.h>
#include<math.h>
#define EPS 0.000001
#define MAXIT 20
#define F(x) (x)*(x)*(x)+(x)*(x)-3*(x)-3
#define FD(x) 3*(x)*(x)+2*(x)-3
void main()
{ clrscr();
int count;
float x0, xn, fx, fdx, fxn;
cout<<"SOLUTION BY NEWTON RAPHSON'S METHOD"<<endl;
cout<<"input initial value of x:"<<endl;
cin>>x0;
count=1;
begin:
fx=F(x0);
fdx=FD(x0);
xn=x0-fx/fdx;
if (fabs((xn-x0)/xn) < EPS)
{ cout<<"root="<<xn<<endl;
fxn =F(xn);
cout<<"function value="<<fxn<<endl;
cout<<"no. of iterations="<<count<<endl;
}
else
{ x0=xn;
count=count+1;
if(count<MAXIT)
{ goto begin;
}
else
{ cout<<"SOLUTION DOESNOT CONVERGE."<<endl;
cout<<"iterations="<<MAXIT<<endl;
}
}
getch();
}
csitnepal



Fixed-point Iteration Method

#include<iostream.h>
#include<conio.h>
#include<iomanip.h>
#include<math.h>
#define EPS 0.000001
#define G(x) 2.0-(x)*(x)
void main()
{ clrscr();
int MAXIT, i;
float x0, x, error;
cout<<"SOLUTON BY FIXED POINT METHOD"<<endl;
cout<<"input initial estimate of a root:"<<endl;
cin>>x0;
cout<<"Maximum iterations allowed:"<<endl;
cin>>MAXIT;
cout<<"iteration value of X error:"<<endl;
for(i=1; i<=MAXIT; i++)
{ x = G(x0);
error=fabs((x-x0)/x);
cout<<setw(10)<<i<<setw(10)<<x<<setw(10.8)<<error<<endl;
if(error < EPS)
goto end;
else
x0=x;
}
cout<<"process doesnot converge to a root"<<endl;
cout<<"exit from the iteratoion loop"<<endl;
end:
;
getch();
}





No comments:

Post a Comment