Numerical NNNN
Numerical NNNN
Computing
Laboratory Journal
Experiment No: 1
AIM
B)- Program
/* Bisection Method */
# include < stdio.h>
# include < math.h>
float y(float x)
{
return (x*x*x 4*x-9);
}
Void bisect (float *x, float a, float b, int* int)
(*x = (a +b)/2;
2
++(*itr);
printf(Iteration no. % 3d x = % 7.5 f \n,*x);
}
main ( )
{
int int=0, maxitr;
float x,a,b,aerr,x1;
printf(Enter the value of a,b allowed error , maximum iterations \n);
scanf ( %f %f %f %d, & a ,& b ,& aerr, &maxitr );
bisect (&x, a, b, &itr);
do
{
if (f(a)*f(x)<0)
b=x;
else
a=x;
bisect (&x1, a, b, &itr);
if ( fabs (x1-x)< aerr)
{
printf (After % d iteration , root =% 6.4\n,itr,x1);
return 0;
}
x=x1;
}
While (itr < maxtr ) ;
printf (Solution does not converge, iterations not sufficient );
return 1;
}
Experiment No: 2
AIM : Flow chart & C program of Newton Ramphson Method
A) Flow chart: Flow chart of Newton Ramphson Method for
B) C- Program
/* Newton Ramphson Method */
# include < stdio.h>
# include < math.h>
float f (float x)
{
return x*log 10(x)-1.2;
}
float dx( float x);
4
{
return log 10(x)+0.43429 ;
}
main ( )
{
int int, maxitr;
float h,x0,x1,aerr;
printf(Enter the value of x0, allowed error , maximum iterations \n);
scanf ( %f %f %d, & x0 ,& aerr, &maxitr );
for (itr=1; itr,=maxitr; itr++)
{
h=f(x0)/ df(x0);
x1=x0-h;
printf(Iteration no. %3d,
x= %9.6f\n, itr, x1);
if (fabs (h) < aerr)
{
printf(After % 3d iterations, root = % 8.6 f\n, itr,x1);
return 0;
}
x0=x1;
}
printf(Iterations not sufficient , solution does not converge\n);
return 1;
}
Experiment No: 3
AIM
A) Flow chart
B) C- Program
/* Gauss elimination method */
# include <studio.h)
# define N 4
main ( )
{
float a[N] [N+1] ,x[N] , t, s;
int i, j, k ;
print f (enter the elements of the
augmented matrix row wise \n) ;
6
Experiment No: 4
AIM
A) Flow chart
B) C- Program
/* Gauss Jordan method */
# include <studio.h>
#define N 3
main ( )
{
float a [N] [N+1] , t;
int i, j, k;
print f (Enter the elements of the
augmented matrix row wise\n)
8
Experiment No: 5
AIM
A) Flow chart.
B) C- Program
/* Eulers Method */
# include < stdio.h>
float df(float x, float y)
{
return x+y;
}
main ( )
{
float x0,y0,h,x,z1,y1;
printf(Enter the value of x0,y0, h,x);
10
Experiment No: 6
AIM
A) Flow chart
11
B) C- Program
/* Runga Kutta Method */
# include < stdio.h>
float f (float x, float y)
{
return x+x*y;
}
main ( )
{
float x0,y0,h,xn,x,y,k1,k2,k3,k4,k;
12
Experiment No: 7
AIM
A) Flow chart
13
C) C- Program
y(x)is the function to be integrated
x0 is x0
xN is xn
/* Trapezoidal Rule */
# include < stdio.h>
float y(float x)
{
return 1/1(1+x*x);
}
14
main ( )
{
float x0,xn,h,s;
int i, n;
printf(Enter x0,xn, no. of subintervals);
scanf ( %f %f %d , & x0 ,& xn ,& n );
h=( xn- x0)/ n;
s= y(x0) + y(xn) ;
for (i=1;i<=n-1;i++)
s += 2*y(x0+i*h);
printf ( Value of integral is % 6.4 F \n (h/2)*s) ;
}
Experiment No:8
AIM
15
B)
C- Program
/* Simpsons 1/3rd Rule */
# include < stdio.h>
float y(float x)
{
return 1/1(1+x*x);
}
main ( )
{
16
float x0,xn,h,s;
int I,n;
printf(Enter x0,xn, no. of subintervals);
scanf ( %f %f %d , & x0 ,& xn ,& n );
h= ( xn- x0)/ n;
s= y(x0) + y(xn) +4*y(x0+h);
for (i=3;i<=n-1;i+=2)
s += 4*y(x0+i*h)+2*y(x0+(i-1)*h);
printf ( Value of integral is % 6.4 F \n (h/3)*s) ;
}
Experiment No: 9
AIM : Flow chart & C program of Newton Forward Method
A) Flow chart
17
C- Program
/* NETWON FORWARD METHOD*/
# include <studio .h>
#define MAXN 100
#define ORDER 4
main 0
{
18
Experiment No: 10
AIM
19
B) C- Program
/* Modified Eulers Method */
# include < stdio.h>
# include < conio. h>
# include < math. h>
void main ( )
{
float x[5], y[5], h, f(float, float);
int i, n;
clrscr ();
printf ( Modified Eulers Method\n\n)
printf(Enter the initial value of X, x(0)=\t);
scanf ( %f , & x[0] );
20
Out put
Enter the initial value of X, x (0) =1
Enter the initial value of Y, y (0) =1
Enter the width of interval, h= 2
Enter the no. of steps, n = 2
x (1)=1.20000
y(1)=1.639195
x (1)=1.40000
y(1)=2.359992
x (1)=1.60000
y(1)=3.165585
Experiment No: 11
AIM
21
B) C- Program
/* Parabo;ic fit by Least Square Method */
# include < stdio.h>
main ( )
{
float augm [3] [4] ={ { 0,0,0,0},{0,0,0,0}, {0,0,0,0}};
float t, a, b, c, x, y, xsq;
22
int i, j, k , n;
printf (Enter the no. of pairs of observed values: );
scanf ( %d ,&n );
augm [0] [0] = n ;
for ( i = 0 ; i < n ; i++)
{
printf ( Pair no. %d \ n,i+1);
scanf ( %f %f ,&x, &y );
xsq = x*x ;
augm [0] [1]
augm [0] [2]
augm [1] [2]
augm [2] [2]
augm [0] [3]
augm [1] [3]
augm [2] [3]
+=x;
+ = xsq ;
+ = x * xsq;
+ = xsq * xsq;
+=y;
+ = x*y ;
+ = xsq*y
}
augm [1] [1] = augm [0] [2] ;
augm [2] [1] = augm [1] [2] ;
augm [1] [0] = augm augm [0] [1] ;
augm [2] [0] = augm augm [1] [1] ;
printf (The augmentd matrix is : - ) ;
for ( i=0 ;i<3; i++)
{
for ( j=0 ;j<4; j++)
printf (% 9.4 f , augm [i] [j] ) ;
printf ( \n ) ;
}
/* Now solving for a, b, c by Gauss jorden Method */
for ( j=0;j,3j++)
for ( i=0; i<3; i++)
if (i!=j)
{
t= augm [i] [j] / augm [j] [j]
for (ki=0;k<4; k++)
augm [i] [k]-= augm [j] [k]*t;
}
a=
b=
c=
printf(a= % 8.4 f b = 8.4 f c = % 8.4 f \n , a ,b ,c );
}
23