Program To Implement The Bisection Method: Anshul Siwach
Program To Implement The Bisection Method: Anshul Siwach
/* ********************************************************
Program to Implement the Bisection Method
************************************************************* */
//...Included Header Files
#include<stdio.h>
#include<math.h>
#include<conio.h>
#include<process.h>
#include<string.h>
#define EPS 0.00000005
#define F(x) (x)*log10(x)–1.2
//...Function Prototype Declaration
void Bisect();
//...Global Variable Declaration field
int count=1,n;
float root=1;
//... Main Function Implementation
void main()
{
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);
Bisect();
getch();
}
//... Function Declaration
void Bisect()
{
float x0,x1,x2;
float f0,f1,f2;
int i=0;
/*Finding an Approximate ROOT of Given Equation, Having +ve Value*/
for(x2=1;;x2++)
{
f2=F(x2);
if (f2>0)
{
break;
}
}
/*Finding an Approximate ROOT of Given Equation, Having -ve Value*/
for(x1=x2-1;;x2--)
{
f1=F(x1);
if(f1<0)
{
break;
}
}
//...Printing Result
Developed by : Anshul Siwach
Computer Oriented Numerical Techniques: 2
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)
{
x2=x0;
}
else
{
x1=x0; f1=f0;
}
printf("\n\t\t ITERATION %d", count);
printf("\t :\t%f",x0);
if(fabs((x1-x2)/x1) < EPS)
{
printf("\n\t\t---------------------------------");
printf("\n\t\t Root = %f",x0);
printf("\n\t\t Iterations = %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\t Iterations = %d\n", count-1);
printf("\t\t------------------------------------------");
getch();
}
OUTPUT
Solution by BISECTION method
Equation is
x* log(x) - 1.2=0
Enter the number of iterations: 30
-----------------------------------------
ITERATIONS ROOTS
-----------------------------------------
ITERATION 1: 2.500000
ITERATION 2: 2.750000
ITERATION 3: 2.625000
ITERATION 4: 2.687500
ITERATION 5: 2.718750
ITERATION 10: 2.741211
ITERATION 11: 2.740723
ITERATION 12: 2.740479
ITERATION 13: 2.740601
ITERATION 14: 2.740662
ITERATION 15: 2.740631
ITERATION 16: 2.740646
ITERATION 17: 2.740639
ITERATION 18: 2.740643
ITERATION 19: 2.740644
ITERATION 20: 2.740645
ITERATION 21: 2.740646
ITERATION 22: 2.740646
ITERATION 23: 2.740646
ITERATION 24: 2.740646
ITERATION 25: 2.740646
ITERATION 26: 2.740646
ITERATION 27: 2.740646
ITERATION 28: 2.740646
ITERATION 29: 2.740646
ITERATION 30: 2.740646
-----------------------------------------
Root = 2.7406
Iterations = 30
-----------------------------------------
C:\tc\exe>
/* ********************************************************
Program to Implement the Iteration Method
******************************************************** */
#include<stdio.h>
#include<math.h>
#include<conio.h>
#define EPS 0.00005
#define F(x) (x*x*x + 1)/2
#define f(x) x*x*x - 2*x + 1
void ITER();
void main ()
{
clrscr();
printf("\n\t Solution by ITERATION method \n");
printf("\n\t Equation is ");
printf("\n\t\t\t\t X*X*X - 2*X + 1 = 0\n\n");
ITER();
getch();
}
void ITER()
{
float x1,x2,x0,f0,f1,f2,error;
int i=0,n;
for(x1=1;;x1++)
{
f1=F(x1);
if (f1>0)
break;
}
for(x0=x1-1;;x0--)
{
f0=f(x0);
if(f0<0)
break;
}
x2=(x0+x1)/2;
printf("Enter the number of iterations:");
scanf("%d",&n);
printf("\n\n\t\t The 1 approximation to the root is: %f",x2);
for(;i<n-1;i++)
{
f2=F(x2);
printf("\n\n\t\t The %d approximation to the root
is:%f",i+2,f2);
x2=F(x2);
error=fabs(f2-f1);
if(error<EPS)
break;
f1=f2;
}
if(error>EPS)
printf("\n\n\t NOTE:- The number of iterations are
notsufficient.");
printf("\n\n\n\t\t\t------------------------------");
printf("\n\t\t\t The root is %.4f",f2);
printf("\n\t\t\t-----------------------------");
}
Output
Solution by ITERATION method
Equation is
x*x*x-2*x+1=0
Enter the number of iterations: 15
The 1 approximation to the root is: 0.000000
The 2 approximation to the root is: 0.500000
The 3 approximation to the root is: 0.562500
The 4 approximation to the root is: 0.588989
The 5 approximation to the root is: 0.602163
The 6 approximation to the root is: 0.609172
The 7 approximation to the root is: 0.613029
The 8 approximation to
The 11 approximation to the root is: 0.617504
The 12 approximation to the root is: 0.617730
The 13 approximation to the root is: 0.617860
The 14 approximation to the root is: 0.617934
The 15 approximation to the root is: 0.617977
the root is: 0.615190 The 9 approximation to
the root is: 0.616412
The 10 approximation to the root is: 0.617107
-----------------------------------------------------
The Root is 0.6179 (Correct to four decimal places)
-----------------------------------------------------
/*********************************************************
Program to Implement the Method of Regula Falsi
******************************************************** */
// ... Included Header files
#include<stdio.h>
#include<math.h>
#include<conio.h>
#include<string.h>
#include<process.h>
//...Formulae declaration
#define EPS 0.00005
#define f(x) 3*x+sin(x)-exp(x)
//...Function Declaration Prototype
void FAL_POS();
//...Main Execution Thread
void main()
{
clrscr();
printf("\n Solution by FALSE POSITION method\n");
printf("\n Equation is ");
printf("\n\t\t\t 3*x + sin(x)-exp(x)=0\n\n");
FAL_POS();
}
//...Function Definition
void FAL_POS()
{
float f0,f1,f2;
float x0,x1,x2;
int itr;
int i;
printf("Enter the number of iteration:");
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--------------------------------------------");
for(i=0;i<itr;i++)
{
x2=x0-((x1-x0)/(f1-f0))*f0;
f2=f(x2);
if(f0*f2>0)
{
x1=x2;
f1=f2;
}else
{
x0=x2;f0=f2;
}
if(fabs(f(2))>EPS)
{
printf("\n\t\t%d\t%f\t%f\n",i+1,x2,f2);
}
}
printf("\t\t--------------------------------------------");
printf("\n\t\t\t\tRoot=%f\n",x2);
printf("\t\t-------------------------------------------");
getch();
}
OUTPUT
Solution by FALSE POSITION method
Equation is
3*x+sin(x)-exp(x)=0
Enter the number of iteration: 11
----------------------------------------------------
ITERATION X2 F(x)
----------------------------------------------------
1 0.361262 0.002101
2 0.360409 -0.000031
3 0.360422 0.000000
4 0.360422 -0.000000
5 0.360422 0.000000
6 0.360422 0.000000
7 0.360422 0.000000
8 0.360422 0.000000
9 0.360422 0.000000
10 0.360422 0.000000
11 0.360422 0.000000
----------------------------------------------------
Root=0.360422
----------------------------------------------------
/* *****************************************************
Program to Implement the False Position Method
******************************************************** */
\\METHOD OF FALSE POSITION
#include<stdio.h>
#include<conio.h>
#include<math.h>
float f(float x)
{
return cos(x)-x*exp(x);
}
void regula (float *x, float x0,float x1, float fx0, float
fx1,int*itr)
{
*x=x0-((x1-x0)/(fx1-fx0))*fx0;
++(*itr);
printf("Iteration no.%3d x=%7.5f\n",*itr,*x);
}
main()
{
int itr=0,maxitr;
float x0,x1,x2,x3,aerr;
printf("Enter the values for x0,x1, allowed
error,max.iteration\n");
scanf("%f%f%f%d",&x0,&x1,&aerr,&maxitr);regula(&x2,x0,x1
,f(x0),f(x1),&itr);
do
{
if(f(x0)*f(x2)<0) x1=x2;
else
x0=x2;
regula(&x3,x0,x1,f(x0),f(x1),&itr);
if(fabs(x3 x2)<aerr)
{
printf("After %d iterations, root=%6.4f\n",itr,x3
}
x2=x3;
getch();
return (0);
}
while (itr<maxitr);
printf("Solution does not converge, iterations not
sufficient\n");
getch();
return(1);
OUTPUT
Enter the values for x0,x1, allowed error, max.iteration 01.0000520
Iteration number 1 x = 0.31467
Iteration number 2 x = 0.44673
Iteration number 3 x = 0.49402
Iteration number 4 x = 0.50995
Iteration number 5 x = 0.51520
Iteration number 6 x = 0.51692
Iteration number 7 x = 0.51748
Iteration number 8 x = 0.51767
Iteration number 9 x = 0.51773
Iteration number 10 x = 0.51775
After 10 iterations, root = 0.5177
/* *****************************************************
PROGRAM TO IMPLEMENT MULLER’S METHOD OF FINDING ROOTS
******************************************************** */
//...HEADER FILES DECLARATION
#include <stdio.h>
#include <string.h>
#include <conio.h>
#include <math.h>
#include <process.h>
#include <dos.h>
//... Function Prototype Declaration
float y(float);
float A(float,float,float,float,float,float);
float B(float,float,float,float,float,float);
float approx(float,float,float,float);
void main()
{
//... Variable Declaration Field
//... Floating Type
float a,b;
float Xi,Xi1,Xi2;
float Yi,Yi1, Yi2;
float Xn;
float aerr;
//... Integer Type
int i=1;
int loop=0;
float c;
c=(((Yc-Ya)*pow((Xb-Xa),2))-((Yb-Ya)*pow((Xc-Xa),2)))
/((Xb-Xa)*(Xc-Xa)*(Xb-Xc));
return(c);
}
//...Termination of Function B
//...Function approx body
float approx(float x,float y,float a,float b)
{
int c;
float t;
c=sqrt(b*b-4*a*y);
if((b+c)>(b-c))
{
t=x-((2*y)/(b+c));
}
else
{
t=(x-((2*y)/(b-c)));
}
return (t);
}
//...Termination of Function approx
OUTPUT
Enter the values of X(i),X(i-1),X(i-2), absolute error
Enter the value of X(i) - 3
Enter the value of X(i-1) - 2
Enter the value of X(i-2) - 1
Enter the value of Absolute Error - 0.000001
Processing ..................................
After Iteration 1 value of x - 2.085714
After Iteration 2 value of x - 2.094654
After Iteration 3 value of x - 2.094550
After Iteration 4 value of x - 2.094552
After Iteration 5 value of x - 2.094552
After 6 iteration root is - 2.094552
Press Enter to Exit
/* *************************************************************
Program made for NEWTON RAPHSON to solve the equation
******************************************************************\
//....including source header files
# include <stdio.h>
# include <conio.h>
# include <math.h>
# include <process.h>
# include <string.h>
//....defining formulae
# define f(x) 3*x -cos(x)-1
# define df(x) 3+sin(x)
//...Function Declaration prototype
void NEW_RAP();
//... Main Execution Thread
void main()
{
clrscr();
printf ("\n Solution by NEWTON RAPHSON method \n");
printf ("\n Equation is: ");
printf ("\n\t\t\t 3*X - COS X - 1=0 \n\n ");
NEW_RAP();
getch();
}
//...Function Declaration
void NEW_RAP()
{
//...Internal Declaration Field
long float x1,x0;
long float f0,f1;
long float df0;
int i=1;
int itr;
float EPS;
float error;
/*Finding an Approximate ROOT of Given Equation, Having
+ve Value*/
for(x1=0;;x1 +=0.01)
{
f1=f(x1);
if (f1 > 0)
{
break;
}
}
/*Finding an Approximate ROOT of Given Equation, Having
-ve value*/
x0=x1-0.01;
f0=f(x0);
\*****************************************************************************************************
Program to Implement Newton’s Forward Method of Interpolation
*********************************************************************************** ****************/
//... HEADER FILES DECLARATION
# include <stdio.h>
# include <conio.h>
# include <math.h>
# include <process.h>
# include <string.h>
//... MAIN EXECUTION THREAD
void main()
{
//... Variable declaration Field
//... Integer Type
int n; //... Number of terms
int i,j; //... Loop Variables
//...Floating Type
float ax[10]; //... array limit 9
float ay[10]; //... array limit 9
float x; //... User Querry
float y = 0; //... Initial value 0
float h; //... Calc. section
float p; //... Calc. section
float diff[20][20]; //... array limit 19,19
float y1,y2,y3,y4; //... Formulae variables
//... Invoke Function Clear Screen
clrscr();
//... Input Section
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<n;i++)
{
printf("\n\n Enter the value of x%d - ",i+1);
scanf("%f",&ax[i]);
}
Output
Enter the number of terms – 7
Enter the value in the form of x -
Enter the value of x1 - 100
Enter the value of x2 - 150
Enter the value of x3 - 200
Enter the value of x4 - 250
Enter the value of x5 - 300
Enter the value of x6 - 350
Enter the value of x7 - 400
Enter the value in the form of y -
Enter the value of y1 - 10.63
Enter the value of y2 - 13.03
Enter the value of y3 - 5.04
Enter the value of y4 - 16.81
Enter the value of y5 - 18.42
Enter the value of y6 - 19.9
Enter the value of y7 - 21.27
Enter the value of x for which you want the value of y-218
When X=218.0000, Y=15.69701481
Press Enter to Exit
\*****************************************************************************
Program to Implement Newton’s Backward Method of Interpolation
*************************************************************************** */
//...HEADER FILES DECLARATION
# include <stdio.h>
# include <conio.h>
# include <math.h>
# include <process.h>
# include <string.h>
//... MAIN EXECUTION THREAD
void main()
{
//...Variable declaration Field
//...Integer Type
int n; //...Number of terms
int i,j,k; //...Loop Variables
//...Floating Type
float my[10]; //... array limit 9
float my[10]; //... array limit 9
float x; //... User Querry
float x0 = 0; //... Initial value 0
float y0; //... Calc. Section
float sum; //... Calc. Section
float h; //... Calc. Section
float fun; //... Calc. Section
float p; //... Calc. Section
float diff[20][20]; //... array limit 19,19
float y1, y2, y3, y4; //... Formulae variables
//...Invoke Function Clear Screen
clrscr();
//...Input Section
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<n;i++)
{
printf("\n\n Enter the value of x%d - ",i+1);
scanf (“%f”,&mx[i]);
}
//...Input Sequel for array Y
printf ("\n\n Enter the value in the form of y -");
//...Input Loop for Y
for (i=0;i<n;i++)
Developed by : Anshul Siwach
Computer Oriented Numerical Techniques: 19
{
printf ("\n\n Enter the value of y%d - ",i+1);
scanf ("%f",&my[i]);
}
//...Inputting the required value query
printf ("\nEnter the value of x for");
printf("\nwhich you want the value of y - ");
scanf("%f",&x);
//...Calculation and Processing Section
h=mx[1]-mx[0];
for(i=0;i<n-1;i++)
{
diff[i][1]=my[i+1]-my[i];
}
for (j=2;j<=4;j++)
{
for (i=0;i<n-j;i++)
{
diff[i][j]=diff[i+1][j-1]-diff[i][j-1];
}
}
i=0;
while(!mx[i]>x)
{
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];
}
//...Output Section
printf ("\nwhen x=%6.4f,y=%6.8f",x,sum);
//...Invoke User Watch Halt Function
printf("\n\n\n Press Enter to Exit");
getch( );
}
//...Termination of Main Execution Thread
Output
Enter the number of terms-7
Enter the value in the form of x-
Enter the value of x1 - 100
Enter the value of x2 - 150
Enter the value of x3 - 200
Enter the value of x4 - 250
Enter the value of x5 - 300
Enter the value of x6 - 350
Enter the value of x7 - 400
Enter the value in the form of y -
Enter the value of y1 - 10.63
Enter the value of y2 - 13.03
Enter the value of y3 - 15.04
Enter the value of y4 - 16.81
Enter the value of y5 - 18.42
Enter the value of y6 - 19.90
Enter the value of y7 - 21.27
Enter the value of x for which you want the value of y - 410
When x = 410.0000, y = 21.34462738
Press Enter to Exit
/* *********************************************************************************
Program to Implement Gauss’s Forward Method of Interpolation
******************************************************************************** */
//...HEADER FILES DECLARATION
# include <stdio.h>
# include <conio.h>
# include <math.h>
# include <process.h>
# include <string.h>
//...MAIN EXECUTION THREAD
void main()
{
//...Variable declaration Field
//...Integer Type
int n;
int i,j;
//...Floating Type
float ax[10]; //...array limit 9
float ax[10]; //...array limit 9
float x;
float nr,dr;
float y=0; //...Initial value 0
float h;
float p;
float diff[20][20]; //...array limit 19,19
float y1,y2,y3,y4;
//...Invoke Function Clear Screen
clrscr();
//...Input Section
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 Array X
for (i=0;i<n;i++)
{
printf("\n\n Enter the value of x%d – ",i+i);
scanf("%f”,&ax[i]);
}
printf("\n\n Enter the value in the form of y – ");
//...Input Loop for Array Y
for(i=0;i<n;i++)
{
printf("\n\n Enter the value of y%d–",i+1);
scanf("%f",&ay[i]);
}
//...Inputting the required value query
Output
Enter the number of terms – 7
Enter the value in the form of x –
Enter the value of x1 – 1.00
Enter the value of x2 – 1.05
Enter the value of x3 – 1.10
Enter the value of x4 – 1.15
Enter the value of x5 – 1.20
Enter the value of x6 – 1.25
Enter the value of x7 – 1.30
Enter the value in the form of y –
Enter the value of y1 – 2.7183
Enter the value of y2 – 2.8577
Enter the value of y3 – 3.0042
Enter the value of y4 – 3.1582
Enter the value of y5 – 3.3201
Enter the value of y6 – 3.4903
Enter the value of y7 – 3.6693
Enter the value of x for
which you want the value of y – 1.17
When x = 1.17, y = 3.2221
Press Enter to Exit
/* ******************************************************************************
Program to Implement Gauss’s Backward Method of Interpolation
********************************************************************************/*
//...HEADER FILES DECLARATION
# include <stdio.h>
# include <conio.h>
# include <math.h>
# include <process.h>
# include <string.h>
//...MAIN EXECUTION THREAD
void main()
{
//...Variable declaration Field
//...Integer Type
int n; //... No. of terms
int i,j; //... Loop Variables
//...Floating Type
float ax[10]; //... array limit 9
float ay[10]; //... array limit 9
float x; //... User Querry
float y=0; //... Initial value 0
float h; //... Calc. section
float p; //... Calc. section
float diff[20][20]; //... array limit 19, 19
float y1,y2,y3,y4; //... Formulae variables
//... Invoke Function Clear Screen
clrscr();
//... Input Section
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<n;i++)
{
printf("\n\n Enter the value of x%d–",i+1);
scanf("%f”,&ax[i]);
}
//...Input Sequel for array Y
printf("\n\n Enter the value in the form of y–");
//...Input Loop for Y
for(i=0;i<n;i++)
{
printf("\n\n Enter the value of y%d–",i+1);
scanf("%f",&ay[i]);
}
Developed by : Anshul Siwach
Computer Oriented Numerical Techniques: 25
Output
Enter the number of terms – 7
Enter the value in the form of x –
Enter the value of x1 – 1.00
Enter the value of x2 – 1.05
Enter the value of x3 – 1.10
Enter the value of x4 – 1.15
Enter the value of x5 – 1.20
Enter the value of x6 – 1.25
Enter the value of x7 – 1.30
Enter the value in the form of y –
Enter the value of y1 – 2.1783
Enter the value of y2 – 2.8577
Enter the value of y3 – 3.0042
Enter the value of y4 – 3.1582
Enter the value of y5 – 3.3201
Enter the value of y6 – 3.4903
Enter the value of y7 – 3.6693
Enter the value of x for
which you want the value of y – 1.35
When x = 1.35, y=3.8483
Press Enter to Exit
/******************************************************************************
Program to Implement Stirling Method of Interpolation
****************************************************************************/*
//... HEADER FILES DECLARATION
#include<stdio.h>
#include<conio.h>
#include<math.h>
#include<process.h>
//...MAIN EXECUTION THREAD
void main()
{
//...Variable declaration Field
//...Integer Type
int n;
int i,j;
//...Floating Type
float ax[10]; //... array-limit 9
float ax[10]; //... array-limit 9
float h;
float p;
float diff[20][20]; //...array 2d-limit 19,19
float x,y;
float y1,y2,y3,y4;
clrscr(); //... Clear Screen
//... Input Section
printf("\n Enter the value of terms");
scanf("%d",%n);
//... Input Section Array X
printf(”\n Enter the values for x \n”);
//...Input Section Loop for X
for(i=0;i<n;i++)
{
printf("\n Enter the value for x%d-",i+1);
scanf("%f”,&ax[i]);
}
//... Input Section for Y
printf("\n Enter the values for y \n");
//... Input Section Loop for Y
for(i=0;i<n;i++)
{
printf("\n Enter the value for y%d-",i+1);
scanf("%f",&ay[i]);
}
//... Input Section Loop for Value of X for Y
printf("\n Enter the value of x for");
Output
Enter the value of terms-7
Enter the values for x
Enter the value for x1 - .61
Enter the value for x2 - .62
Enter the value for x3 - .63
Enter the value for x4 - .64
Enter the value for x5 - .65
Enter the value for x6 - .66
Enter the value for x7 - .67
Enter the values for y
Enter the value for y1 - 1.840431
Enter the value for y2 - 1.858928
Enter the value for y3 - 1.877610
Enter the value for y4 - 1.896481
Enter the value for y5 - 1.915541
Enter the value for y6 - 1.934792
Enter the value for y7 - 1.954237
Enter the value of x for
which you want the value of y - 0.6440
When x=0.6440,y=1.90408230
Press Enter to Continu
/* ***********************************************************************
Program to Implement Bessel’s Method of Interpolation
*********************************************************************** */
//...HEADER FILES DECLARATION
#include<stdio.h>
#include<conio.h>
#include<math.h>
#include<process.h>
//... MAIN EXECUTION THREAD
void main()
{
//...Variable declaration Field
//...Integer Type
int n;
int i,j;
//...Floating Type
float ax[10]; //...array – limit 9
float ay[10]; //...array – limit 9
float h;
float p;
float diff[20][20]; //... array 2d – limit
19, 19
float x,y;
float y1,y2,y3,y4,
//...Invoke Clear Screen Function
clrscr(); //... Clear Screen
//... Input Section
printf("\n Enter the number of terms");
scanf("%d",&n);
//... Input Section Array X
printf("\n Enter the values for x \n");
//... Input Section Loop for X
for(i=0;i<n;i++)
{
printf("\n Enter the value for x%d–",i+1);
scanf("%f,&ax[i]);
}
//... Input Section for Array Y
printf("\n Enter the values for y\n");
//...Input Section Loop for Y
for(i=0;i<n;i++)
{
printf("\n Enter the value for y%d–",i+1);
scanf("%f",&ay[i]);
}
//...Input Section Loop for Value Of X for Y
printf("\n Enter the value of x for ");
Developed by : Anshul Siwach
Computer Oriented Numerical Techniques: 31
Output
Enter the number of terms - 7
Enter the values of x
Enter the value of x1 - .61
Enter the value of x2 - .62
Enter the value of x3 - .63
Enter the value of x4 - .64
Enter the value of x5 - .65
Enter the value of x6 - .66
Enter the value of x7 - .67
Enter the values of y
Enter the value of y1 - 1.840431
Enter the value of y2 - 1.858928
Enter the value of y3 - 1.877610
Enter the value of y4 - 1.896481
Enter the value of y5 - 1.915541
Enter the value of y6 - 1.934792
Enter the value of y7 - 1.954237
Enter the value of x for
which you want the value of y - .644
When x = 0.644, y=1.90408230
Press Enter to Exit
/************************************************************************
Program to Implement Laplace Everett’s Method of Interpolation
***********************************************************************/
//... HEADER FILES DECLARATION
# include <stdio.h>
# include <conio.h>
# include <math.h>
# include <process.h>
# include <string.h>
//... MAIN EXECUTION THREAD
void main()
{
//... Variable declaration Field
//... Integer Type
int n;
int i,j;
//... Floating Type
float ax[10]; //... array limit 9
float ay[10]; //... array limit 9
float x;
float nr,dr;
float y=0; //... Initial value 0
float h;
float p,q;
float diff[20][20]; //... array limit 19,19
float y1,y2,y3,y4;
float py1,py2,py3,py4;
//... Invoke Function Clear Screen
clrscr();
//... Input Section
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 Array X
for (i=0;i<n;i++)
{
Printf("\n\n Enter the value of x%d - ",i+1);
scanf("%f",&ax[i]);
}
//... Input Sequel for Array X
printf ("\n\n Enter the value in the form of y - ");
//... Input Loop Array Y
for (i=0;i<n;i++)
{
printf ("\n\n Enter the value of y%d - ",i+1);
scanf("%f",&ay[i]);
}
//... Inputting the required value query
printf("\nEnter the value of x for ");
printf("\nwhich you want the value of y - ");
scanf("%f",&x);
Output
Enter the number of terms - 7
Enter the value in the form of x -
Enter the value of x1 - 1.72
Enter the value of x2 - 1.73
Enter the value of x3 - 1.74
Enter the value of x4 - 1.75
Enter the value of x5 - 1.76
Enter the value of x6 - 1.77
Enter the value of x7 - 1.78
Enter the value in the form of y -
Enter the value of y1 - .1790661479
Enter the value of y2 - .1772844100
Enter the value of y3 - .1755204006
Enter the value of y4 - .1737739435
Enter the value of y5 - .1720448638
Enter the value of y6 - .1703329888
Enter the value of y7 - .1686381473
Enter the value of x for
which you want the value of y - 1.7475
When x = 1.7475, y = 0.17420892
Press Enter to Exit
/*****************************************************************************
Program to Implement Lagrange’s Method of Interpolation
**************************************************************************** */
//... HEADER FILES DECLARATION
# include <stdio.h>
# include <conio.h>
# include <math.h>
# include <process.h>
# include <string.h>
//... MAIN EXECUTION THREAD
void main()
{
//... Variable declaration Field
//... Integer Type
int n; //... Number of terms
int i,j; //... Loop Variables
//... Floating Type
float ax[100]; //... array limit 99
float ay[100]; //... array limit 99
float x=0; //... User Querry
float y=0; //... Initial value 0
float nr; //... Calc. section
float dr; //... Calc. section
//... Invoke Function Clear Screen
clrscr();
//... Input Section
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<n;i++)
{
printf ("\n\n Enter the value of x%d - ", i+1);
scanf("%f",&ax[i]);
}
//... Input Sequel for array Y
printf("\n\n Enter the value in the form of y - ");
//... Input Loop for Y
for (i=0;i<n;i++)
{
printf("\n\n Enter the value of y%d - ", i+1);
scanf ("%f",&ay[i]);
}
//... Inputting the required value query
Developed by : Anshul Siwach
Computer Oriented Numerical Techniques: 37
printf("\n Enter the value of x for ");
printf("\n which you want the value of y - ");
scanf("%f",&x);
//... Calculation & Processing Section
for(i=0;i<n;i++)
{
nr=1;
dr=1;
for(j=0;j<n;j++)
{
if(j!=i)
{
nr=nr*(x-ax[j]);
dr=dr*(ax[i]–ax[j]);
}
y+=(nr/dr)*ay[i];
}
}
//... Output Section
printf("\n\n When x=%5.2f,y=%5.2f ",x,y);
//... Invoke User Watch Halt Function
printf("\n\n\n Press Enter to Exit");
getch();
}
//... Termination of Main Execution Thread
Output
Enter the number of terms - 5
Enter the value in the form of x -
Enter the value of x1- 5
Enter the value of x2 - 7
Enter the value of x3 - 11
Enter the value of x4 - 13
Enter the value of x5 - 17
Enter the value in the form of y -
Enter the value of y1 - 150
Enter the value of y2 - 392
Enter the value of y3 - 1452
Enter the value of y4 - 2366
Enter the value of y5 - 5202
Enter the value of x for
Which you want the value of y - 9.0
When x = 9.00, y = 810.00
Press Enter to Exit
/*************************************************************************
PROGRAM TO IMPLEMENT TRAPEZOIDAL METHOD OF NUMERICAL INTEGRATION
*************************************************************************/
//... HEADER FILES DECLARATION
#include <stdio.h>
#include <conio.h>
#include <math.h>
#include <process.h>
#include <string.h>
//... Function Prototype Declaration
float fun(float);
//... Main Execution Thread
void main()
{
//... Variable Declaration Field
//... Floating Type
float result=1;
float a,b;
float h,sum;
//... Integer Type
int i,j;
int n;
//... Invoke Clear Screen Function
clrscr();
//... Input Section
//... Input Range
{
float temp;
temp = 1/(1+(x*x));
return temp;
}
//... Termination of Function Body
OUTPUT
OUTPUT
/************************************************************
PROGRAM TO IMPLEMENT SIMPSON’S 1/3 rd METHOD OF NUMERICAL
INTEGRATION
*********************************************************** */
//... HEADER FILES DECLARATION
# include <stdio.h>
# include <conio.h>
# include <math.h>
# include <process.h>
# include <string.h>
//... Function Prototype Declaration
float fun(float);
//... Main Execution Thread
void main()
{
//...Variable Declaration Field
//... Floating Type
float result=1;
float a,b;
float h,sum;
//... Integer Type
int i,j;
int n;
//... Invoke Clear Screen Function
clrscr();
//... Input Section
//...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 Number of Subintervals
printf("\n\n Enter number of subintervals - ");
scanf("%d",&n);
//... Calculation and Processing Section
h=(b-a)/n;
sum=0;
sum=fun(a)+4*fun(a+h)fun(b);
for(i=3;i<n;i+=2)
{
sum+=2*fun(a+(i-1)*h)+4*fun(a+i*h);
}
result=sum*h/3;
//... Output Section
printf("\n\n\n\n Value of the integral is %6.4f\t",result);
//... Invoke User Watch Halt Function
printf("\n\n\n Press Enter to Exit");
getch();
}
//... Termination of Main Execution Thread
/***********************************************
PROGRAM OF MODIFIED EULER`S METHOD
********************************************** */
#include<stdio.h>
#include<math.h>
#define F(x,y) (x-y)/(x+y)
main ()
{
int i,n,itr ;
float x[5],y[50],yc[50],h,yp,p,xn;
printf("\n Enter the values: x[1],y[1],h,xn:\n");
scanf("%f%f%f%f",&x[1],&y[1],&h,&xn);
yp=y[1]+h*F(x[1],y[1]);
itr=(xn-x[1])/h;
printf("\n\n X=%f Y=%f\n",x[1],y[1];
for (i=1;i<=itr;i++)
{
x[i+1]=x[i]+h;
for (n=1;n<=50;n++)
{
yc[n+1]=y[i]+(h/2.0)*(F(x[i],y[i])+F(x[i+1],yp));
printf("\nN=%d Y=%f",n,yc[n+1]);
p=yc[n+1]-yp;
if(fabs (p)<0.0001)
goto next;
else
yp=yc[n+1];
}
next:
y[i+1]=yc[n+1];
printf("\n\n X=%f Y=%f\n",x[i+1], yp);
}
return;
}
Output
Enter the values: x[1],y[1],h,xn:
0 1 0.02 0.06
X=0.000000 Y=1.000000
N=1 Y=0.980400
N=2 Y=0.980400
X=0.020000
Y=0.980400
N=1 Y=0.961584
N=2 Y=0.961598
X=0.040000 Y=0.961584
Y=0.943572
N=1
N=2 Y=0.943593
X=0.060000
Y=0.943572
/*************************************************
PROGRAM TO IMPLEMENT RUNGE-KUTTA METHOD
**************************************************/
#include<stdio.h>
#define F(x,y) (x-y)/(x+y)
main()
{
int i,n;
float x0,y0,h,xn,k1,k2,k3,k4,x,y,k;
printf("\n Enter the values: x0,y0,h,xn:\n");
scanf("%f%f%f%f", &x0,&y0,&h,&xn);
n=(xn-x0)/h;
x=x0;
y=y0;
for(i=0;i<=n;i++)
{
k1=h*F(x,y);
k2=h*F(x+h/2.0,y+k1/2.0);
k3=h*F(x+h/2.0,y+k2/2.0);
k4=h*F(x+h,y+k3);
k=(k1+(k2+k3)*2.0+k4)/6.0;
printf("\n X=%f Y=%f", x, y);
x=x+h;
y=y+k;
}
return;
}
Output
Enter the values: x0,y0,h,xn:
0 1 0.02 0.1
X=0.000000 Y=1.000000
X=0.020000 Y=0.980000
X=0.040000 Y=0.960816
X=0.060000 Y=0.942446
X=0.080000 Y=0.924885
X=0.100000 Y=0.908128
/*********************************************
PROGRAM TO IMPLEMENT MILNE`S METHOD
******************************************** */
Program of Milne’s Method
#include<stdio.h>
#include<math.h>
#define F(x,y) x+y
main()
{
int i,n;
float x[20],y[20],h,f,f1,f2,yp,yc,xn;
printf("\n Enter the value: xn: "};
scanf{"%f",&xn);
printf("\n Enter the value: x{i], y[i]:\n"};
for(i=0;i<=3;i++)
scanf("%f%f",&x[i],&y[i]);
h=x[1]-x[0];
n=(xn-x[0]/h;
for(i=3;i<=n;i++)
{
x[i+1]=x[i]+h;
f=F[x[i],y[i]);
f1=F(x[i-1],y[i-1]);
f2=F(x[i-2],y[i-2]);
yp=y[i-3]+4.0*h/3.0*(2.0*f2-f1+2.0*f);
yc=y[i-1]+h/3.0*(f1+4.0*f+F(x[i+1],yp));
printf("\n\nPredicated Y=%f Correctd Y=%f", yp,yc);
If(fabs (yp-yc)<0.00005)
goto next;
yp=yc;
next;
y[i+1]=yc;
printf("\n\n X=%f Y=%f", x[i+1], y[i+1]);
}
return;
}
Output
Enter the value: xn: 1
Enter the value: x[i], y[i]:
0.0 0.0
0.2 0.02
0.4 0.0906
0.6 0.2214
Predicted Y=0.423147 Corrected Y=0.429650
X=0.800000 Y=0.429650
Predicted Y=0.721307 Corrected Y=0.718820
X=1.000000 Y=0.718820
/**********************************************************************
PROGRAM TO IMPLEMENT CURVE FITTING TO FIT A STRAIGHT LINE
**********************************************************************/
//... HEADER FILE DECLARATION
# include <stdio.h>
# include <conio.h>
# include <math.h>
//... Main Execution Thread
void main()
{
//... Variable Declaration Field
//... Integer Type
int i=0;
int observ;
//... Floating Type
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 Type
double a;
double b;
//... Invoke Function Clear Screen
clrscr ();
//... Input Section
//... Input Number of Observations
printf(“\n\n Enter the number of observations - ”);
scanf(“%d” ,&observ);
//... Input Sequel For Array X
printf(“\n\n\n Enter the values of x – \n");
for (;i<observ;i++)
{
printf("\n\n Enter the Value of x%d: ",i+1);
scanf(“%f” ,&x[i]);
sum1 +=x[i];
}
//... Input Sequel For Array Y
printf(“\n\n Enter the values of y - \n”);
for(i=0;i<observ;i++)
{
printf("\n\n Enter the value of y%d:",i+1);
scanf("%f",&y[i]);
sum2+=y[i];
}
//... Processing and Calculation Section
for(i=0;i<observ;i++)
{
xy[i]=x[i]*y[i];
sum3 +=xy[i];
}
for(i=0;i<observ; i++)
{
x2[i]=x[i]*x[i];
sum4+ =x2[i];
}
a=(sum2*sum4–sum3*sum1)/(observ*sum4–sum1*sum1); b=(sum2–
observ*a)/sum1;
//... Output Section
printf(“\n\n\n\n Equation of the STRAIGHT LINE");
printf("of the form y = a + b*x is:");
printf(“\n\n\n \t\t\t Y = %.2f + (%.2f) X", a,b);
//... Invoke User Watch Halt Function
printf("\n\n\n Press Enter to Exit");
getch();
}
//... Termination of Main Execution Thread
float u[3][4], x,y,x2,p,a,b,c;
printf("\nEnter the value of data set n:");
scanf("%d",&n);
for(i=0; i<3; i++)
for(j=0; j<4; j++)
u[i][j]=0;
u[0][0]=n;
printf("\nEnter the value of x & y:\n");
for(i=0; i<n; i++)
{
scanf("%f%f", &x, &y);
x2=x*x;
u[0][1]+=x;
u[0][2]+=x2;
u[1][2]+=x*x2;
u[2][2]+=x2*x2;
u[0][3]+=y;
u[1][3]+=x*y;
u[2][3]+=x2*y;
}
u[1][1]=u[0][2];
u[2][1]=u[1][2];
u[1][0]=u[0][1];
u[2][0]=u[1][1];
/* Finding the value of a,b,c */
for (j=0;j<3;j++)
for (i=0;i<3;i++)
if(i!=j)
{
p=u[i][j]/u[j][j];
for(k=0;k;k++)
u[i][k]-=u[j][k]*p;
}
Developed by : Anshul Siwach
Computer Oriented Numerical Techniques: 49
a=u[0][3]/u[0][0];
b=u[1][3]/u[1][1];
c=u[2][3]/u[2][2];
printf("\na=%f b=%f c=%f ", a,b,c);
printf("\n\nEquation of parabola is: y=a+bx+cx^2 \n");
printf("\ny=%f+(%f)x+(%f)x^2",a,b,c);
return;
}
Output
Enter the value of data set n: 5
Enter the value of x & y:
1 10.9
2 12.2
3 13.9
4 16.3
5 19.2
a=10.239998 b=0.398574 c=0.278571
Equation of parabola is: y = a+bx+cx^2
y=10.239998+(0.398574)x+(0.278571)x^2