Documente Academic
Documente Profesional
Documente Cultură
Dear Students,
Follow the instructions while writing NA Lab Record
You have to write NA Lab programs in 200 pages record book.
Use only blue pen.
Programs should be written in right side on the ruled sheet.
Output should be written on the left side of the plain sheet.
Use always the fresh ruled sheet for next program.
Record should be written neatly.
Record should be covered with brown sheet.
Record should be written and submitted as said, other wise late
submission will be written on the Record certificate.
Slno
Program list
2
3
4
5
6
7
8
9
10
11
12
Date
gopalkrishnasir@yahoo.com
Page 1 of 24
%.5f\t %.5f
%.5f\n",i,fn(A),fn(B),C,fn(C));
if(YC==0)
{ A=C;
B=C;
}
else if( ( (YB >= 0) && (YC >=0) ) || ( (YB < 0) && (YC < 0) )
{
B=C;
YB=YC;
}
else
{
A=C;
YA=YC;
}
if ((B-A) < 0.00001) k=1;
}
printf("----------------------------------------------------------\n");
printf("The number of performed iterations is : %d\n",i-1);
gopalkrishnasir@yahoo.com
Page 2 of 24
OUTPUT
Please enter intervals A and B
1
2
The interval ranges from 1.0000 to 2.0000
Iteration F(a)
F(b)
MidPoint
F(c)
---------------------------------------------------------1
-2.00000 1.00000
1.50000 -1.62500
2
-1.62500 1.00000
1.75000 -0.64062
3
-0.64062 1.00000
1.87500 0.09180
4
-0.64062 0.09180
1.81250 -0.29565
5
-0.29565 0.09180
1.84375 -0.10733
6
-0.10733 0.09180
1.85938 -0.00913
7
-0.00913 0.09180
1.86719 0.04099
8
-0.00913 0.04099
1.86328 0.01585
9
-0.00913 0.01585
1.86133 0.00334
10
-0.00913 0.00334
1.86035 -0.00290
11
-0.00290 0.00334
1.86084 0.00022
12
-0.00290 0.00022
1.86060 -0.00134
13
-0.00134 0.00022
1.86072 -0.00056
14
-0.00056 0.00022
1.86078 -0.00017
15
-0.00017 0.00022
1.86081 0.00002
16
-0.00017 0.00002
1.86079 -0.00008
17
-0.00008 0.00002
1.86080 -0.00003
---------------------------------------------------------The number of performed iterations is : 17
The computed root of f(x)=1.8608
gopalkrishnasir@yahoo.com
Page 3 of 24
gopalkrishnasir@yahoo.com
Page 4 of 24
OUTPUT
Trapezoidal Rule
enter the value for lower and upper limits
0
1
enter the value for intervals
6
************************************
F0=1.0000 F6=0.5000
Iterations
X
F(x)
i=1
x=0.1667 F1=0.9730
i=2
x=0.3333 F2=0.9000
i=3
x=0.5000 F3=0.8000
i=4
x=0.6667 F4=0.6923
i=5
x=0.8333 F5=0.5902
*************************************
Final Solution= 0.7842
Trapezoidal Rule
=h/2[(sum of First and last ordinates) + 2(y1+y2+y3+y4+.yn-1)]
gopalkrishnasir@yahoo.com
Page 5 of 24
Program to find the integral of a function using Simpsons 1/3rd and 3/8th
rule using switch case.
#include<stdio.h>
#include<conio.h>
#include<math.h>
float fn(float);
void main()
{
int i,n,k;
float a,b,x,h,s1=0.0,s2=0.0,s3=0.0,s4=0.0,s5=0.0,s6=0.0,sim13,sim38;
clrscr();
printf("\nSimpsons Rule\n");
printf("\nenter the value for lower and upper limits\n");
scanf("%f%f",&a,&b);
printf("enter the value for intervals\n");
scanf("%d",&n);
h=(b-a)/n;
s1=fn(a)+fn(b);
printf("************************************\n");
printf("\tF0=%.4f F%d=%.4f\n",fn(a),n,fn(b));
x=a;
printf("\nIterations\t X \t F(x)\n");
for(i=1;i<n;i++)
{
k=i;
x=x+h;
s2=s2+fn(x);
printf(" i=%d\t
x=%.4f\t F%d=%.4f\n",i,x,i,fn(x));
if(i%2==0)
{
s3=s3+fn(x);
}
else
{
s4=s4+fn(x);
}
if(k%3==0)
{
s5=s5+fn(x);
}
else
{
s6=s6+fn(x);
}
}
printf("*************************************\n");
printf("\n\tSimpsons 1/3 rule ");
sim13=(h/3.0)*((s1)+(2.0*s3)+(4.0*s4));
printf("Final Solution= %.4f",sim13);
printf("\n\tSimpsons 3/8 rule ");
sim38=((3.0*h)/8.0)*((s1)+(2.0*s5)+(3.0*s6));
gopalkrishnasir@yahoo.com
Page 6 of 24
OUTPUT
Simpsons Rule
enter the value for lower and upper limits
0
1
enter the value for intervals
6
************************************
F0=1.0000 F6=0.5000
Iterations
X
F(x)
i=1
x=0.1667 F1=0.9730
i=2
x=0.3333 F2=0.9000
i=3
x=0.5000 F3=0.8000
i=4
x=0.6667 F4=0.6923
i=5
x=0.8333 F5=0.5902
*************************************
Simpsons 1/3 rule Final Solution= 0.7854
Simpsons 3/8 rule Final Solution= 0.7854
Simpsons1/3 Rule
=h/3[(sum of First and Last ordinates) + 4(sum of Odd ordinates) +
4(Sum of Even ordinates)]
Simpsons3/8 Rule
=3h/8[(sum of First and Last ordinates) + 3(sum of Odd ordinates which
are not multiply of 3) + 2(Sum of ordinates which are multiply by of 3)]
gopalkrishnasir@yahoo.com
Page 7 of 24
OUTPUT
Secant Method
Enter the value of roots lies between
0
1
Its
1
2
3
4
5
X0
0.0000
1.0000
0.2500
0.1864
0.2017
X1
X2
1.0000 0.2500
0.2500 0.1864
0.1864 0.2017
0.2017 0.2016
0.2016 0.2016
gopalkrishnasir@yahoo.com
Page 8 of 24
gopalkrishnasir@yahoo.com
Page 9 of 24
10
OUTPUT
Newtons Raphson Method
The Roots lies between 2 and 3
The Approximate roots is 2.50
Iteration
F(I)
---------------------1
2.302
2
2.279
3
2.279
gopalkrishnasir@yahoo.com
Page 10 of 24
11
gopalkrishnasir@yahoo.com
Page 11 of 24
12
OUTPUT
Gauss Elimination Method
Enter the coefficients
1
2
3
2
-3
1
3
1
-2
Enter the constants:
10
1
9
The
1
0
0
matrix becomes
2 3 10
-7 -5 -19
0 -7 -7
The solution is
X=3.00, Y=2.00, Z=1.00
gopalkrishnasir@yahoo.com
Page 12 of 24
13
OUTPUT
Enter Initial Value of x,y
gopalkrishnasir@yahoo.com
Page 13 of 24
14
0
1
Enter Interval (h)
.2
Enter Final Value(xf)
.4
*******************************
Runge-Kutta 4th Order Method(First Order ODE)
Slno.
X
Y
0
0.0000 1.0000
1
0.2000 1.1679
2
0.4000 1.2902
gopalkrishnasir@yahoo.com
Page 14 of 24
15
gopalkrishnasir@yahoo.com
Page 15 of 24
16
znew=z+(m1+2*m2+2*m3+m4)/6.0;
return;
}
OUTPUT
Enter Initial Value of x,y,z
0
0
1
Enter Interval (h)
.1
Enter Final Value(xf)
.2
*******************************
Runge-Kutta 4th Order Method(Second Order ODE)
Slno.
X
Y
Z
0
0.0000 0.0000
1
0.1000 0.1052
2
0.2000 0.2215
gopalkrishnasir@yahoo.com
1.0000
1.1053
1.2229
Page 16 of 24
17
OUTPUT
Enter Lower and Upper Limit
gopalkrishnasir@yahoo.com
Page 17 of 24
18
0
6
Enter Epsilon
.001
ADAPTIVE SIMPSONS'S
a=0.00 b=6.00 s1=2.143
a=0.00 b=3.00 s1=1.425
a=0.00 b=1.50 s1=0.921
a=0.00 b=0.75 s1=0.560
a=0.75 b=1.50 s1=0.357
a=1.50 b=3.00 s1=0.470
a=3.00 b=6.00 s1=0.560
METHOD
s2=1.985
s2=1.392
s2=0.917
s2=0.560
s2=0.357
s2=0.470
s2=0.560
gopalkrishnasir@yahoo.com
Page 18 of 24
19
gopalkrishnasir@yahoo.com
Page 19 of 24
20
if(big<=EPS)
{
printf("\n\t Converges to a solution");
for(i=1;i<=n;i++)
printf(" x[%d]=%5.3f ",i,x[i]);
getch();
return;
}
for(i=1;i<=n;i++)
oldx[i]=x[i];
}
printf("\n\nDoes not converge in 10 iterations");
return;
}
OutPut
Enter the order of Matrix
3
Enter coefficients of matrix
10
2
1
9
1
10
-1
-22
-2
3
10
22
Gauss-Seidal Iterative Method
Given matrix
10.000 2.000 1.000 9.000
1.000 10.000 -1.000 -22.000
-2.000 3.000 10.000 22.000
iter x1
x2
x3
1 0.900 -2.290 3.067
2 1.051 -1.998 3.010
3 0.999 -1.999 2.999
4 1.000 -2.000 3.000
Converges to a solution x[1]=1.000 x[2]=-2.000
gopalkrishnasir@yahoo.com
x[3]=3.000
Page 20 of 24
21
fun1(float,float);
fun2(float,float);
fun1x(float,float);
fun1y(float,float);
fun2x(float,float);
fun2y(float,float);
void main()
{
float x0,y0,x1,y1,xprev,yprev,eps,Dx,Dy,demon;
int iter=0;
float f,g,f1,f2,g1,g2;
clrscr();
printf("\n Enter intial value x0,y0\n");
scanf("%f%f",&x0,&y0);
printf("Enter Epsilon\n");
scanf("%f",&eps);
printf("\n\t Newton raphson method for non-linear systems\n");
printf("\n\t Slno. x0 y0
Dx
Dy
x1
y1 \n");
do
{
iter++;
xprev=x0;yprev=y0;
f=fun1(x0,y0);
g=fun2(x0,y0);
f1=fun1x(x0,y0);
f2=fun1y(x0,y0);
g1=fun2x(x0,y0);
g2=fun2y(x0,y0);
demon=(f1*g2-f2*g1);
Dx=-(f*g2-g*f2)/demon;
Dy=-(g*f1-f*g1)/demon;
x1=x0+Dx;
y1=y0+Dy;
printf("\n\t %d %6.2f %6.2f %6.2f %6.2f %6.3f
%6.3f",iter,x0,y0,Dx,Dy,x1,y1);
x0=x1;
y0=y1;
}
while(fabs(x1-xprev)>eps&&fabs(y1-yprev)>eps);
printf("\n\n\t\tRoots are x=%7.4f y=%7.4f",x0,y0);
getch();
return;
}
float fun1(float x,float y)
{
gopalkrishnasir@yahoo.com
Page 21 of 24
22
return(x*x-y*y+7);
}
float fun2(float x,float y)
{
return(x-x*y+9);
}
float fun1x(float x,float y)
{
return(2*x);
}
float fun1y(float x,float y)
{
return(-2*y);
}
float fun2x(float x,float y)
{
return(-y);
}
float fun2y(float x,float y)
{
return(-x);
}
OUTPUT
Enter intial value x0,y0
4
5
Enter Epsilon
.001
Newton raphson method for non-linear systems
Slno. x0
1
2
3
4
5
4.00
3.24
3.04
3.01
3.00
y0
Dx
Dy
5.00
4.20
4.03
4.01
4.00
-0.76
-0.20
-0.04
-0.01
-0.00
-0.80
-0.16
-0.03
-0.00
-0.00
x1
y1
3.244
3.045
3.007
3.001
3.000
4.195
4.032
4.005
4.001
4.000
gopalkrishnasir@yahoo.com
Page 22 of 24
23
Write a program to solve the system of equations Ax = b using Jacobi Iteration method.
#include<stdio.h>
#include<conio.h>
#include<math.h>
#define EPS 1.0e-02
void main()
{
float a[5][5],x[5],oldx[5],err,big,sum;
int i,j,k,n,iter,maxit;
clrscr();
printf("\nEnter the no. of unknowns and no. of iterations n:");
scanf("%d",&n);
printf("\n Enter the coefficient of the matrix:\n");
for(i=1;i<=n;i++)
{
for(j=1;j<=n+1;j++)
{
scanf("%f",&a[i][j]);
}
}
for(i=1;i<=n;i++)
oldx[i]=0.0;
printf("\t\t Jacobi's Iterative method");
printf("\n\t\tGiven matrix");
for(i=1;i<=n;i++)
{
printf("\n\t\t");
for(j=1;j<=n+1;j++)
{
printf("%2.2f ",a[i][j]);
}
}
printf("\n\tSlno X\t Y\t
Z");
for(iter=1;iter<=10;iter++)
{
big=0.0;
for(i=1;i<=n;i++)
{
sum=0.0;
for(j=1;j<=n+1;j++)
{
if(i!=j)
sum=sum+a[i][j]*oldx[j];
}
x[i]=(a[i][n+1]-sum)/a[i][i];
err=fabs((x[i]-oldx[i]));
if(err>big)
big=err;
}
gopalkrishnasir@yahoo.com
Page 23 of 24
24
printf("\n\t %d",iter);
for(i=1;i<=n;i++)
printf(" %8.2f ",x[i]);
if(big<=EPS)
{
printf("\n Converges to a solution: ");
for(i=1;i<=n;i++)
printf("X[%d]=%2.2f ",i,x[i]);
getch();
return;
}
for(k=1;k<=n;k++)
oldx[k]=x[k];
}
printf("\n\n Does not converge in 10 iterations");
getch();
return;
}
OUTPUT
Enter the no. of unknowns and no. of iterations n:
3
Enter the coefficient of the matrix:
10
1
1
24
-1
20
1
21
1
-2
100
300
Jacobi's Iterative method
Given matrix
10.00 1.00 1.00 24.00
-1.00 20.00 1.00 21.00
1.00 -2.00 100.00 300.00
Slno X
Y
Z
1
2.39
1.05
2.99
2
1.99
1.02
2.99
3
1.99
1.00
2.99
4
1.99
1.00
2.99
Converges to a solution: X[1]=1.99 X[2]=1.00 X[3]=2.99
gopalkrishnasir@yahoo.com
Page 24 of 24