You are on page 1of 43

# PRACTICAL FILE

Of

## PTU Giani Zail Singh Campus Bathinda

SUBMITTED BY: Name-Gurjinder Singh Roll No.100311129870 Mechanical 5th Semester

S.No.

TOPIC To find the root of Algebraic Equation using Bisection Method. To find the root of Transcendental Eqn using Bisection Method. To find the root of Algebraic Equation using Regula-Falsi Method. To find the root of Transcendental Eqn using Regula-Falsi Method. To find the root of Algebraic Equation using Newton-Raphson Method. To find the root of Transcendental Eqn using Newton-Raphson Method. To find the solution of Simultaneous Equations using Gauss Elimination Method. To find the solution of Simultaneous Equations using Gauss Jordon Method. To find the value of f(x) from the given data using Lagranges Interpolation Method. To find the solution of Ordinary Differential Eqn using Eular Method. To find the solution of Ordinary Differential Eqn using Runge- Kutta Method of 2nd Order.

Page No.

Signature

1.

## 1-3 4-5 6-8 9-10 11-12 13-14

2.

3.

4.

5.

6.

7.

15-17

8.

18-21

9.

22-24

10.

25-27

11.

28-29

S.No.

TOPIC To find the solution of Ordinary Differential Eqn using Runge- Kutta Method of 4th order To fit the straight line (y=ax+ b) on given data using method of least squares. Fit the Curve ( y=ax2 +bx +c ) on given data using method of least squares To fit the Exponential Curve(y=aebx)on given data using method of least squares.

Page No.

Signature

12.

30-31

13.

14.

15.

Aim Program :

## : To find the root of Algebraic Equation using Bisection Method.

#include<iostream.h> #include<conio.h> float f(float x); int p,q,r,s,n; void main() { clrscr(); float a,b,c,d,temp; cout<<"enter the coff. of x^3:"; cin>>p; cout<<"enter the coff. of x^2:"; cin>>q; cout<<"enter the coff. of x:"; cin>>r; cout<<"enter the constant term:"; cin>>s; for(int i=-15;i<10;i++) { a=i; b=i+1; if((f(a)*f(b))<0) { if(f(b)<f(a)) {temp=a; a=b; b=temp; } cout<<"\n"<<"enter the no. of iteration:"; cin>>n; for(int i=0;i<n;i++) { c=(b+a)/2; cout<<"\n"<<i+1<<"iteration"<<"\t"<<c<<"\t"<<f(c)<<endl; if(f(c)<0) a=c; else b=c; } } } getch(); } float f(float x) { return(p*x*x*x+q*x*x+r*x+s); }

www.facebook.com/Gurjinder.Romana

www.facebook.com/PTUGZS

Que: Find Solution of Algebraic Eqn x3+x2-3x-3=0 using Bisection Method. OUTPUT:

ANSWER:

## Solution of x3+x2-3x-3=0 is x=1.73206 (Correct up to 3

decimal places)

www.facebook.com/Gurjinder.Romana

www.facebook.com/PTUGZS

Que: Find Solution of Algebraic Eqn x3-3x2-x+8=0 using Bisection Method. OUTPUT:

ANSWER:

## Solution of x3-3x2-x+8=0 is x=-1.45673 (Correct up to 3

decimal places)

www.facebook.com/Gurjinder.Romana

www.facebook.com/PTUGZS

Aim

## : To find the root of Transcendental Eqn (3x-cosx-1=0) using Bisection Method.

Program :
#include<iostream.h> #include<conio.h> #include<math.h> float f(float x); void main() { clrscr(); float a,b,c,d,temp; int n; for(int i=0;i<10;i++) { a=i; b=i+1; if((f(a)*f(b))<0) { if(f(b)<f(a)) {temp=a; a=b; b=temp; } cout<<"enter the no. of iteration:"; cin>>n; for(int i=0;i<n;i++) { c=(b+a)/2; cout<<"\n"<<i+1<<"iteration"<<"\t"<<c<<"\t"<<f(c)<<endl; if(f(c)<0) a=c; else b=c; } } } getch(); } float f(float x) { return(3*(x)-cos(x)-1);
}

www.facebook.com/Gurjinder.Romana

www.facebook.com/PTUGZS

Que OUTPUT

## : Find Solution of Transcendental Equation 3x-cosx-1=0 using Bisection Method. :

ANSWER:
Solution of 3x-cosx-1=0 is x=0.607086 (Correct up to 3
decimal places)

www.facebook.com/Gurjinder.Romana

www.facebook.com/PTUGZS

Aim

## : To find the root of Algebraic Equation using Regula-Falsi Method.

Program :
#include<iostream.h> #include<conio.h> float f(float x); int p,q,r,s,n; void main() { clrscr(); float a,b,c,d,temp; cout<<"enter the coff. of x^3:"; cin>>p; cout<<"enter the coff. of x^2:"; cin>>q; cout<<"enter the coff. of x:"; cin>>r; cout<<"enter the constant term:"; cin>>s; for(int i=-10;i<10;i++) { a=i; b=i+1; if((f(a)*f(b))<0) { if(f(b)<f(a)) {temp=a; a=b; b=temp; } cout<<"enter the no. of iteration:"; cin>>n; for(int i=0;i<n;i++) { c=a-((b-a)*f(a)/(f(b)-f(a))); cout<<"\n"<<i+1<<"iteration"<<"\t"<<c<<"\t"<<f(c)<<endl; if(f(c)<0) a=c; else b=c; } } } getch(); }

www.facebook.com/Gurjinder.Romana

www.facebook.com/PTUGZS

7
float f(float x) { return(p*x*x*x+q*x*x+r*x+s); }

www.facebook.com/Gurjinder.Romana

www.facebook.com/PTUGZS

## Que: Find Solution of Algebraic Eqn 4x3+5x2-7=0

using Regula-Falsi Method.

OUTPUT:

ANSWER:
Solution of

4x3+5x2-7=0

## is x=0.901813 (Correct upto six

decimal places)

www.facebook.com/Gurjinder.Romana

www.facebook.com/PTUGZS

Aim

## : To find the root of Transcendental Eqn (x+sin(x)-1=0) using Regula-Falsi Method.

Program :
#include<iostream.h> #include<conio.h> #include<math.h> float f(float x); void main() { clrscr(); float a,b,c,d,temp; int n; for(int i=-10;i<10;i++) { a=i; b=i+1; if((f(a)*f(b))<0) { if(f(b)<f(a)) {temp=a; a=b; b=temp; } cout<<"enter the no. of iteration:"; cin>>n; for(int i=0;i<n;i++) { c=a-((b-a)*f(a)/(f(b)-f(a))); cout<<"\n"<<i+1<<"iteration"<<"\t"<<c<<"\t"<<f(c)<<endl; if(f(c)<0) a=c; else b=c; } } } getch(); } float f(float x) { return(x+sin(x)-1);
}

www.facebook.com/Gurjinder.Romana

www.facebook.com/PTUGZS

10

Que

## : Find Solution of Transcendental Equation x+sin(x)-1=0 using Regula-Falsi Method.

OUTPUT:

ANSWER:
Solution of x+sin(x)-1=0 is x=0.510973
(Correct upto six decimal places)

www.facebook.com/Gurjinder.Romana

www.facebook.com/PTUGZS

11

Aim

## : To find the root of Algebraic Equation using Newton-Raphson Method.

Program :
#include<iostream.h> #include<conio.h> #include<math.h> float f(float x); float f1(float x); int p,q,r,s,n; void main() { clrscr(); float a,b,c,d,temp; cout<<"enter the coff. of x^3:"; cin>>p; cout<<"enter the coff. of x^2:"; cin>>q; cout<<"enter the coff. of x:"; cin>>r; cout<<"enter the constant term:"; cin>>s; for(int i=-15;i<10;i++) { a=i; b=i+1; if((f(a)*f(b))<0) { if(abs(f(b))<abs(f(a))) a=b; cout<<"enter the no. of iteration:"; cin>>n; for(int i=0;i<n;i++) { a=a-(f(a)/f1(a)); cout<<"\n"<<i+1<<"iteration"<<"\t"<<a<<"\t"<<f(a)<<endl; } } } getch(); } float f(float x) { return(p*x*x*x+q*x*x+r*x+s); } float f1(float x) { return(3*p*x*x+2*q*x+r);
}

www.facebook.com/Gurjinder.Romana

www.facebook.com/PTUGZS

12

Answer:

## (Correct upto six decimal places)

www.facebook.com/Gurjinder.Romana

www.facebook.com/PTUGZS

13

Aim

## : To find the root of Transcendental Eqn (cos(x)-xex=0)using Newton-Raphson Method.

Program :
#include<iostream.h > #include<conio.h> #include<math.h> float f(float x); float f1(float x); void main() { clrscr(); float a,b,c,d,temp; int n; for(int i=0;i<10;i++) { a=i; b=i+1; if((f(a)*f(b))<0) { if(abs(f(b))<abs(f(a))) a=b; cout<<"enter the no. of iteration:"; cin>>n; for(int i=0;i<n;i++) { a=a-(f(a)/f1(a)); cout<<"\n"<<i+1<<"iteration"<<"\t"<<a<<"\t"<<f(a)<<endl; } } } getch(); } float f(float x) { return(cos(x)-(x*exp(x))); } float f1(float x) { return(-sin(x)-x*exp(x)-exp(x)); }

www.facebook.com/Gurjinder.Romana

www.facebook.com/PTUGZS

14

OUTPUT:

Answer:

## (Correct upto six decimal places)

www.facebook.com/Gurjinder.Romana

www.facebook.com/PTUGZS

15

Aim

## : To find the solution of Simultaneous Equations using Gauss Elimination Method.

Program :
#include<stdio.h> #include<iostream.h> #include<iomanip.h> #include<math.h> #include<conio.h> #define n 3 main() { float a[n][n+1],x[n],t,s; int i,j,k; cout<<"enter the elements of the augmented matrix row wise "<<endl; 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; } 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; } 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]; } cout<<"the solution is:-"<<endl; for(i=0;i<n;i++) cout<<"x["<<setw(3)<<i+1<<"]"<<setw(7)<<setprecision(4)<<x[i]<<e ndl; return 0; } www.facebook.com/Gurjinder.Romana www.facebook.com/PTUGZS

16

Que : Find the solution of following eqn by Gauss Elimination method 4x1+4x2+6x3 = 1 4x1+6x2+4x3 = 2 x1+2x2+3x3 = 1

OUTPUT:

Answer:
Solution of Eqns is

## x1=-1 x2= 2 x3= 1

www.facebook.com/Gurjinder.Romana

www.facebook.com/PTUGZS

17

Que : Find the solution of following eqn by Gauss Elimination method 4x1+3x2+9x3 =-2 7x1+2x2-2x3 = 5 x1+4x2+2x3 = 3

OUTPUT:

Answer:

Solution of Eqns is
x1= 0.2308 x2= 1.026 x3= -0.6667

www.facebook.com/Gurjinder.Romana

www.facebook.com/PTUGZS

18

Aim

## : To find the solution of Simultaneous Equations using Gauss Jordon Method.

Program :
#include<iostream.h> #include<conio.h> #include<process.h> void main () { clrscr(); int i,j,q,c; float a[3][3],b[3],temp,k,d; cout<<"enter the values of matrix A|B (row wise):"<<endl; for(i=0;i<3;i++) { for(j=0;j<3;j++) { cin>>a[i][j]; } cin>>b[i]; } for(q=0;q<3;q++) { i=q; temp=a[i][i]; if(temp==0) { for(j=0;j<3;j++) { d=a[i][j];a[i][j]=a[i+1][j];a[i+1][j]=d; } d=b[i];b[i]=b[i+1];b[i+1]=d; } temp=a[i][i]; for(j=0;j<3;j++) { a[i][j]=a[i][j]/temp; } b[i]=b[i]/temp; for(i=0;i<3;i++) www.facebook.com/Gurjinder.Romana www.facebook.com/PTUGZS

19 { if(i!=q) { k=a[i][q]; for(j=0;j<3;j++) { a[i][j]=a[i][j]-k*a[q][j]; } b[i]=b[i]-k*b[q]; } } } cout<<endl<<"the matrix A|B will become :"<<endl; for(i=0;i<3;i++) { for(j=0;j<3;j++) { cout<<" "<<a[i][j]; } cout<<" "<<b[i]<<endl; } cout<<"X= "<<b[0]<<endl; cout<<"Y= "<<b[1] <<endl; cout<<"Z= "<<b[2] <<endl; getch(); }

www.facebook.com/Gurjinder.Romana

www.facebook.com/PTUGZS

20

Que : Find the solution of following eqn by Gauss Jordan method 2x+3y+z = 9 x+2y+3z = 6 3x+y+2z = 8 OUTPUT:

Answer:
Solution is X = 1.944 Y = 1.611 Z = 0.277

www.facebook.com/Gurjinder.Romana

www.facebook.com/PTUGZS

21

Que : Find the solution of following eqn by Gauss Jordan method 2x-3y+10z = 3 -x+4y+2z = 20 5x+2y+z =-12 OUTPUT:

Answer:
Solution is X =-4 Y = 3 Z = 2

www.facebook.com/Gurjinder.Romana

www.facebook.com/PTUGZS

22

Aim

: To find the value of f(x) from the given data using Lagranges Interpolation Method.

Program :
#include<iostream.h> #include<conio.h> int main() { int n,i,j; float mult,sum=0,x[10],f[10],a; cout<<"Enter no of sample points : "; cin>>n; cout<<"Enter all values of x and corresponding funtional value: "<<endl; cout<<"x f(x)"<<endl; for(i=0;i<n;i++) cin>>x[i]>>f[i]; cout<<"\nEnter your x for calculation : "; cin>>a; for(i=0;i<=n-1;i++) { mult=1; for(j=0;j<=n-1;j++) { if(j!=i) mult*=(a-x[j])/(x[i]-x[j]); } sum+=mult*f[i]; } cout<<"The estimated value of f(x) = "<<sum<<endl; getch(); return 0; }

www.facebook.com/Gurjinder.Romana

www.facebook.com/PTUGZS

23

## Que : Find the value of f(2.8) from the given data

using Lagranges Interpolation Method.
x y 1 2 2 8 3 29 4 64

OUTPUT:

ANSWER:

## The estimated value of f(2.8) = 23.648

www.facebook.com/Gurjinder.Romana

www.facebook.com/PTUGZS

24

Que : Find the value of f(6) from the given data using
Lagranges Interpolation Method.
x y 1 1 2 5 7 5 8 4

OUTPUT:

ANSWER:

## The estimated value of f(6)=6.23809

www.facebook.com/Gurjinder.Romana

www.facebook.com/PTUGZS

25

Aim Program

: To find the solution of Ordinary Differential Eqn (dy/dx = 2x+3y) using Eular Method. :

#include<iostream.h> #include<conio.h> float d1(float x,float y); void main() { double x0,xg,h,y0,yg; float n; int i; clrscr(); cout<<"enter the value cin>>x0; cout<<"enter the value cin>>y0; cout<<"enter the value cin>>xg; cout<<"enter the value cin>>h; n= (xg-x0)/h; cout<<"\n"<<"

## the value of n is :"<<n<<\n<<endl;

for(i=0;i<n;i++) { yg=y0+h*d1(x0,y0); x0=x0+h; y0=yg; cout<<" the value of y("<<x0<<") ="<<y0<<endl; } getch(); } float d1(float x,float y) { return((2*x)-(3*y)); }

www.facebook.com/Gurjinder.Romana

www.facebook.com/PTUGZS

26

## Que: Find the solution of Ordinary Differential Eqn

(dy/dx = 2x+3y),y(0)=0 using Eular Method at x=1.4

OUTPUT:

Answer:
value of y at 1.4 = 0.711475

www.facebook.com/Gurjinder.Romana

www.facebook.com/PTUGZS

27

## (dy/dx = x2/5y),y(0)=1 using Eular Method at x=0.5

OUTPUT:

Answer:
value of y at 0.5 = 1.00599

www.facebook.com/Gurjinder.Romana

www.facebook.com/PTUGZS

28

Aim

: To find the solution of Ordinary Differential Eqn (dy/dx = x2+y2)using Runge- Kutta Method of 2nd Order. :

Program

#include<iostream.h> #include<conio.h> float d1(float x,float y); void main() { double x0,xg,h,y0,yg,k1,k2,k; float n; int i; clrscr(); cout<<"enter the value of x0:"; cin>>x0; cout<<"enter the value of y0:"; cin>>y0; cout<<"enter the value of x :"; cin>>xg; cout<<"enter the value of h :"; cin>>h; n= (xg-x0)/h; cout<<"/n"<<"the value of n is :"<<n<<endl; for(i=0;i<n;i++) { k1=h*d1(x0,y0); k2=h*d1(x0+h,y0+k1); k=(k1+k2)/2; yg=y0+k; x0=x0+h; y0=yg; cout<<" The value of y("<<x0<<") ="<<y0<<endl; } getch(); } float d1(float x,float y) { return((x*x*x)/(2*y)); } www.facebook.com/Gurjinder.Romana www.facebook.com/PTUGZS

29

Que: Find the solution of Ordinary Differential Eqn (dy/dx = x3/2y),y(0)=1 using Runge-Kutta Method of 2nd Order at 0.5? OUTPUT:

## Answer: The value of y at 0.5 is 1.00809

www.facebook.com/Gurjinder.Romana

www.facebook.com/PTUGZS

30

Aim Program

: To find the solution of Ordinary Differential Eqn dy/dx = x + y2 using Runge- Kutta Method. :

#include<iostream.h> #include<conio.h> float d1(float x,float y); void main() { double x0,xg,h,y0,yg,k1,k2,k3,k4,k; float n; int i; clrscr(); cout<<"enter the value of x0:"; cin>>x0; cout<<"enter the value of y0:"; cin>>y0; cout<<"enter the value of x:"; cin>>xg; cout<<"enter the value of h:"; cin>>h; n= (xg-x0)/h; cout<<"\n"<<" the value of n is :"<<n<<endl; for(i=0;i<n;i++) { k1=h*d1(x0,y0); k2=h*d1(x0+(h/2),y0+(k1/2)); k3=h*d1(x0+h/2,y0+k2/2); k4=h*d1(x0+h,y0+k3); k=(k1+2*k2+2*k3+k4)/6; yg=y0+k; x0=x0+h; y0=yg; cout<<" } getch(); } float d1(float x,float y) { return(x+(y*y)); } the value of y("<<x0<<") ="<<y0<<endl;

www.facebook.com/Gurjinder.Romana

www.facebook.com/PTUGZS

31

## Que: Find approximate value of y for x=0.4 if

dy/dx = x + y2 and y(0)=1?

OUTPUT:

Answer:
The value of y at x = 0.4 is 1.78935

www.facebook.com/Gurjinder.Romana

www.facebook.com/PTUGZS

32

Aim

: To fit the straight line (y=ax+ b) on given data using method of least squares.

Program :
#include<iostream.h > #include<conio.h> void main() { clrscr(); float x[10],y[10],s1=0,s2=0,s3=0,s4=0,a,b; int i,n; cout<<"Enter the number of values:"; cin>>n; for( i=0;i< n;i++) { cout<<"x"<<i<<"="; cin>>x[i]; cout<<"y"<<i<<"="; cin>>y[i]; } for( i=0;i<n;i++) { s1=s1+x[i]; s2=s2+y[i]; s3=s3+x[i]*y[i]; s4=s4+x[i]*x[i]; } a=(s1*s2-n*s3)/(s1*s1-n*s4); b=(s4*s2-s1*s3)/(n*s4-s1*s1); cout<<endl<<"Y= "<<a<<" X + "<<b; getch(); }

www.facebook.com/Gurjinder.Romana

www.facebook.com/PTUGZS

33

## Que: Fit the straight line (Y=aX+ b) on given data

using method of least squares.
x y 0.5 15 1.0 17 1.5 19 2.0 14 2.5 10 3.0 7

OUTPUT:

Answer:
Eqn of straight line is Y=-3.77143 X + 20.2667

www.facebook.com/Gurjinder.Romana

www.facebook.com/PTUGZS

34

## Que: Fit the straight line (Y=aX+ b) on given data

using method of least squares.
x y -4 1.2 -2 2.5 0 3.2 2 7.8 4 13.2

OUTPUT:

Answer:
Eqn of straight line is Y=1.45 X + 6.24

www.facebook.com/Gurjinder.Romana

www.facebook.com/PTUGZS

35

Aim Program

: To fit the Curve ( y=ax2 +bx +c ) on given data using method of least squares. :

#include<iostream.h > #include<conio.h> #include<math.h> void main() { clrscr(); float x[10],y[10],a[3][3],b[3], k,temp ; float s1=0,s2=0,s3=0,s4=0,s5=0,s6=0,s7=0; int n,q,i,j; cout<<"Enter the number of values:"; cin>>n; for(i=0;i<n;i++) { cout<<"x"<<i<<"="; cin>>x[i]; cout<<"y"<<i<<"="; cin>>y[i]; } for(i=0;i<n;i++) { s1=s1+x[i]; s3=s3+y[i]; s2=s2+x[i]*x[i]; s4=s4+x[i]*x[i]*x[i]; s5=s5+x[i]*x[i]*x[i]*x[i]; s6=s6+x[i]*y[i]; s7=s7+x[i]*x[i]*y[i]; } a[0][0]=n; a[0][1]=s1; a[0][2]=s2; b[0]=s3; a[1][0]=s1; a[1][1]=s2; a[1][2]=s4; b[1]=s6; a[2][0]=s2; a[2][1]=s4; a[2][2]=s5; b[2]=s7; for(q=0;q<3;q++) { i=q; www.facebook.com/Gurjinder.Romana

www.facebook.com/PTUGZS

36 temp=a[i][i]; for(j=0;j<3;j++) {a[i][j]=a[i][j]/temp; } b[i]=b[i]/temp; for(i=0;i<3;i++) { if(i!=q) { k=a[i][q]; for(j=0;j<3;j++) { a[i][j]=a[i][j]-k*a[q][j]; } b[i]=b[i]-k*b[q]; } } } cout<<"Y = ("<<b[2]<<")X^2 + ("<<b[1]<<")X + ("<<b[0]<<")"<<endl; getch(); }

www.facebook.com/Gurjinder.Romana

www.facebook.com/PTUGZS

37

## Que: Fit the Curve ( Y=aX2 +bX +c ) on given data

using method of least squares.
x y -2 17 -1 19 0 22 1 26 2 30

OUTPUT:

Answer:

## Solution is Y=(0.359143)X2 +(3.3)X +(22.0857)

www.facebook.com/Gurjinder.Romana

www.facebook.com/PTUGZS

38

## Que: Fit the Curve ( Y=aX2 +bX +c )on given data

using method of least squares.
x y -3 3 0 1 2 1 4 3

OUTPUT:

Answer:

## Solution is Y =(0.178462)X2 -(0.192495)X +(0.850518)

www.facebook.com/Gurjinder.Romana

www.facebook.com/PTUGZS

39

Aim

: To fit the Exponential Curve(y=aebx)on given data using method of least squares.

Program :
#include<iostream.h > #include<conio.h> #include<math.h> void main() { clrscr(); float x[10],y[10],s1=0,s2=0,s3=0,s4=0,a1,b1,a,b; int n; cout<<"Enter the number of values:"; cin>>n; for(int i=0;i<n;i++) { cout<<"x"<<i<<"="; cin>>x[i]; cout<<"y"<<i<<"="; cin>>y[i]; } for( i=0;i<n;i++) { s1=s1+x[i]; s2=s2+log(y[i]); s3=s3+x[i]*x[i]; s4=s4+x[i]*log(y[i]); } a1=(s3*s2-s1*s4)/(n*s3-s1*s1); b1=(s1*s2-n*s4)/(s1*s1-n*s3); a= exp(a1); b=b1; cout<<"Y= ("<<a<<")e^("<<b<<")X"<<endl; getch(); }

www.facebook.com/Gurjinder.Romana

www.facebook.com/PTUGZS

40

## Que: Fit the Exponential Curve(Y=aebX) on given data

using method of least squares.
x y 1 145 3 137 5 129 7 114 9 100 11 97 13 84 15 76

OUTPUT:

Answer:

Solution is Y=157.282e-0.0473462X .

www.facebook.com/Gurjinder.Romana

www.facebook.com/PTUGZS