0% found this document useful (0 votes)
16 views22 pages

Finale File

Uploaded by

RTTI TECH
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views22 pages

Finale File

Uploaded by

RTTI TECH
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 22

Practical 1

Program to find bisection method.

#include<iostream.h>

#include<conio.h>

#include<math.h>

#define f(x)(x)*(x)*(x)+(x)*(x)+(x)+7

void main()

{ int i=1; float

x0,x1,x2,e=0.0001;

double f0,f1,f2; clrscr();

cout<<"x0=";

cin>>x0;

cout<<"x1=";

cin>>x1; do

x2=(x0+x1)/2; f0=f(x0); f1=f(x1); f2=f(x2); cout<<"\

nx0="<<x0<<endl<<"x1="<<x1<<endl<<"x2="<<x2;

cout<<endl<<"f0="<<f0<<endl<<"f1="<<f1<<endl<<"f2="<<f2<<endl

; if(f0*f2<0)

x1=x2; cout<<"\nx1="<<x1;

else

x0=x2; cout<<"\nx0="<<x0;
} i+

+;

}while(fabs(f2)<e); cout<<"\

nApproximate root="<<x2; getch();

Output:-
Practical 4

Give a program to find secant method using f(x)=x2-4x-10.

#include<iostream.h>

#include<conio.h>

#include<math.h>

#define f(x)(x)*(x)-4*(x)-10;

void main()

float x3,x1,x2,e=0.0001;

double t,f1,f2; clrscr();

cout<<"x1="; cin>>x1; cout<<"x2="; cin>>x2; cout<<"\nx1\t\tx2\t\tx3\t\tf(x1)\t\tf(x2)";

cout<<"\n \n";

do

f1=f(x1); f2=f(x2); x3=x2-((f2*(x2-x1))/(f2-f1)); cout<<"\n"<<x1<<"\t\

t"<<x2<<"\t\t"<<x3<<"\t\t"<<f1<<"\t\t"<<f2; x1=x2; x2=x3; if(f2<0)

t=fabs(f2);

else

t=f2;

}
}while(t>e); cout<<"\nApproximate

root="<<x3; getch();

Output:-
Practical 2

Program to find False position method.

#include<iostream.h>

#include<conio.h>

#include<math.h>

#define f(x)3*(x)-1-cos(x)

void main()

{ int i=1; float

x0,x1,x2,e=0.0001;

double f0,f1,f2; clrscr();

do

cout<<"x0=";

cin>>x0;

}while(f(x0)>0); do

cout<<"x1=";

cin>>x1;

}while(f(x1)<0); do

cout<<"\n\tx0\tx1\tx2\tf0\tf1\tf2";

f0=f(x0);

f1=f(x1); x2=x0-((f0*(x1-x0))/(f1-f0)); f2=f(x2); cout<<"\n\t"<<x0<<"\

t"<<x1<<"\t"<<x2<<"\t"<<f0<<"\t"<<f1<<"\t"<<f2; if(f0*f2<0)
{

x1=x2;

else

x0=x2;

} i+

+;

}while(fabs(f2)>e); cout<<"\

nApproximate root="<<x2; getch();

Output:-
Practical 3

Explain program to find general Newton Raphson

Method.

#include<iostream.h>

#include<conio.h>

#include<math.h>

#define f(x)(x)*(x)-4*(x)-10;

#define fd(x)2*(x)-4;

void main()

float x0,x1,f0,fd0,e,E=0.0001; clrscr();

cout<<"\t\t\tNewton Raphson Method"; cout<<"\n\nFinding roo of x^2-4x-10";

cout<<"Enter the initial value"; cin>>x0; cout<<"\nf(x0)\t\tfd(x0)\t\tx1\t\

terror"; cout<<"\n ";

begin: f0=f(x0);

fd0=fd(x0);

x1=x0-(f0/fd0);

e=fabs((x1-x0)/x1);

if(e<E)

cout<<"\n\n\nApproximate root="<<x1;

else

{
cout<<"\n"<<f(x0);

cout<<"\t\t"<<fd(x0);

cout<<"\t\t"<<x1;

cout<<"\t\t"<<e;

x0=x1; goto begin;

getch();

Output:-
Practical 5

Program to find Gauss elimination method.

#include<iostream.h>

#include<conio.h>

void main()

float m[4][4],t,t1,x,y,z;

int i,j,n; clrscr();

cout<<"Enter the number of columns ";

cin>>n; for(i=0;i<n;i++)

for(j=0;j<n;j++)

cin>>m[i][j];

cout<<"Enter the value of constant:";

cin>>m[i][j];

cout<<"Matrix:\n"; for(i=0;i<n;i++)

for(j=0;j<n;j++)

cout<<m[i][j]<<" ";

}
cout<<"\t"<<m[i][j]<<"\n";

t=m[1][0]/m[0][0]; t1=m[2][0]/m[0][0];

for(i=0,j=0;j<n+1;j++)

m[i+1][j]=m[i+1][j]-(m[i][j]*t);

m[i+2][j]=m[i+2][j]-(m[i][j]*t1);

t=m[2][1]/m[1][1]; for(i=1,j=0;j<n+1;j++)

m[i+1][j]=m[i+1][j]-(m[i][j]*t);

cout<<"\n";

for(i=0;i<n;i++)

for(j=0;j<n;j++)

cout<<m[i][j]<<" ";

cout<<"\n";

z=m[2][3]/m[2][2]; y=(m[1][3]-m[1][2])*z/m[1][1];

x=(((m[0][3]-m[0][2])*z)-(m[0][1]*y))/m[0][0];
cout<<"\nx="<<x<<"\ny="<<y<<"\nz="<<z; getch();

Output:-
Practical 7

Program to find Lagrange’s Interpolation method.

#include<iostream.h>

#include<conio.h>

void main()

float x[100],y[100],xp,yp=0,p;

int i,j,n;

clrscr();

cout<<"Enter the number of data: ";

cin>>n; cout<<"Enter the data

:";<<endl; for(i=1;i<=n;i++)

cout<<"x["<<i<<"]="; cin>>x[i];

cout<<"y["<<i<<"]="; cin>>y[i];

cout<<"Enter interpolation point:

"; cin>>xp; for(i=1;i<=n;i++)

{ p=

1;

for(j=1;j<=n;j++)

if(i!=j)

p=p*(xp-x[j])/(x[i]-x[j]);
}

yp=yp+p*y[i];

cout<<endl<<"Interpolated value at "<<xp <<"is "<<yp; getch();

Output:-
Practical 9

Explain program to find trapezoidal rule.

#include<iostream.h>

#include<conio.h>

#include<math.h>

double f(double x)

double a=1/(1+x*x);

return a;

void main()

{ int n,i; double a,b,h,s=0,in;

clrscr(); cout<<"initial limit

a="; cin>>a; cout<<"final

limit b="; cin>>b;

cout<<"Enter the value of

n"; cin>>n; double

x[n+1],y[n+1]; h=(b-a)/n;

for(i=0;i<=n;i++)

x[i]=a+i*h;

y[i]=f(x[i]);

for(i=1;i<n;i++)

{
s=s+h*y[i];

in=h/2.0*(y[0]+y[n])+s; cout<<"The

definite integral is "<<in<<endl; getch();

Output:-
Practical 8

Program to find simpson’s 1/3 rule.

#include<iostream.h>

#include<conio.h>

#include<math.h>

#define f(x) 1-exp(-(x)/2.0);

void main()

int m,n,i; float a,b,h,x,s,in,f1,f2,f3,f;

clrscr(); cout<<"Enter the value of a:";

cin>>a; cout<<"Enter the value of b:";

cin>>b; cout<<"Enter the even no. of

segment:"; cin>>n; h=(b-a)/n; m=n/2;

s=0.0; x=a; f1=f(x); for(i=1;i<=m;i++)

f2=f(x+h);

f3=f(x+2*h);

s=s+f1+4*f2+f3; f1=f3;

x=x+2*h;

in=s*h/3.0; cout<<"\nIntegral from "<<a<<" to

"<<b<<endl; vout<<"\nWhen h="<<h<<" is

"<<in<<endl; getch();

}
Output:-
Practical 6

Program to find simpson’s 3/8 rule.

#include<iostream.h>

#include<conio.h> #include<math.h>

float sim(float);

void main()

int m,n,i; float a,b,h,s,in; clrscr(); cout<<"Enter the value of

a:"; cin>>a; cout<<"Enter the value of b:"; cin>>b;

cout<<"Enter the no. of subinterval(should be multiple of

3):"; cin>>n; h=(b-a)/n; s=0.0; s=sim(a)+sim(b); cout<<"The

value of h:"<<h; for(i=1;i<=n;i++)

{ if(i

%3==0)

s=s+2*sim(a+i*h);

else

s=s+3*sim(a+i*h);

in=s*3*h/8; cout<<"\nThe define integral is

"<<in<<endl; getch();

}
float sim(float x)

float temp;

temp=1/(1+(x*x));

return(temp);

Output:-
Practical 10

Program to find Runge kutta Method.

#include<iostream.h>

#include<conio.h>

#include<math.h>

#defineF(x,y) (y)-(x);

#define dy/dx=y-x;

void main()

double x0,y0,y1,n,h,f,k1,k2;

int i; clrscr(); cout<<"Enter

the value of x0:"; cin>>x0;

cout<<"Enter the value of

y0:"; cin>>y0; cout<<"Enter

the value of n:"; cin>>n;

cout<<"Enter the value of h:";

cin>>h;

for(x0=1;x0<n;x0=x0+h)

{ f=F(x0,y0

);

k1=h*f;

f=F(x0+h,y0+k1);

k2=h*f;

cout<<"k1="<<k1;

cout<<"\nk2="<<k
2;

cout<<"\ny="<<y;

y0=y1;

getch();

Output:-

You might also like