0% found this document useful (0 votes)
37 views

NM Lab Filf 2015

This document contains code for numerical methods like bisection method, Euler's method, Gauss elimination method, Gauss-Jordan method, Lagrange interpolation, Newton-Raphson method, Regula Falsi method, Runge-Kutta method, Simpson's rule, and trapezoidal rule. The codes include functions to calculate integrals and derivatives and implement the algorithms to find roots of equations or solve systems of equations. Each method is presented with a brief description and the C++ code to program the algorithm.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
37 views

NM Lab Filf 2015

This document contains code for numerical methods like bisection method, Euler's method, Gauss elimination method, Gauss-Jordan method, Lagrange interpolation, Newton-Raphson method, Regula Falsi method, Runge-Kutta method, Simpson's rule, and trapezoidal rule. The codes include functions to calculate integrals and derivatives and implement the algorithms to find roots of equations or solve systems of equations. Each method is presented with a brief description and the C++ code to program the algorithm.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 11

A

PRACTICAL FILE ON
Numerical method
LAB

Submitted by: Submitted to:


Mr.. Mr. Jagjeet Singh
Branch: - Mechanical (Inst. Mech. Engg. Dept
Group:- M1G1
Roll No.:- 4001
Semester:- 5th
BISECTION METHOD
#include<iostream.h>
#include<iomanip.h>
#include<math.h>
float f(float x)
{
return(x*x*x-4*x-9);
}
void bisect (float*x,float a,float b,int*itr)
{
*x=(a+b)/2;
++(*itr);
cout<<"iteration
no."<<setw(3)<<*itr<<"x="<<setw(7)<<setprecision(5)<<*x<<endl;
}
int main()
{
int itr=0,maxitr;
float x,a,b,aerr,x1,fixed;
cout<<"enter the values of a,b,allowed error,maximum iteration"<<endl;
cin>>a>>b>>aerr>>maxitr;
cout<<fixed;
bisect(&x,a,b,&itr);
do
{
if(f(a)*f(x)<0)
b=x;
else
a=x;
bisect(&x1,a,b,&itr);
if(fabs(x1-x)<aerr)
{
cout<<"after"<<itr<<"iteration,root="<<setw(6)<<setprecision(4)<<x1<<endl;
return 0;
}
x=x1;
}
while(itr<maxitr);
cout<<"solution does not converge,iterations does not sufficient"<<endl;
return 1;
}
EULARS METHOD
#include<iostream.h>
#include<iomanip.h>
float df(float x,float y)
{
return x+y;
}
int main()
{
float x0,y0,h,x,x1,y1;
int i,n,fixed;
cout<<"enter the values of x0,y0,h,x"<<endl;
cin>>x0>>y0>>h>>x;
cout<<fixed;
x1=x0;y1=y0;
while(1)
{
if(x1>x) return 0;
y1+=h*df(x1,y1);
x1+=h;
cout<<"when
="<<setw(3)<<setprecision(1)<<x1<<"y="<<setw(4)<<setprecision(2)<<y1
<<endl;
}
}
GAUSSELIMINATION METHOD

#include<iostream.h>
#include<iomanip.h>
#include<math.h>
#define N 4
int main()
{
float a[N][N+1],x[N],t,s;
int i,j,k,fixed;
cout<<"enter the elements of the augmented matrix rowwise"<<endl;
cout<<fixed;
for(i=0;i<N;i++)
for(j=0;j<N+1;j++)
cin>>a[i][j];
for(j=0;j<N-1;j++)
for (i=j+1;i<N;i++)
{
t=a[i][j]/a[j][j];
for (k=0;k<N+1;k++)
a[i][k] -=a[j][k]*t;
}
/* now printing the upper triangular matrix */
cout<<"the upper triangular matrix is:-"<<endl;
for(i=0;i<N;i++)
{
for(j=0;j<N+1;j++)
cout<<setw(8)<<setprecision(4)<<a[i][j];
cout<<endl;
}
/* now performing back susbstitution */
for (i=N-1;i>=0;i--)
{
s = 0;
for (j=i+1;j<N;j++)
s +=a[i][j]*x[j];
x[i] = (a[i][N]-s)/a[i][i];
}
/* now printing the results */
cout<<"the solution is:-"<<endl;
for (i=0;i<N;i++)
cout<<"x["<<setw(3)<<i+1<<"]="<<setw(7)<<setprecision(4)<<x[i]<<endl;
return 0;
}

GAUSSJORDON METHOD

#include<iostream.h>
#include<iomanip.h>
#define N 4
int main()
{
float a[N][N+1],t;
int i,j,k,fixed;
cout<<"enter the elements of the augmented matrix rowwise"<<endl;
for(i=0;i<N;i++)
for(j=0;j<N+1;j++)
cin>>a[i][j];
/* now calculating the values of x1,x2,x3,.......xN */
cout<<fixed;
for(j=0;j<N;j++)
for (i=0;i<N;i++)
if(i!=j)
{
t=a[i][j]/a[j][j];
for (k=0;k<N+1;k++)
a[i][k] -=a[j][k]*t;
}
/* now printing the diagonal matrix */
cout<<"the diagonal matrix is:-"<<endl;
for(i=0;i<N;i++)
{
for(j=0;j<N+1;j++)
cout<<setw(9)<<setprecision(4)<<a[i][j];
cout<<endl;
}
/* now printing the results */
cout<<"the solution is:-"<<endl;
for (i=0;i<N;i++)
cout<<"x["<<setw(3)<<i+1<<"]="<<setw(7)<<setprecision(4)<<a[i][N]/a[i][i]<<endl;
return 0;
}
LANGRANGES INTERPOLATION FORMULA
#include<iostream.h>
#include<iomanip.h>
#define MAX 100
int main()
{
float ax[MAX+1],ay[MAX+1],nr,dr,x,y=0;
int i,j,n,fixed;
cout << "enter the value of n" << endl;
cin>>n;
cout<<"enter the set of values"<<endl;
for (i=0;i<=n;i++)
cin>>ax[i]>>ay[i];
cout<<"enter the value of x for which value of y is wanted "<<endl;
cin>>x;
cout<<fixed;
for(i=0;i<=n;i++)
{
nr=dr=1;
for(j=0;j<=n;j++)
if(j!=i)
{
nr*=x-ax[j];
dr*=ax[i]-ax[j];
}
y += (nr/dr)*ay[i];
}
cout<<"when
x="<<setw(4)<<setprecision(1)<<x<<"y="<<setw(7)<<setprecision(1)<<y<<endl;
return 0;
}
NEWTON RAPHSON METHOD
#include<iostream.h>
#include<iomanip.h>
#include<math.h>
float f(float x)
{
return x*log10(x)-1.2;
}
float df(float x)
{
return log10(x)+0.43429;
}
int main()
{
int itr,maxitr;
float h,x0,x1,aerr,fixed;
cout<<"enter x0,allowed error,maxiterations"<<endl;
cin>>x0>>aerr>>maxitr;
cout<<fixed;
for(itr=1;itr<=maxitr;itr++)
{
h=f(x0)/df(x0);
x1=x0-h;
cout<<"iteration
no."<<setw(3)<<itr<<"x="<<setw(9)<<setprecision(6)<<x1<<endl;
if(fabs(h)<aerr)
{
cout<<"after
no."<<setw(3)<<itr<<"iteration,root="<<setw(8)<<setprecision(6)<<x1;
return 0;
}
x0=x1;
}
cout<<"iterations not sufficient,solution does not converse"<<endl;
return 1;
}
REGULA FALSI METHOD
#include<iostream.h>
#include<iomanip.h>
#include<math.h>
float f (float x)
{
return cos(x)-x*exp(x);
}
void regula (float *x, float x0, float x1,float fx0, float fx1, int *itr)
{
*x =x0-((x1-x0)/(fx1-fx0))*fx0;
++ (*itr);
cout<<"iteration no."<<setw(3)<<*itr<<
"x="<<setw(7)<<setprecision(5)<<*x<<endl;
}
int main()
{
int itr=0, maxitr;
float x0,x1,x2,x3,aerr,fixed;
cout<< "enter the values for x0,x1, allowed error, maximum iterations"<<endl;
cin>>x0>>x1>>aerr>>maxitr;
regula(&x2,x0,x1,f(x0),f(x1),&itr);
cout<<fixed;
do
{
if (f(x0)*f(x2)<0)
x1=x2;
else
x0=x2;
regula (&x3,x0,x1,f(x0),f(x1),&itr);
if(fabs(x3-x2)<aerr)
{
cout<<"after"<<itr<<"itrerations"<<"root="<<setw(6)<<setprecision(4)<<x3<<endl;
return 0;
}
x2=x3;
}
while(itr<maxitr);
cout<<"solution does not converge iteration not sufficient"<<endl;
return 1;
}
RUNGE-KUTTA METHOD
#include<iostream.h>
#include<iomanip.h>
float f(float x , float y)
{
return x+y*y;
}
int main()
{
float x0,y0,h,xn,x,y,k1,k2,k3,k4,k;
int fixed;
cout<<"enter the valued of x0,y0,h,xn"<<endl;
cin>>x0>>y0>>h>>xn;
x=x0; y=y0;
cout<<fixed;
while(1)
{
if(x==xn) break;
k1=h*f(x,y);
k2=h*f(x+h/2,y+k1/2);
k3=h*f(x+h/2,y+k2/2);
k4=h*f(x+h,y+k3);
k=(k1+(k2+k3)*2+k4)/6;
x+=h; y+=k;
cout<<"whenx="<<setprecision(4)<<setw(8)<<x<<"y="<<setw(8)<<y<<endl
;
}
return 0;
}
SIMPSONS RULE

#include<iostream.h>
#include<iomanip.h>
float y(float x)
{
return 1/(1+x*x);
}
int main()
{
float x0,xn,h,s;
int i,n,fixed;
cout<<"enter x0,xn,no.of subintervals"<<endl;
cin>>x0>>xn>>n;
cout<<fixed;
h=(xn-x0)/n;
s=y(x0)+y(xn)+4*y(x0+h);
for (i=3;i<=n-1;i+=2)
s+=4*y(x0+i*h)+2*y(x0+(i-1)*h);
cout<<"value of integral is"<<setw(6)<<setprecision(4)<<(h/3)*s<<endl;
return 0;
}
TRAPEZOIDAL RULE

#include<iostream.h>
#include<iomanip.h>
float y(float x)
{
return 1/(1+x*x);
}
int main()
{
float x0,xn,h,s;
int i,n,fixed;
cout<<"enter x0,xn,no.of subintervals"<<endl;
cin>>x0>>xn>>n;
cout<<fixed;
h=(xn-x0)/n;
s=y(x0)+y(xn);
for (i=1;i<=n-1;i++)
s+=2*y(x0+i*h);
cout<<"value of integral is"<<setw(6)<<setprecision(4)<<(h/2)*s<<endl;
return 0;
}

You might also like