all in one maths lab
Post on 14-Apr-2018
217 Views
Preview:
TRANSCRIPT
-
7/30/2019 All in One Maths Lab
1/27
@ [P]-[A__B]-[] Page 1
C/C++
PROGRAMMING
FOR
NUMERICAL LAB
@[P]-[A__B]-[]
-
7/30/2019 All in One Maths Lab
2/27
@ [P]-[A__B]-[] Page 2
CONTENTS
1. BISECTION METHOD
2. SECANT METHOD
3. EULER METHOD
4. TRAPEZOIDAL METHOD
5. REGULA FALSI METHOD
6. FIXED POINT METHOD
7. LAGRANGES INTERPOLATION
8. RUNGE-KUTTA METHOD
9. SIMPSONS 1/3 RULE
10. SIMPSONS 3/8 RULE
11. GAUSS-SEIDEL METHOD12. NEWTON-RAPHSON METHOD
13. NEWTONS DIVIDED DIFFERENCE METHOD
14. HUENS METHOD
-
7/30/2019 All in One Maths Lab
3/27
@ [P]-[A__B]-[] Page 3
BISECTION METHOD
AIM: To find solution of x2
-4x+3 by BISECTION METHOD.
ALGORITHM:
1. Start2. Define the function f(x)3. Read initial values say x1 & x2.
4. if (((f(x1)*f(x2))
-
7/30/2019 All in One Maths Lab
4/27
@ [P]-[A__B]-[] Page 4
cout
-
7/30/2019 All in One Maths Lab
5/27
@ [P]-[A__B]-[] Page 5
SECANT METHOD
AIM:To find solution of equation x2-5x+4 by SECANT METHOD.
ALGORITHM:
1. Start
2. Function f(x) is declared.
3. Range in which the root lies is determined.
4. Then consecutive values are calculated:
Xn+1=Xn-(f(Xn)(Xn+1-Xn)/(f(Xn+1)-f(Xn))
Till((Xn+1-Xn)>0.000001))
5. Print the root.8. Stop
PROGRAM:
#include
main()
{
float a,b,c,f,g,s,h;
printf("enter two value :");
scanf("%f %f",&a,&s);f=(a*a)-(5*a)+4;
g=s*s-5*s+4;
b=a-((f*(a-s))/(f-g));
printf("\nITERATION SOLUTION");
printf(" \n\n 1 \t\t %f \n",b);
float e;
e=0.1;
int i=1;
while(e>0.001){
f=(a*a)-(5*a)+4;
h=(b*b)-(5*b)+4;
c=b-((h*(b-a))/(h-f));
if(h==f)
{
printf("\t 1 \t\t %f",b);
}
else
printf("\n %d \t\t %f \n",i+1,c);
if(c>b)e=(c-b);
-
7/30/2019 All in One Maths Lab
6/27
@ [P]-[A__B]-[] Page 6
else
e=(b-c);
a=b;
b=c;
i++;
}printf("\n Solution after %d iterations is %f \n",i,c);
}
SAMPLE OUTPUT:
Enter two values: 5 6
ITERATION SOLUTION
1 4.333333
2 4.0769233 4.007519
4 4.000187
5 4.000000
Solution after 5 iterations is 4.000000
EULER METHOD
AIM: To find value of y at x=0.3 when given differential equation is y= (x+y).
ALGORITHM:
1.INPUT:
Initial values x0 ,y0, stepsize h , number of steps N
2.OUTPUT:
Approximation y(n+1) to the solution y(x(n+1)) at
x(n+1) = x0+ (n+1)h where n=0,1,2...N-1
3.For n=0,1,2...N-1 do
4.y=y+h*(x+y)
5.x=x+h
6.OUTPUT x(n+1) , y(n+1)
7.End
8.stop
PROGRAM:
-
7/30/2019 All in One Maths Lab
7/27
@ [P]-[A__B]-[] Page 7
#include
float f(float x,float y)
{
float z;z=x+y;
return z;
}
int main()
{
float h,x,y;
int i,n;
printf("enter values of x , y , h,n \n ");
scanf(" %f %f %f %d", &x ,&y ,&h ,&n);
for (i=0;i
-
7/30/2019 All in One Maths Lab
8/27
@ [P]-[A__B]-[] Page 8
x5 0.250000
y5 1.302563
x6 0.300000
y6 1.380191
TRAPEZOIDAL METHOD
AIM:
To find the solution of integration of x3
from 0 to 1.
ALGORITHM:
1.Input: enter limits a,b,n=1
2.compute:
3.h=(b-a)/n
4.while(x0.0001)
-
7/30/2019 All in One Maths Lab
9/27
@ [P]-[A__B]-[] Page 9
{
h=(b-a)/n;
m=0; x=a+h;
while(xy)
e=z-y;
else
e=y-z;
z=y;
n++;}
printf(\n Solution of integration is : %f,y);
}
SAMPLE OUTPUT:
Enter the limits : 0 1
Iteration Solution
1 0.3125002 0.2777783 0.2656254 0.2600005 0.2569446 0.2551027 0.2539068 0.2530869 0.25250010 0.25206611 0.33506912 0.25147913 0.25127614 0.25111115 0.25097716 0.25086517 0.250772
Solution of integration is : 0.250772
-
7/30/2019 All in One Maths Lab
10/27
@ [P]-[A__B]-[] Page 10
REGULA FALSI METHOD
AIM: To find solution of the function : x2-5x+4 by REGULA FALSI METHOD.
ALGORITHM:
1. Start.
2. Set values of x1 & x2.
3. A function f is defined where functional values are
calculated.
4. Repeat the steps until you obtain a sign change.
5. If((f(x1)*f(x2))0.000001
7. z=(x*f(y)-y*f(x))/(f(y)-f(x))
8. If (f(z)*f(y))0)
printf("invalid input");else if(f==0 && p==0)
-
7/30/2019 All in One Maths Lab
11/27
@ [P]-[A__B]-[] Page 11
printf("solution is %f",a);
else if(f==0 && q==0)
printf("\n solution is %f",b);
else
{
while(r>0.000001 || r
-
7/30/2019 All in One Maths Lab
12/27
@ [P]-[A__B]-[] Page 12
17 1.003042
18 1.001826
19 1.001096
20 1.000658
21 1.000395
22 1.00023723 1.000142
24 1.000085
25 1.000051
26 1.000031
27 1.000018
28 1.000011
29 1.000007
30 1.000004
31 1.000002
32 1.000001
33 1.00000134 1.000000
Solution after 34 iterations is 1.000000
FIXED POINT METHOD
AIM:To find the solution of equation : 1/(x2+1) by fixed point method.
ALGORITHM:
1.Read the first value
2.Calculate z(x) as (1/(x*x)+1)
3.while (g-z)>0.0001
4.g=(1/(z*z)+1)
5.z=g
6.End of while loop
PROGRAM:
#includemain()
{
int i=0;
float x,y,g,z,e=0.1;
printf("\n Enter the first value");
scanf("%f",&x);
z=(1/((x*x)+1));
while(e>0.0001)
{
g=1/((z*z)+1);printf("\n%d iteration %f",(i+1),g);
-
7/30/2019 All in One Maths Lab
13/27
@ [P]-[A__B]-[] Page 13
if(g>z)
e=g-z;
else
e=z-g;
z=g;
i++;}
printf("\n Solution of equation is %f",g);
}
SAMPLE OUTPUT:
Enter the first value : 0.5
1 iteration 0.609756
2 iteration 0.728968
3 iteration 0.653000
4 iteration 0.701061
5 iteration 0.6704726 iteration 0.689878
7 iteration 0.677538
8 iteration 0.685374
9 iteration 0.680394
10 iteration 0.683557
11 iteration 0.681547
12 iteration 0.682824
13 iteration 0.682013
14 iteration 0.682528
15 iteration 0.682201
16 iteration 0.68240917 iteration 0.682276
18 iteration 0.682360
Solution of equation is 0.682360
LAGRANGES INTERPOLATION
AIM-
To determine value at a given point using Lagranges interpoation when values are given as f(0)=1,
f(1)=3, f(3) = 13,f(8)=123
ALGORITHM-
1. Read the number of points(n),given points and its functional values.2. Declare d,p,sum3. For i=0 to n-1
-
7/30/2019 All in One Maths Lab
14/27
@ [P]-[A__B]-[] Page 14
4. p=15. for j=0 to n-16. if (i!=j)7. d= (a-x[j])/(x[i]-x[j])8. p=p*d9. End of if statement10. End of inner for loop11.P1=p*f[i]12.Sum=sum+p113.End of for loop14. Print the result
PROGRAM:
#include
int main()
{
int n;
float a,x[10],f[10];
couta;
coutn;
for(int k=0;k
-
7/30/2019 All in One Maths Lab
15/27
@ [P]-[A__B]-[] Page 15
}
SAMPLE OUTPUT:
Enter given point : 5Enter number of points : 4
Enter value of x0 : 0
Enter functional value : 1
Enter value of x1 : 1
Enter functional value : 3
Enter value of x2 : 3
Enter functional value : 13
Enter value of x3 : 8
Enter functional value : 123
Value at given point : 38.14286
RUNGE-KUTTA METHOD
AIM- To solve (dy/dx=(y2-x
2)/(y
2+x
2)) with y(0) =1 at 0.2 and 0.4
ALGORITHM-
1.Define given function f(x,y)
2.Read x0,y0,h,n.
3. For i=0 to (n-1)
4.K1=h*(f(x[i],y[i])
5.K2=h*(f(x[i]+(h/2), y[i]+(k1/2)))
6.K3=h*(f(x[i]+(h/2),y[i]+(k2/2))
7.K4=h*f(x[i]+h,y[i]+k3)
8.y[i+1]=y[i]+(k1+k2+k3+k4)/6
9.Print x[i],y[i]
10.Next i
11.End
PROGRAM-
9
#include
#include
float f(float x,float y)
{
return(((y*y)-(x*x))/((y*y)+(x*x)));
}
-
7/30/2019 All in One Maths Lab
16/27
@ [P]-[A__B]-[] Page 16
int main()
{
float x[10],y[10],h,k1,k2,k3,k4;
int n,i;
printf("enter x.,y.,h");
scanf("%f%f%f",&x[0],&y[0],&h);printf("Enter value of n");
scanf("%d",&n);
printf(" \n x0 and y0 : %f %f ",x[0],y[0]);
for( i=0;i
-
7/30/2019 All in One Maths Lab
17/27
@ [P]-[A__B]-[] Page 17
printf("enter x.,y.,h");
scanf("%f%f%f",&x[0],&y[0],&h);
printf("Enter value of n");
scanf("%d",&n);
printf("x0 and y0 :%f %f ",x[0],y[0]);
for( i=0;i
-
7/30/2019 All in One Maths Lab
18/27
@ [P]-[A__B]-[] Page 18
#include
main()
{
int n,i;
i=1;float a,y,m,s,b,h,f,g,z,x,k,e;
printf("\n Input the limits");
scanf("%f %f",&a,&b);
f=a*a*a; g=b*b*b;
n=2;
h=(b-a)/n;
m=h*h*h;
z=h*(f+g+(4*m))/3;
n=n+2; e=0.1;
printf("\n ITERATION SOLUTION");
while(e>0.001){
h=(b-a)/n;
x=a+h;
s=0;int c=1;
while(xy)
e=z-y;
else
e=y-z;
z=y; n=n+2;
}
printf(\n Solution of integration is :%f,y);
}
SAMPLE OUTPUT:
Input the limits: 8.2 9.0
ITERATION SOLUTION
1 607.145630
2 574.745667
3 509.945984
4 548.825623
-
7/30/2019 All in One Maths Lab
19/27
@ [P]-[A__B]-[] Page 19
5 542.345642
6 509.946136
7 509.945923
Solution of integration is : 509.945923
SIMPSONS 3/8 RULE
AIM: To find the solution of integration of sqrt(1-0.692sin2(m)) from 0 to 6.
ALGORITHM:
1.INPUT:read lmits-a,b
2.h=(b-a)/n
3.for i=1 to n
4.x[i]=a+ih
5.y[i]=f(x[i])
6.if(i%3=0)
S2+=y[i]
7.else s1+=y[i]
8.s=3h/8(f(a)+f(b)+3s1+2s2)
9.print s.10.end.
PROGRAM:
#include
#include
float f(float m);
main()
{
float a,b,n;float h,s1=0,s2=0,s=0;
b=M_PI_2;
printf("\n Enter the limits : ");
scanf("%f %f",&a,&n);
float x[6],y[6];
x[0]=a;x[n]=b;
h=(b-a)/n;
for(int i=1;i
-
7/30/2019 All in One Maths Lab
20/27
@ [P]-[A__B]-[] Page 20
if(i%3==0)
s2+=y[i];
else
s1+=y[i];
}
s=(3*h/8)*((f(a)+f(b)+(3*s1)+(3*s2)));
printf("\n The value of integration is : %f",s);
}
float f(float m)
{
float p;
p=sqrt(1-(0.162*sin(m)*sin(m)));
return p;
}
SAMPLE OUTPUT:
Enter the limits : 0 6
The value of integration is : 1.599218
GAUSS SEIDEL METHOD
AIM:
To find the solution of system of equations by GAUSS-SEIDEL METHOD.
10w-2x-y-z=3
-2w+10x-y-z=15
-w-x+10y-2z=27
-w-x-2y+10z=-9
ALGORITHM:
1.INPUT: Input the equations s1 , s2 , s3 , s4..sn and no.of iterations
set values of the solutions to zero
2.OUTPUT: Approximate solutions x1 , x2 ,x3 ,xn will be out
3. For n = 0 , 1 , 2..N-1
4. assign values of x1 , x2 , x3.xn to k1 , k2 ,.kn
X1 = s1 ( x2 , x3 ,x4 ,..xn )
-
7/30/2019 All in One Maths Lab
21/27
@ [P]-[A__B]-[] Page 21
X2= s2 ( x1 , x3 ,x4 .xn )
.
.
.
.
.
Xn = sn ( x1 ,x2,x3 ,.xn-1)
5.Print the approximate value of x1 , x2...xn
6.End
7.stop
PROGRAM:
#include
int main( ){
float x[10][10],k0,k1,k2,k3 ;
int i,j,k;
x[0][0]=0;x[1][0]=0;x[2][0]=0;x[3][0]=0;
printf (" iteration values are \n ") ;
for(i=0;i
-
7/30/2019 All in One Maths Lab
22/27
@ [P]-[A__B]-[] Page 22
w x y z
0.300000 1.500000 2.700000 -0.900000
0.780000 1.740000 2.700000 -0.180000
0.900000 1.908000 2.916000 -0.108000
0.962400 1.960800 2.959200 -0.0360000.984480 1.984800 2.985120 -0.015840
0.993888 1.993824 2.993760 -0.006048
NEWTON RAPHSON METHODAIM:
To find the solution of the function x2-4x+3 by NEWTON-RAPHSON METHOD.
ALGORITHM:
1.The function f(x) is declared.
2.The derivative of the function is found out and stored in another variable(say f1(x)).
3.If f1(xn)=0,output fails & exit
4. Else xn+1=xn-(f(xn)/f1(xn))
5.The process continues till |xn+1-xn|0.000001)
{
c=b-((b*b)-(4*b)+3)/((2*a)-4);
if(c==2.0){printf("Solution is %f ",b);}
printf("\n %d \t %f",i+2,c);if(c>b)
-
7/30/2019 All in One Maths Lab
23/27
@ [P]-[A__B]-[] Page 23
e=(c-b);
else
e=(b-c);
b=c;
i++;
}
printf("\n\nSolution of function is %f",c);
}
SAMPLE OUTPUT:
Enter the first value : 0.5
ITERATION SOLUTION1. 0.916667
2. 0.974537
3. 0.991728
4. 0.997266
5. 0.999091
6. 0.999697
7. 0.999899
8. 0.999966
9. 0.999989
10. 0.999996
11. 0.99999912. 1.000000
Solution of the function is 1.000000
NEWTONS DIVIDED
DIFFERENCE METHOD
AIM :
To find value of a function at a given point using NEWTONS DIVIDED DIFFERENCE METHOD Values given as f(5)=150, f(7)=392, f(11)=1452 , f(13)=2366, f(17)=5202
ALGORITHM:
1. Define function as given.
-
7/30/2019 All in One Maths Lab
24/27
@ [P]-[A__B]-[] Page 24
2. Define f[x0 x1] as f(x1)-f(x0)/(x1-x0)
3. Define f[x0 x1 x2] as (f[x1 x2]-f[x0 x1])/(x2-x0)
4. Define f[x0 x1 x2 x3] as (f[x1 x2 x3]-f[x0 x1 x2])/(x3-x0)
5. Define f[x0 x1 x2 x3 x4] as (f[x1 x2 x3 x4]-f[x0 x1 x2 x3])/(x4-x0)
6. Define polynomial p(a) in terms of f[x0 x1],f[x0 x1 x2 ],f[x0 x1 x2 x3],f[x0 x1 x2 x3 x4]
7. In main, read the 5 values8. Print result.
PROGRAM:
#include
float x[20],f[20];
float func(int low,int high)
{
if(low==high-1)return (f[high]-f[low])/(x[high]-x[low]);
else
return (func(low+1,high)-func(low,high-1))/(x[high]-x[low]);
}
float calcpx(float y,int n)
{
float px;
px=f[0];
float fact=1;
int j;
for(int i=1;i
-
7/30/2019 All in One Maths Lab
25/27
@ [P]-[A__B]-[] Page 25
cin>>n;
for(int i=0;i
-
7/30/2019 All in One Maths Lab
26/27
@ [P]-[A__B]-[] Page 26
ALGORITM:
1.INPUT: Initial values x0 , y0 , step size h , number of steps N
2.OUTPUT: Approximation y(n+1) to the solution y(x(n+1)) at
x ( n + 1 ) = x0 + ( n + 1 ) h where n = 0 ,1 ,2 ,..N - 1
3.for n = 0 , 1 , 2 , ..N - 1 do
4.y = y+ h * ( x + y )
5.x = x + h
6. OUTPUT x ( n + 1 ) , y ( n + 1 )
7.End
8.stop
PROGRAM:
#include
#include
float f(float x,float y)
{
float z ;
z=x+y;
return z;
}
int main()
{
float x0,y0,y1,h,y11,e;
int i,n;
printf (" enter values of x0 , y0 , h , n \n ");
scanf ("%f%f%f%d",&x0,&y0,&h,&n);
printf(" x y \n ");
for (i=0;i0)
e=(y1-y11);
else
e=(y11-y1);
while (e>0.001)
{
-
7/30/2019 All in One Maths Lab
27/27
y11=y1 ;
y1=y0+((h/2)*(f(x0,y0)+f(x0,y11)));
printf(" %f \n ",y1);
if((y1-y11)>0)
e=(y1-y11);
else
e=(y11-y1);
}
y0=y1;
x0=x0+h;
}
return 0;
}
SAMPLE OUTPUT:
Enter values of x0 , y0 , h , n : 0 1 0.05 6
Iteration values of 0.050000 are
1.051250
1.051281
Iteration values of 0.100000 are
1.107722
1.107756
Iteration values of 0.150000 are
1.169654
1.169692
Iteration values of 0.200000 are
1.237326
1.237367
Iteration values of 0.250000 are1.311032
1.311077
Iteration values of 0.300000 are
1.391082
1.391131
****************************************
top related