Page |1
TO IMPLEMENT ITERATIVE METHODS
1. FLOW CHART OF BISECTION METHOD
START
Define F(x)
Get the value of interval (a,b),
Initialize i =1 Call Subroutine bisect mid
Is B=X
F(mid)<
=
0
Is Abs (XI-
X
X)
I
Y Roll no: 0905210064
Page |2
No Print Solution does not Converge
X
Print iter, XI
STOP
B
Subroutine Bisect
X= (A+B)/2
Iter ++
Print Print ITER ITER XI
RETURN
VIVEK GUPTA
Roll no: 0905210064
Page |3
2. FLOW CHART OF NEWTON-RAPHSON METHOD
START
Define Function f(x)
Define function df(x)
Get the value of x0,aerr,maxitr
Loop for itr = 1 to maxitr
h = f(x0)/df(x0) x1 = x0-h
Print itr,x1
No
Is fabs(h)
X0 =x1
Print solution
Print “solution does not converge”
STOP STOP
VIVEK GUPTA
Roll no: 0905210064
Page |4
3. FLOW CHART OF REGULA-FALSI METHOD START
Define Function F(x)
Define function Regulafalsi()
Get the value of x0, x1, aerr, mitr
Initialize itr
Call function regulafalsi with x3, x0, x1, f(x1), f(x0), itr A B
Yes X1 = x2
No Is
X0 = x1
f(x0)*f(x1)<0
Call function regulafalsi with x3, x0, x1, f(x1), f(x0), itr
Yes Is fabs(x3x2
C
No x2 = x3
VIVEK GUPTA
Roll no: 0905210064
Page |5
Yes Is itr < maxitr
B
No
Print “ Not Convergent”
STOP
A
X = x0-((x1-x0)/(f(x1)-f(x0))*f(x0)
Print itr, x
Return
C
Print “Solution”
RETURN
VIVEK GUPTA
Roll no: 0905210064
Page |6
PROGRAM FOR ITERATIVE METHODS /* ................TO IMPLEMENT NUMERICAL TECHNIQUES USING SWITCH CASE...........*/ #include #include #include #include #include // Declaration of Functions void bisection(); void regulafalsi(); void raphson(); void main() { int c; printf("------------************ NUMERICAL TECHNIQUES ************-----------\n\n\n"); printf("\t\t( 1 ). Bisection Method\n"); printf("\t\t( 2 ). Regula Falsi\ Method Of False Position\n\n"); printf("\t\t( 3 ). Newton Raphson Method\n\n"); printf("
Please enter a choice............... : ");
scanf("%d",&c); switch(c) { case 1: bisection(); break;
case 2: VIVEK GUPTA
Roll no: 0905210064
Page |7 regulafalsi(); break;
case 3: raphson(); break; } getch(); } // Function Definition void bisection() { #define EPS 0.00000005 #define F(x) (x)*log10(x)-1.2 //variable declaration field int count=1,n; float root=1; float x0,x1,x2; float f0,f1,f2; int i=0; clrscr(); printf("\n Solution by Bisection Method \n"); printf("\n Equation is "); printf(" \n\t\t\t (x*log x)-1.2 = 0\n\n"); printf(" Enter the number of iterations : "); scanf("%d",&n); /* finding an approximate ROOT of given equatoin, having +ve value*/ for(x2=1;;x2++) { VIVEK GUPTA
Roll no: 0905210064
Page |8 f2=F(x2); if(f2>0) { break; } } /*finding an approximate ROOT of g iven equation, having -ve value*/ for(x1=x2-1;;x2--) { f1=F(x1); if(f1<0) { break; } } //... printing result printf("\t\t----------------------------------------------------------"); printf("\n\t\t ITERATIONS\t\t
ROOTS\n");
printf("\t\t----------------------------------------------------------"); for(;count<=n;count++) { x0=(x1+x2)/2.0; f0=F(x0); if(f0==0) { root=x0; } if(f0*f1<0) { VIVEK GUPTA
Roll no: 0905210064
Page |9 x2=x0; } else { x1=x0; f1=f0; } printf("\n\t\t ITERATION %d", count); printf("\t :\t %f",x0);
if(fabs((x1-x2)/x1)
Root = %f",x0); Iteration = %d\n", count);
printf("\t\t--------------------------------------------------------"); getch(); exit(0); } } printf("\n\t\t--------------------------------------------------------"); printf("\n\t\t\t
Root = %7.4f",x0);
printf("\n\t\t Iterations = %d\n", count-1); printf("\t\t----------------------------------------------------------"); getch(); } //Function Definition void regulafalsi() { //... formulae declaration VIVEK GUPTA
Roll no: 0905210064
P a g e | 10 #define EPS 0.00005 #define f(x) 3*x+sin(x)-exp(x); float f0,f1,f2; float x0,x1,x2; int itr; int i; clrscr(); printf("\n Solution by FALSE POSITION METHOD\n"); printf("\n\t\t\t Equation is : "); printf("\n\t\t\t 3*x+sin(x)-exp(x)=0\n\n"); printf(" Enter the number of iterations :"); scanf("%d",&itr); for(x1=0.0;;) { f1=f(x1); if(f1>0) { break; } else { x1=x1+0.1; } } x0=x1-0.1; f0=f(x0); printf("\n\t\t----------------------------------------------------------"); printf("\n\t\t
ITERATION\t x2\t\t F(x)\n");
printf("\t\t------------------------------------------------------------"); VIVEK GUPTA
Roll no: 0905210064
P a g e | 11 for(i=0;i0) { x1=x2; f1=f2; } else { x0=x2; f0=f2; } if(fabs(f2)>EPS) { printf("\n\t\t
%d\t%f\t%f\n",i+1,x2,f2);
} continue; } printf("\t\t------------------------------------------------------------"); printf("\n\t\t\t\tRoot = %f\n",x2); printf("\t\t-----------------------------------------------------------"); getch(); } //Function definition void raphson() { //......defining formulae VIVEK GUPTA
Roll no: 0905210064
P a g e | 12 #define f(x) 3*x*x*x-cos(x)-1 #define df(x) 9*x*x+sin(x) float x1,x0; float f0,f1; float df0; float eps; float error; int i=1; int itr; clrscr(); printf("\n Sloution by NEWTON RAPHSON method\n"); printf("\n Equation is: "); printf("\n\t\t\t 3*x-cos(x) - 1 = 0\n\ n"); /* Finding an appropriate ROOT of Given Equation, Having +ve value*/ for(x1=0; ;x1+=0.01) { f1=f(x1); if(f1>0) { break; } } /* Finding an appropriate ROOT of Given Equation, Having -ve value*/ x0=x1-0.01; f0=f(x0); printf("Enter the number of iterations : "); scanf(" %d",&itr); printf("Enter the maximum possible error :"); scanf("%f",&eps); VIVEK GUPTA
Roll no: 0905210064
P a g e | 13 if(fabs(f0)>f1) { printf("\n\t\t The root is near to %.6f\n",x1); } if (f1>fabs(f(x0))) { printf("\n\t\t The root is near to %.6f\n",x0); } x0=(x0+x1)/2;
for(;i<=itr;i++) { f0=f(x0); df0=df(x0); x1=x0-(f0/df0); printf("\n\t\t The %d approximation to the root is : %f",i,x1); error=fabs(x1-x0); if(erroreps) { printf("\n\n\t NOTE :-"); printf(" The number of iterations are not sufficient."); } printf("\n\n\n\t\t\t-----------------------------------"); printf("\n\t\t\t
The root is %.6f ",x1);
printf("\n\t\t\t------------------------------------");
}
--------------------------------------------------------**************************-----------------------------------------------------VIVEK GUPTA
Roll no: 0905210064
P a g e | 14
OUTPUT OF ITERATIVE METHODS
-----------------------************ NUMERICAL TECHNIQUES ************-----------
( 1 ). Bisection Method
( 2 ). Regula Falsi Method Of False Position
( 3 ). Newton Raphson Method
Please enter a choice............... : 1
Solution by Bisection Method
Equation is
(x*log x)-1.2 = 0
Enter the number of iterations : 5 ---------------------------------------------------------ITERATIONS
ROOTS
---------------------------------------------------------ITERATION 1
:
2.500000
ITERATION 2
:
2.750000
ITERATION 3
:
2.625000
ITERATION 4
:
2.687500
ITERATION 5
:
2.718750
-------------------------------------------------------Root = 2.7188 Iterations = 5 ---------------------------------------------------------VIVEK GUPTA
Roll no: 0905210064
P a g e | 15
Solution by FALSE POSITION METHOD
Equation is : 3*x+sin(x)-exp(x)=0
Enter the number of iterations :1
---------------------------------------------------------ITERATION
x2
F(x)
-----------------------------------------------------------1
0.361262
0.002101
-----------------------------------------------------------Root = 0.361262 ----------------------------------------------------------Solution by NEWTON RAPHSON method
Equation is:
3*x-cos(x) - 1 = 0
Enter the number of iterations : 3 Enter the maximum possible error :.0000001 The root is near to 0.820000 The 1 approximation to the root is : 0.824127 The 2 approximation to the root is : 0.824126 The 3 approximation to the root is : 0.824126 ----------------------------------The root is 0.824126 ------------------------------------
VIVEK GUPTA
Roll no: 0905210064
P a g e | 16
4.FLOW CHART OF NEWTON’S FORWARD INTERPOLATION
START
Input n
Get the values of ax, ay
Get value of x
h = ax[1] – ax[0]
Start loop i = 0 to n-1
Diff.[i][1] = ay[I + 1] – ay[i]
End loop i
Start loop j = 2 to 4 Start loo I = 0 to n Diff[i][j] = diff[I +1][j +1] – diff[i][j – i] End loo i
A
VIVEK GUPTA
Roll no: 0905210064
P a g e | 17
A
I=0
Is B
No
ax[i]>x
I=I+1
B
I = i-1
P = (x – ax[i])/h
Y1 = p* diff[I - 1][1]
Y2 = p * (p + 1) * diff[I – 2][2]/2
Y3 = ( p + 2 ) * P * (p -1 )*diff[I – 2][3]/6
Y4 = (p + 2) * (p + 1) * p * (p – 1) * diff[I – 3][4]/24
Ay[i] + y1 + y2 + y3 + y4
Print output x,y
STOP
VIVEK GUPTA
Roll no: 0905210064
P a g e | 18
PROGRAM FOR NEWTON’S FORWARD INTERPOLATION #include #include #include #include #include //...main execution method... void main() {// ... variable declaration... int n,i,j; float ax[10],ay[10],x,h,p,diff[20][20],y1,y2,y3,y4,y=0; clrscr(); printf("\n enter the no. of terms -"); scanf("%d",&n); printf("\n\n enter the value in the form of x -"); for(i=0;i
Roll no: 0905210064
P a g e | 19
} printf("\n\n enter the value of x for"); printf("\nwhich you want the value of of y -"); scanf("%f",&x); h=ax[1]-ax[0]; for(i=0;i
}
i=0; do { i++; }while(ax[i]
Roll no: 0905210064
P a g e | 20
OUTPUT OF NEWTON’S FORWARD INTERPOLATION enter the no. of terms -3
enter the value in the form of x -
enter the value of x1-1
enter the value of x2-2
enter the value of x3-3
enter the value in the form of y -
enter the value of y1-1.2
enter the value of y2-2.3
enter the value of y3-3.2
enter the value of x for
which you want the value of of y -2.3
when x=2.3000,y=2.62999988
press enter to exit……. VIVEK GUPTA
Roll no: 0905210064
P a g e | 21
5.FLOW CHART OF NEWTON’S BACKWARD INTERPOLATION START
Input Number of terms n
Get the values of ax, ay
h = ax[1] – ax[0]
Start loop i = 0 to n-1
Diff.[i][1] = ay[I + 1] – ay[i]
End loop i
Start loop j = 2 to 4
Start loop I = 0 to n - j
Diff[i][j] = diff[I +1][j +1] – diff[i][j – i]
End loop i
End loop j
I=0
VIVEK GUPTA
Roll no: 0905210064
P a g e | 22
Is ! ax[i] < x
A
I = I +1
A
X0 = mx[i]
Sum = 0
Y0 = my[i] Fun = 1
P = (x – x0)/n
Sum = y0
Start loop k = 1 to 4
Fun = (fun * (p – (k- 1)))/k
Sum = sum + fun * diff[i][k]
End loop k
STOP VIVEK GUPTA
Roll no: 0905210064
P a g e | 23
PROGRAM FOR NEWTON’S BACKWARD INTERPOLATION #include #include #include #include #include void main() { int n,i,j,k; float mx[10],my[10],x,x0=0,y0,sum,h,fun,p,diff[20][20],y1,y2,y3,y4; clrscr(); printf("\n enter the no. of terms -"); scanf("%d",&n); printf("\n\n enter the value in the form of x -"); for(i=0;i
Roll no: 0905210064
P a g e | 24
scanf("%f",&x); h=mx[1]-mx[0]; for(i=0;ix) { i++; } x0=mx[i]; sum=0; y0=my[i]; fun=1; p=(x-x0)/h; sum=y0; for(k=1;k<=4;k++) { fun=(fun*(p-(k-1))/k); sum=sum+fun*diff[i][k]; }printf("\n when x=%6.4f,y=%6.8f",x,sum); printf("\n\n\n press enter to exit");getch();} --------------------------------------------------**********************----------------------------------------VIVEK GUPTA
Roll no: 0905210064
P a g e | 25
OUTPUT OF NEWTON’S BACKWARD INTERPOLATION enter the no. of terms -3
enter the value in the form of x -
enter the value of x1-1
enter the value of x2-2
enter the value of x3-3
enter the value in the form of y -
enter the value of y1-1.2
enter the value of y2-2.3
enter the value of y3-3.4
enter the value of x for which you want the value of of y -2.3
when x=2.3000,y=2.62999988
press enter to exit……..
VIVEK GUPTA
Roll no: 0905210064
P a g e | 26
6.FLOWCHART OF LAGRANGE’S INTERPOLATION START
Get the value of n
Get the values ax, ay
Get the value of x Y=0
Loop for I = 0 to n Nr = dr = 1
Loop for j to n
Is j ! = i
B
Nr* = x – ax[j]
Dr* = ax[i] – ax[j] B End loop [j]
Print x, y as solution
STOP
VIVEK GUPTA
Roll no: 0905210064
P a g e | 27
PROGRAM FOR LAGRANGE’S INTERPOLATION #include #include #include #include #include void main() { int n; int i,j;
//...No. of terms //...Loop variables
float ax[100];
//...array limit 99
float ay[100];
//...array limit 99
float x=0;
//...User Query
float y=0;
//...Initial value 0
float nr;
//...calc. section
float dr;
//...calc. section
clrscr(); printf("\n Enter the number of terms - "); scanf("%d",&n); //... Input Sequel for array X printf("\n\n Enter the value in the form of x - "); //...Input loop for X for(i=0;i
Roll no: 0905210064
P a g e | 28
//...Input Sequel for array Y printf("\n\n Enter the value in the form of y - "); //... Input loop for Y for(i=0;i
Roll no: 0905210064
P a g e | 29
OUTPUT OF LAGRANGE’S INTERPOLATION Enter the number of terms - 3 Enter the value in the form of x -
Enter the value of x1 - 1 Enter the value of x2 - 2
Enter the value of x3 - 3
Enter the value in the form of y -
Enter the value of y1 - 1.2
Enter the value of y2 - 2.3
Enter the value of y3 - 4.5
Enter the value of x for which you want the value of y - 2.3
When x= 2.30,y=13.47
Press Enter to Exit.........
VIVEK GUPTA
Roll no: 0905210064
P a g e | 30
7.FLOW CHART OF SIMPSON’S 1/3 RULE
START
Define function y(x)
Get values x0, Xn, n
H = (Xn- x0)/n
S = Y0 + Yn + 4Y1
Loop for I = 3 to n -1
S + = 4 * y1 + 2 * Yi+1
End loop (i)
p = (h/3 ) * s
Print “ solution”, p
STOP
VIVEK GUPTA
Roll no: 0905210064
P a g e | 31
PROGRAM FOR SIMPSON’S 1/3 RULE #include #include #include #include #include float fun(float); void main() { float result=1; float a,b; float sum,h; int i,j,n; clrscr(); printf("\n\n Enter the range - "); printf("\n\n Lower Limit a - "); scanf("%f",&a); printf("\n\n Upper limit b - "); scanf("%f",&b);
//... Input No. of Subintervals printf("\n\n Enter number of subintervals - "); scanf("%d",&n); //... Calculation and processing section h=(b-a)/n; sum=0; VIVEK GUPTA
Roll no: 0905210064
P a g e | 32
sum=fun(a)+4*fun(a+h)+fun(b); for(i=3;i
//... Output Section printf("\n\n\n\n Value of integral is %6.4f\t",result); //...Invoke User Watch halt function printf("\n\n\n Prress enter to exit"); getch(); } //... Termination of Main Execution Thread //...Function body float fun(float x) { float temp; temp=1/(1+(x*x)); return temp; } //...Termination of Function body
------------------------------------*******************************-------------------------
VIVEK GUPTA
Roll no: 0905210064
P a g e | 33
OUTPUT OF SIMPSON’S 1/3 RULE
Enter the range -
Lower Limit a - 2
Upper limit b - 8
Enter number of subintervals - 4
Value of integral is 0.3433
Prress enter to exit……………………………
VIVEK GUPTA
Roll no: 0905210064
P a g e | 34
8.FLOW CHART FOR SIMPSON’S 3/8 RULE
START
Define function f(x) Get values x0, Xn, n
H = (Xn – x0)/n
Sum = 0
Sum = f(a) + f(b)
Loop for I = 1 to n
Is I % 3
Sum + = 2 * f(a + I * h)
=0
Sum + = 3 * f(a + I * h)
End loop i
Print sum
STOP
VIVEK GUPTA
Roll no: 0905210064
P a g e | 35
PROGRAM FOR SIMPSON’S 3/8 RULE #include #include #include #include float fun(float); void main() { float result=1; float a,b; float h,sum; int i,j,n; clrscr(); //...Input Range printf("\n\n Enter the range - "); printf("\n\n Lower Limit a - "); scanf("%f",&a); printf("\n\n Upper Limit b - "); scanf("%f",&b); //... Input No.of subintervals printf("\n\n Enter the number of subintervals - "); scanf("%f",&n); //... calculation and processing section h=(b-a)/n; VIVEK GUPTA
Roll no: 0905210064
P a g e | 36
sum=0; sum=fun(a)+fun(b); for(i=0;i
---------------------------------************************---------------------------------VIVEK GUPTA
Roll no: 0905210064
P a g e | 37
9.FLOW CHART OF TRAPEZOIDAL RULE
START
Define function y(x)
Get values x0, Xn, n
H = (Xn- x0)/n
S = y(x0) + y(Xn)
Loop for I = 1 to n -1
S + = 2 * y * (x0 + I * h)
End loop (i)
T = (h/2 ) * s
Print “ solution”, t
STOP
VIVEK GUPTA
Roll no: 0905210064
P a g e | 38
PROGRAM FOR TRAPEZOIDAL RULE #include #include #include #include #include float fun(float); void main() { float result=1; float a,b; float h,sum; int i,j,n; clrscr(); //...Input range printf("\n\n Enter the range - "); printf("\n\n Lower limit a - "); scanf("%f",&a); printf("\n\n Upper Limit b - "); scanf("%f",&b); //... Input no. of subintervals printf("\n\n Enter the number of subintervals - "); scanf("%d",&n); //... calculation and processing section h=(b-a)/n; VIVEK GUPTA
Roll no: 0905210064
P a g e | 39
sum=0; sum=fun(a)+fun(b);
for(i=1;i
-----------------------------***********************----------------------
VIVEK GUPTA
Roll no: 0905210064
P a g e | 40
OUTPUT OF TRAPEZOIDAL RULE
Enter the range -
Lower limit a - 2
Upper Limit b - 8
Enter the number of subintervals - 3
value of the integral is 0.5330
Press Enter to Exit................
VIVEK GUPTA
Roll no: 0905210064
P a g e | 41
IMPLEMENTATION OF LEAST SQUARE METHOD FOR CURVE FITTING #include #include #include void main() { Int i=0; Int observ; float x[10]; float y[10]; float xy[10]; float x2[10]; float sum1=0.0; float sum2=0.0; float sum3=0.0; float sum4=0.0; double a; double b; clrscr(); printf(“\n\n Enter the number of observations-“); scanf(“%d”,&observ); Printf(“\n\n\n Enter the values of x-\n”);
for(;i
Roll no: 0905210064
P a g e | 42
Printf(“\n\n Enter the value of x%d:”,i1); Scanf(“%f”,&x*i+);
Sum1+=x[i]; } Printf(“\n\n Enter th values of y-\n”);
for(i=0;i
Sum2+=y[i]; } for(i=0;i
Roll no: 0905210064
P a g e | 43
printf(“\n\n\n \t\t\t Y=%.2f(%.2f)X”,a,b); printf(“\n\n\n Press Enter to Exit”);
getch(); }
VIVEK GUPTA
Roll no: 0905210064
P a g e | 44
VIVEK GUPTA
Roll no: 0905210064