Sunteți pe pagina 1din 24

BCA306P -NUMERICAL ANALYSIS and LINEAR PROGRAMMING Lab

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

Write a program to find the roots of an equation f (x) = 0 using


Bisection method
Write a program to find the simple/multiple roots of f (x) = 0 using
Newton Raphson method.
Write a program to find the roots of system of non-linear algebraic
equations using Newtons method.
Write a program to find the roots of f(x) = 0 using Secant method.

2
3
4
5
6
7
8
9
10
11
12

Date

Write a program to find the integral of a function using Trapezoidal


rule.
Write a program to find the integral of a function using Simpsons
1/3rd and 3/8th rule using switch case.
Write a program to find the integral of a function using adaptive
Simpson method
Write a program to solve the system of equations Ax = b using Gauss
elimination method.
Write a program to solve the system of equations Ax = b using Jacobi
Iteration method.
Write a program to solve the system of equations Ax = b using
Gauss-Seidel method
Write a program to solve first order ordinary differential equations (
initial value problem) using Runge-Kutta fourth order method
Write a program to solve second order ordinary differential
equations ( initial value problem) using Runge-Kutta fourth order
method

gopalkrishnasir@yahoo.com

Page 1 of 24

BCA306P -NUMERICAL ANALYSIS and LINEAR PROGRAMMING Lab

Program to find the roots of an equation f (x) = 0 using Bisection method


#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<math.h>
float fn(float);
void main()
{
float A,B,C,YA,YB,YC;
int i,k=0;
clrscr();
printf("Please enter intervals A and B\n");
scanf("%f%f",&A,&B);
printf("The interval ranges from %.4f to %.4f\n", A,B);
YA=fn(A);
YB=fn(B);
if( ( (YA >= 0) && (YB >=0) ) || ( (YA < 0) && (YB < 0) ) )
{
printf("The values fn(A) and fn(B) do not differ in sign.\n");
getch();
exit(0);
}
printf("Iteration F(a)
F(b)\t MidPoint
F(c) \n");
printf("----------------------------------------------------------\n");
for(i=1;i<=20;i++)
{
if(k==1) break;
C=(A+B)/2;
YC=fn(C);
printf(" %d\t %.5f

%.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

BCA306P -NUMERICAL ANALYSIS and LINEAR PROGRAMMING Lab

printf("The computed root of f(x)=%.4f \n",C);


getch();
}
float fn(float x)
{
return((x*x*x)-4*x+1);
}

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

BCA306P -NUMERICAL ANALYSIS and LINEAR PROGRAMMING Lab

Program to find the integral of a function using Trapezoidal rule.


#include<stdio.h>
#include<conio.h>
#include<math.h>
float fn(float);
void main()
{
int i,n;
float a,b,x,h,s1=0.0,s2=0.0,trip;
clrscr();
printf("\nTrapezoidal 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++)
{
x=x+h;
s2=s2+fn(x);
printf(" i=%d\t
x=%.4f\t F%d=%.4f\n",i,x,i,fn(x));
}
trip=(h/2.0)*((s1)+(2.0*s2));
printf("*************************************\n");
printf("\nFinal Solution= %.4f",trip);
getch();
}
float fn(float z)
{
float fx;
fx=(1.0/(1.0+z*z));
return(fx);
}

gopalkrishnasir@yahoo.com

Page 4 of 24

BCA306P -NUMERICAL ANALYSIS and LINEAR PROGRAMMING Lab

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

BCA306P -NUMERICAL ANALYSIS and LINEAR PROGRAMMING Lab

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

BCA306P -NUMERICAL ANALYSIS and LINEAR PROGRAMMING Lab

printf("Final Solution= %.4f",sim38);


getch();
}
float fn(float z)
{
float fx;
fx=(1.0/(1.0+z*z));
return(fx);
}

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

BCA306P -NUMERICAL ANALYSIS and LINEAR PROGRAMMING Lab

Program to find the roots of f(x) = 0 using Secant method


#include<stdio.h>
#include<math.h>
double f(double x)
{
return(pow(x,3)-5*x+1);
}
double SecantMethod(double xn_1, double xn,double e,int m)
{
int n;
double d;
printf("\nIts X0
X1
X2 \n");
for (n = 1; n <= m; n++)
{
d =(xn - xn_1) / (f(xn) - f(xn_1)) * f(xn);
if (fabs(d) < e)
return xn;
printf("%d %.4f %.4f %.4f\n",n,xn_1,xn,xn-d);
xn_1 = xn;
xn = xn-d;
}
return xn;
}
void main()
{
float a,b;
clrscr();
printf("Secant Method\n");
printf("\nEnter the value of roots lies between\n");
scanf("%f%f",&a,&b);
printf("\nFinal Solution = %0.4f\n", SecantMethod(a, b,5E-11,10));
getch();
}

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

Final Solution = 0.2016

gopalkrishnasir@yahoo.com

Page 8 of 24

BCA306P -NUMERICAL ANALYSIS and LINEAR PROGRAMMING Lab

Program to find the simple/multiple roots of f (x) = 0 using Newton


Raphson method.
#include<stdio.h>
#include<conio.h>
#include<math.h>
float fn(float);
float f1n(float);
void main()
{
float A,B,C,D,YA,Y1A;
int i;
clrscr();
printf("\n Newtons Raphson Method\n");
for(i=0;i<=5;i++)
{
C=fn(i);
D=fn(i+1);
if( ( (C >= 0) && (D <=0)) || ((C < 0) && (D > 0) ))
{
break;
}
}
printf("\n The Roots lies between %d and %d\n",i,i+1);
A=(float)(i+(i+1))/2;
printf("\n The Approximate roots is %.2f\n",A);
printf("\n Iteration\tF(I)\n");
printf(" ----------------------");
for(i=1;i<=20;i++)
{
B=A-(fn(A)/f1n(A));
if(A==B)
{
getch();
exit(0);
}
else
{
A=B;
printf("\n %d\t\t%.3f",i,A);
}
}
getch();
}
float fn(float x)
{
return((x*x*x)-3*x-5);
}
float f1n(float x)
{
return((3*pow(x,2))-3);
}

gopalkrishnasir@yahoo.com

Page 9 of 24

BCA306P -NUMERICAL ANALYSIS and LINEAR PROGRAMMING Lab

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

BCA306P -NUMERICAL ANALYSIS and LINEAR PROGRAMMING Lab

11

Program to solve the system of equations Ax = b using Gauss elimination


method.
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
int i,j;
float a[3][4],b[3][4],c[4][4];
float x,y,z,p,q,r;
clrscr();
printf("Gauss Elimination Method \n");
printf("\nEnter the coefficients\n ");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
scanf("%f",&a[i][j]);
}
printf("\nEnter the constants:\n");
for(i=0;i<3;i++)
{
scanf("%f",&a[i][3]);
}
if(a[0][0]!=0.0)
{
p=a[1][0];
q=a[0][0];
r=a[2][0];
for(j=0;j<=3;j++)
{
b[0][j]=-(p/q)*a[0][j];
a[1][j]+=b[0][j];
c[0][j]=-(r/q)*a[0][j];
a[2][j]+=c[0][j];
}
p=a[2][1];
q=a[1][1];
for(j=0;j<=3;j++)
{
b[1][j]=-(p/q)*a[1][j];
a[2][j]+=b[1][j];
}
printf("\nThe matrix becomes\n");
for(i=0;i<3;i++)
{
for(j=0;j<4;j++)
{
printf("%4.f",a[i][j]);
}
printf("\n");
}
z=a[2][3]/a[2][2];
y=(a[1][3]-a[1][2]*z)/a[1][1];
x=(a[0][3]-a[0][2]*z-a[0][1]*y)/a[0][0];

gopalkrishnasir@yahoo.com

Page 11 of 24

BCA306P -NUMERICAL ANALYSIS and LINEAR PROGRAMMING Lab

12

printf("The solution is");


printf("\nX=%.2f, Y=%.2f, Z=%.2f",x,y,z);
}
else
{
printf("\nThe first cofficient must not be zero");
}
getch();
}

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

BCA306P -NUMERICAL ANALYSIS and LINEAR PROGRAMMING Lab

13

Program to find Solution to First-order ODE using Runge-Kutta 4th Order


Method
#include <stdio.h>
#include <conio.h>
#include<math.h>
float fun(float,float);
float rk4(float,float,float);
void main()
{
float h,x,y,xf;
int iter=0;
clrscr();
printf("\n Enter Initial Value of x,y\n");
scanf("%f%f",&x,&y);
printf("\n Enter Interval (h)\n");
scanf("%f",&h);
printf("\n Enter Final Value(xf)\n");
scanf("%f",&xf);
printf("\n\t\t*******************************\n");
printf("\t\t Runge-Kutta 4th Order Method(First Order ODE)\n");
printf("\n\t\t Slno.\t X\t Y");
printf("\n\t\t %2d\t %2.4f %2.4f",iter,x,y);
while(x<xf)
{
iter++;
y=rk4(h,x,y);
x=x+h;
printf("\n\t\t %2d\t %2.4f %2.4f",iter,x,y);
}
getch();
return;
}
float fun(float x,float y)
{
return((y-x)/(y+x));
}
float rk4(float h,float x,float y)
{
float k1,k2,k3,k4,ynew;
k1=h*fun(x,y);
k2=h*fun(x+h/2,y+k1/2);
k3=h*fun(x+h/2,y+k2/2);
k4=h*fun(x+h,y+k3);
ynew=y+(k1+2*k2+2*k3+k4)/6.0;
return(ynew);
}

OUTPUT
Enter Initial Value of x,y

gopalkrishnasir@yahoo.com

Page 13 of 24

BCA306P -NUMERICAL ANALYSIS and LINEAR PROGRAMMING Lab

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

BCA306P -NUMERICAL ANALYSIS and LINEAR PROGRAMMING Lab

15

Program to find Solution to Second-order ODE using Runge-Kutta 4th Order


Method
#include <stdio.h>
#include <conio.h>
#include<math.h>
float rkfun(float,float, float);
void rk42(float,float,float,float);
float ynew,znew;
void main()
{
float h,x,y,z,xf;
int iter=0;
clrscr();
printf("\n Enter Initial Value of x,y,z\n");
scanf("%f%f%f",&x,&y,&z);
printf("\n Enter Interval (h)\n");
scanf("%f",&h);
printf("\n Enter Final Value(xf)\n");
scanf("%f",&xf);
printf("\n\t\t*******************************\n");
printf("\t\t Runge-Kutta 4th Order Method(Second Order ODE)\n");
printf("\n\t\t Slno.\t X\t Y\t Z");
printf("\n\t\t %2d\t %2.4f %2.4f\t %2.4f",iter,x,y,z);
while(x<xf)
{
iter++;
rk42(h,x,y,z);
x=x+h;
y=ynew;
z=znew;
printf("\n\t\t %2d\t %2.4f %2.4f\t %2.4f",iter,x,y,z);
}
getch();
}
float rkfun(float x,float y,float z)
{
return(x*z+1);
}
void rk42(float h,float x,float y,float z)
{
float k1,k2,k3,k4,m1,m2,m3,m4;
k1=h*z;
m1=h*rkfun(x,y,z);
k2=h*(z+m1/2);
m2=h*rkfun(x+h/2,y+k1/2,z+m1/2);
k3=h*(z+m2/2);
m3=h*rkfun(x+h/2,y+k2/2,z+m2/2);
k4=h*(z+m3);
m4=h*rkfun(x+h,y+k3,z+m3);
ynew=y+(k1+2*k2+2*k3+k4)/6.0;

gopalkrishnasir@yahoo.com

Page 15 of 24

BCA306P -NUMERICAL ANALYSIS and LINEAR PROGRAMMING Lab

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

BCA306P -NUMERICAL ANALYSIS and LINEAR PROGRAMMING Lab

17

Program to find the integral of a function using adaptive Simpson method


#include<stdio.h>
#include<conio.h>
#include<math.h>
float fun(float);
float simpsons(float,float,float);
void main()
{
float a,b,h,eps,sim_result;
clrscr();
printf("\n Enter Lower and Upper Limit\n");
scanf("%f%f",&a,&b);
printf("\n Enter Epsilon\n");
scanf("%f",&eps);
printf("\n\n \tADAPTIVE SIMPSONS'S METHOD");
sim_result=simpsons(a,b,eps);
printf("\n\n Final Solution =%2.4f",sim_result);
getch();
}
float fun(float x)
{
return(1.0/(1+x));
}
float simpsons(float a,float b,float eps)
{
float h,c,d,e,simp1,simp2,it_simp,rt_simp,simp;
h=b-a;
c=(a+b)/2;
simp1=h*(fun(a)+4*fun(c)+fun(b))/6.0;
d=(a+c)/2;
e=(c+b)/2;
simp2=h*(fun(a)+4*fun(d)+2*fun(c)+4*fun(e)+fun(b))/12.0;
printf("\n a=%2.2f b=%2.2f s1=%2.3f s2=%2.3f", a,b,simp1,simp2);
if(fabs(simp2-simp1)<15*eps)
{
simp=simp2+(simp2-simp1)/15;
}
else
{
it_simp=simpsons(a,c,eps/2);
rt_simp=simpsons(c,b,eps/2);
simp=it_simp+rt_simp;
}
return(simp);
}

OUTPUT
Enter Lower and Upper Limit

gopalkrishnasir@yahoo.com

Page 17 of 24

BCA306P -NUMERICAL ANALYSIS and LINEAR PROGRAMMING Lab

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

Final Solution =1.9459

gopalkrishnasir@yahoo.com

Page 18 of 24

BCA306P -NUMERICAL ANALYSIS and LINEAR PROGRAMMING Lab

19

Program to solve the system of equations Ax = b using Gauss-Seidel method


#include<stdio.h>
#include<conio.h>
#include<math.h>
#define EPS 1e-02
void main()
{
static float x[5],a[5][5],oldx[5],sum,err,big;
int i,j,k,iter,n;
clrscr();
printf("\nEnter the order of Matrix\n");
scanf("%d",&n);
printf("\n Enter coefficients of 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++)
{
x[i]=0;
oldx[i]=0;
}
printf("\t\tGauss-Seidal Iterative Method");
printf("\n\t\tGiven matrix");
for(i=1;i<=n;i++)
{
printf("\n");
printf("\t\t");
for(j=1;j<=n+1;j++)
{
printf("%5.3f ",a[i][j]);
}
}
printf("\n\t\titer");
for(i=1;i<=n;i++)
printf("\tx%d ",i);
for(iter=1;iter<=10;iter++)
{
big=0.0;
for(i=1;i<=n;i++)
{
sum=0;
for(j=1;j<=n;j++)
{
if(i!=j)
sum=sum+(a[i][j]*x[j]);
}
x[i]=(a[i][n+1]-sum)/a[i][i];
err=fabs(x[i]-oldx[i]);
if(err>big)big=err;
}
printf("\n\t\t%3d ",iter);
for(k=1;k<=n;k++)
printf("%5.3f ",x[k]);

gopalkrishnasir@yahoo.com

Page 19 of 24

BCA306P -NUMERICAL ANALYSIS and LINEAR PROGRAMMING Lab

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

BCA306P -NUMERICAL ANALYSIS and LINEAR PROGRAMMING Lab

21

Program to find the roots of system of non-linear algebraic equations using


Newtons method.
#include<stdio.h>
#include<math.h>
float
float
float
float
float
float

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

BCA306P -NUMERICAL ANALYSIS and LINEAR PROGRAMMING Lab

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

Roots are x= 3.0002 y= 4.0001

gopalkrishnasir@yahoo.com

Page 22 of 24

BCA306P -NUMERICAL ANALYSIS and LINEAR PROGRAMMING Lab

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

BCA306P -NUMERICAL ANALYSIS and LINEAR PROGRAMMING Lab

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

S-ar putea să vă placă și