CBNMST Assignment 1-2
CBNMST Assignment 1-2
Assignment 1
Academic Year 2023-24 (EVEN Semester)
Signature: ……………………………
CBNST ASSIGNMENT---1
#include<iostream>
#include<iomanip>
#include<math.h>
int main() {
int secder = 0;
int ans = 0;
int n;
cin >> n;
int t;
int pow = i;
int c;
cout << "enter the coeffiecient for power " << i <<
}
cout << "the first derivative of function is :" << ans << std::endl;
cout << "the double derivative of function is : " << secder <<
std::endl;
return 0;
OUTPUT:-
enter point at which u want the
derivative :1 enter the degree of polynomial
:4
enter the coeffiecient for power
0:7 enter the coeffiecient for
power 1:1 enter the coeffiecient
for power 2:6 enter the
coeffiecient for power 3:3 enter
the coeffiecient for power 4:5 the
first derivative of function is :42
the double derivative of function is : 90
#include<iostream>
#include<iomanip>
#include<math.h>
/*
*/
int main()
step = 1;
/* Inputs */
up:
cin>>x0;
cin>>x1;
cin>>e;
f0 = f(x0);
f1 = f(x1);
cout<<"****************"<< endl;
do
{
/* Bisecting Interval */
x = (x0 + x1)/2;
f = f(x);
if( f0 * f < 0)
x1 = x;
}
else
{
x0 = x;
step = step + 1;
}while(fabs(f)>e);
return 0;
OUTPUT:-
Enter first guess: 1
Enter second guess: 2
Enter tolerable error: 0.0001
****************
Bisection Method
****************
Iteration-1: x = 1.500 and f(x) = 0.875
Iteration-2: x = 1.250 and f(x) = -0.297
Iteration-3: x = 1.375 and f(x) = 0.225
Iteration-4: x = 1.312 and f(x) = -0.052
Iteration-5: x = 1.344 and f(x) = 0.083
Iteration-6: x = 1.328 and f(x) = 0.015
Iteration-7: x = 1.320 and f(x) = -0.019
Iteration-8: x = 1.324 and f(x) = -0.002
Iteration-9: x = 1.326 and f(x) = 0.006
Iteration-10: x = 1.325 and f(x) = 0.002
Iteration-11: x = 1.325 and f(x) = -0.000
2.Find a root of the equation f(x)= 4e-x sin x -1=0 by Bisection method
correct to three decimal places.
#include<iostream>
#include<iomanip>
#include<math.h>
/*
*/
int main()
step = 1;
/* Inputs */
up:
cin>>x0;
cin>>x1;
cin>>e;
f0 = f(x0);
f1 = f(x1);
}
/* Implementing Bisection Method */ cout<<
endl<<"****************"<< endl;
cout<<"****************"<< endl;
do
/* Bisecting Interval */
x = (x0 + x1)/2;
f = f(x);
if( f0 * f < 0)
x1 = x;
else
x0 = x;
step = step + 1;
}while(fabs(f)>e);
return 0;
OUTPUT:-
Enter first guess: 1
Enter second guess:
2
Enter tolerable error: 0.0001
****************
Bisection Method
****************
Iteration-1: x = 1.500 and f(x) = -0.110
Iteration-2: x = 1.250 and f(x) = 0.088
Iteration-3: x = 1.375 and f(x) = -0.008
Iteration-4: x = 1.312 and f(x) = 0.041
Iteration-5: x = 1.344 and f(x) = 0.017
Iteration-6: x = 1.359 and f(x) = 0.004
Iteration-7: x = 1.367 and f(x) = -0.002
Iteration-8: x = 1.363 and f(x) = 0.001
Iteration-9: x = 1.365 and f(x) = -0.000
Iteration-10: x = 1.364 and f(x) = 0.001
Iteration-11: x = 1.365 and f(x) = 0.000
Iteration-12: x = 1.365 and f(x) = -0.000
#include<iostream>
#include<iomanip>
#include<math.h>
/*
*/
int main()
{
/* Declaring required variables */
step = 1;
/* Inputs */
cin>>x0;
cin>>x1;
cin>>e;
f0 = f(x0);
f1 = f(x1);
}
/* Implementing False Position Method */ cout<<
endl<<"*********************"<< endl;
cout<<"*********************"<< endl;
do
f = f(x);
if( f0 * f < 0)
x1 = x;
f1 = f;
else
x0 = x;
f0 = f;
step = step + 1;
}while(fabs(f)>e);
return 0;
OUTPUT:-
Enter first guess: 3
Enter second guess:
4
Enter tolerable error: 0.0001
*********************
False Position Method
*********************
Iteration-1: x = 3.788 and f(x) = -0.003
Iteration-2: x = 3.789 and f(x) = -0.000
#include<iomanip>
#include<math.h>
/*
*/
int main()
step = 1;
/* Inputs */
cin>>x0;
cin>>x1;
cin>>e;
f1 = f(x1);
}
/* Implementing False Position Method */ cout<<
endl<<"*********************"<< endl;
cout<<"*********************"<< endl;
do
{
/* Applying False Position Method */
f = f(x);
if( f0 * f < 0)
x1 = x;
f1 = f;
}
else
x0 = x;
f0 = f;
step = step + 1;
}while(fabs(f)>e);
cout<< endl<<"Root is: "<< x<< endl;
return 0;
OUTPUT:-
Enter first guess: 0
Enter second guess: 0.5
Enter tolerable error: 0.0001
*********************
False Position Method
*********************
0.430 and f(x) = 0.085
Iteration-1: x =
Iteration-2: x = 0.396 and f(x) = 0.039
Iteration-3: x = 0.382 and f(x) = 0.017
Iteration-4: x = 0.375 and f(x) = 0.007
Iteration-5: x = 0.372 and f(x) = 0.003
Iteration-6: x = 0.371 and f(x) = 0.001
Iteration-7: x = 0.371 and f(x) = 0.001
Iteration-8: x = 0.371 and f(x) = 0.000
Iteration-9: x = 0.371 and f(x) = 0.000
#include<iostream>
#include<iomanip>
#include<math.h>
#include<stdlib.h>
is to be solved */
#define f(x) x*exp(x)-1
int main()
int step=1, N;
/* Inputs */
cin>>x0;
cin>>e;
cin>>N;
cout<< endl<<"**************************"<<
endl; cout<<"**************************"<<
endl; do
x1 = g(x0);
step = step + 1;
if(step>N)
{
cout<<"Not Convergent.";
exit(0);
}
x0 = x1;
return(0);
OUTPUT:-
Enter initial guess: 0.5
Enter tolerable error:
0.0001 Enter maximum
iteration: 36
**************************
Fixed Point Iteration Method
**************************
Iteration-1: x1 = 0.607 and f(x1) = 0.112
Iteration-2: x1 = 0.545 and f(x1) = -0.059
Iteration-3: x1 = 0.580 and f(x1) = 0.035
Iteration-4: x1 = 0.560 and f(x1) = -0.019
Iteration-5: x1 = 0.571 and f(x1) = 0.011
Iteration-6: x1 = 0.565 and f(x1) = -0.006
Iteration-7: x1 = 0.568 and f(x1) = 0.004
Iteration-8: x1 = 0.566 and f(x1) = -0.002
Iteration-9: x1 = 0.568 and f(x1) = 0.001
Iteration-10: x1 = 0.567 and f(x1) = -0.001
Iteration-11: x1 = 0.567 and f(x1) = 0.000
Iteration-12: x1 = 0.567 and f(x1) = -0.000
Iteration-13: x1 = 0.567 and f(x1) = 0.000
Iteration-14: x1 = 0.567 and f(x1) = -0.000
Root is 0.567
7. Use the method of iteration to find a real root of the equation sin x
=10(x-1). Give your answer correct to three decimal places.
#include<iostream>
#include<iomanip>
#include<math.h>
#include<stdlib.h>
is to be solved */
int main()
int step=1, N;
/* Inputs */
cin>>x0;
cin>>e;
cin>>N;
cout<<"**************************"<< endl;
do
x1 = g(x0);
step = step + 1;
if(step>N)
cout<<"Not Convergent.";
exit(0);
x0 = x1;
return(0);
OUTPUT:-
Enter initial guess: 1
Enter tolerable error: 0.0001
Enter maximum iteration: 9
**************************
Fixed Point Iteration Method
**************************
Iteration-1: x1 = 1.084 and f(x1) = 0.042
Iteration-2: x1 = 1.088 and f(x1) = 0.002
Iteration-3: x1 = 1.089 and f(x1) = 0.000
Root is 1.089
#include<iostream>
#include<iomanip>
#include<math.h>
#include<stdlib.h>
int main()
int step = 1, N;
/* Inputs */
cin>>x0;
cout<<"Enter tolerable error: ";
cin>>e;
cin>>N;
cout<<"*********************"<< endl;
do
g0 = g(x0);
f0 = f(x0);
if(g0 == 0.0)
cout<<"Mathematical Error.";
exit(0);
x1 = x0 - f0/g0;
step = step+1;
if(step > N)
cout<<"Not Convergent.";
exit(0);
f1 = f(x1);
}while(fabs(f1)>e);
return 0;
OUTPUT:-
Enter initial guess: 1.57
Enter tolerable error: 0.0001
Enter maximum iteration: 9
*********************
Newton Raphson Method
*********************
Iteration-1: x = 2.001 and f(x) = -0.183
Iteration-2: x = 1.901 and f(x) = -0.009
Iteration-3: x = 1.896 and f(x) = -0.000
#include<iostream>
#include<iomanip>
#include<math.h>
#include<stdlib.h>
int main()
int step = 1, N;
/* Inputs */
cin>>x0;
cin>>e;
cin>>N;
cout<< endl<<"*********************"<<
endl; cout<<"*********************"<<
endl; do
g0 = g(x0);
f0 = f(x0);
if(g0 == 0.0)
cout<<"Mathematical Error.";
exit(0);
}
x1 = x0 - f0/g0;
step = step+1;
if(step > N)
cout<<"Not Convergent.";
exit(0);
f1 = f(x1);
}while(fabs(f1)>e);
return 0;
}
OUTPUT:-
Enter initial guess: 1 Enter
tolerable error: 0.001 Enter
maximum iteration: 9
*********************
Newton Raphson Method
*********************
Iteration-1: x = -1.5574 and f(x) = 1.5707
Iteration-2: x = 73.7713 and f(x) = -73.7114
Iteration-3: x = 55.9449 and f(x) = -30.9385
Iteration-4: x = 56.6167 and f(x) = 4.8464
Iteration-5: x = 56.5309 and f(x) = -0.0046
Iteration-6: x = 56.5310 and f(x) = -0.0001
10. Using Muller’s method, find the root of the equation f(x)=x3-2x-5=0
which lies between 2 and 3.
#include<iomanip>
#include<math.h>
int i;
float res;
for (i = 0;;++i)
/ to calculate x3
float f1 = f(a);
float f2 = f(b);
float f3 = f(c);
float d1 = f1 - f3;
float d2 = f2 - f3;
float h1 = a - c;
float h2 = b - c;
float a0 = f3;
/ ((h1*h2) * (h1-h2)));
float a2 = (((d1*h2) - (d2*h1))/((h1*h2) * (h1-h2)));
res = x + c;
else
res = y + c;
float m = res*100;
float n = c*100;
m = floor(m);
n = floor(n);
if (m == n)
break;
a = b; b
= c; c =
res;
if (i > MAX_ITERATIONS)
break;
}
if (i <= MAX_ITERATIONS)
int main()
float a,b,c;
cin>>a;
cin>>b;
cin>>c;
Muller(a, b, c);
return 0;
}
OUTPUT:-
enter value of a :
1 enter value of b
: 2 enter value of c : 3
The value of the root is 2.09455
11. Using Muller’s method, find the root of the equation f(x)=x3+2x2+10x-
20=0 by taking x=0, x=1, x=2 as initial approximations
/ C++ Program to find root of a function, f(x) #include<iostream>
#include<iomanip>
#include<math.h>
int i;
float res;
for (i = 0;;++i)
/ to calculate x3
float f1 = f(a);
float f2 = f(b);
float f3 = f(c);
float d1 = f1 - f3;
float d2 = f2 - f3;
float h1 = a - c;
float h2 = b - c;
float a0 = f3;
float a1 = (((d2*pow(h1, 2)) - (d1*pow(h2, 2)))
/ ((h1*h2) * (h1-h2)));
else
res = y + c;
float m = res*100;
float n = c*100;
m = floor(m);
n = floor(n);
if (m == n)
break;
a = b; b
= c; c =
res;
if (i > MAX_ITERATIONS)
break;
if (i <= MAX_ITERATIONS)
int main()
float a,b,c;
cin>>a;
cin>>b;
cout<<"enter value of c : ";
cin>>c;
Muller(a, b, c);
return 0;
OUTPUT:-
enter value of a :
1 enter value of b
: 2 enter value of c : 3
The value of the root is 1.36881
12. Find the real roots of the equation x3-6x2+11x-6=0 using Quotient-
difference method
#include <iostream>
#include <cmath>
{ if (i != k) {
}
return result;
{ double newRoots[n];
maxError = error;
}
roots[j] = newRoots[j];
std::cout << "Root" << j + 1 << " = " << roots[j] << ", ";
break;
int main() {
std::cout << "Initial roots: Root1 = " << roots[0] << ", Root2 = "
<< roots[1] << ", Root3 = " << roots[2] << std::endl;
quotientDifferenceMethod(roots, n);
return 0;
OUTPUT:-
Initial roots: Root1 = -2, Root2 = 0, Root3 = 2
Iteration 1: Root1 = 5.5, Root2 = -1.5, Root3 = 2, Max Error = 7.5
Iteration 2: Root1 = 3.89286, Root2 = 0.107143, Root3 = 2, Max Error = 1.60714
Iteration 3: Root1 = 3.21058, Root2 = 0.78942, Root3 = 2, Max Error = 0.682278
Iteration 4: Root1 = 3.01832, Root2 = 0.981685, Root3 = 2, Max Error = 0.192264
Iteration 5: Root1 = 3.00016, Root2 = 0.999835, Root3 = 2, Max Error = 0.0181504
Iteration 6: Root1 = 3, Root2 = 1, Root3 = 2, Max Error = 0.000164691
Iteration 7: Root1 = 3, Root2 = 1, Root3 = 2, Max Error = 1.35616e-08
#include <cmath>
{ if (i != k) {
return result;
maxError = error;
roots[j] = newRoots[j];
std::cout << "Root" << j + 1 << " = " << roots[j] << ", ";
break;
int main() {
/ Initial guesses for the roots double roots[] = {-2.0, 0.0, 2.0};
std::cout << "Initial roots: Root1 = " << roots[0] << ", Root2 = "
<< roots[1] << ", Root3 = " << roots[2] << std::endl;
quotientDifferenceMethod(roots, n);
return 0;
OUTPUT:-
Initial roots: Root1 = -2, Root2 = 0, Root3 = 2
Iteration 1: Root1 = 5.25, Root2 = -0.5, Root3 = 2.25,
Max Error = 7.25
Iteration 2: Root1 = 5.11866, Root2 = 0.0612648, Root3 =
1.82008, Max Error = 0.561265
Iteration 3: Root1 = 5.125, Root2 = 0.220163, Root3 =
1.65484, Max Error = 0.16524
Iteration 4: Root1 = 5.12489, Root2 = 0.238211, Root3
= 1.6369, Max Error = 0.0180484
Iteration 5: Root1 = 5.12489, Root2 = 0.238443, Root3
= 1.63667, Max Error = 0.000232006
Iteration 6: Root1 = 5.12489, Root2 = 0.238443, Root3
= 1.63667, Max Error = 3.84597e-08
Assignment-2
1. Construct the forward difference table for a given set of data values
x 5 10 15 20 25 30
y 9962 9848 9659 9397 9063 8660
2. Construct the backward difference table for a given set of data values
x 0 1 2 3
y 1 2 1 10
3. The population of a town was as given below. Estimate the population for the year
1895.
Year (x) 1891 1901 1911 1921 1931
Population (y) 46 66 81 93 101
(In thousands)
4. The population of a town was as given below. Estimate the population for the year
1925.
Year (x) 1891 1901 1911 1921 1931
Population (y) 46 66 81 93 101
(In thousands)
5. Apply Gauss’ forward central difference interpolation formula to obtain y(9) given
that:
x 0 4 8 12 16
y 14 24 32 35 40
6. Apply Gauss’ backward interpolation formula to find the population for the year 1936
given that
Year (x) 1901 1911 1921 1931 1941 1951
Population (y) 12 15 20 27 39 52
(In thousands)
7. Given
0 0
θ 0 5 100 150 200 250 300
tanθ 0 0.0875 0.1763 0.2679 0.364 0.4663 0.5774
0
Find the value of tan 16 using Stirling interpolation formula.
8. Probability distribution function values of a normal distribution are given as follows:
x 0.2 0.6 1.0 1.4 1.8
p(x) 0.39104 0.33322 0.24197 0.14973 0.07895
Apply Bessel’s formula to find the value of p(1.2).
9. Apply Everett’s interpolation formula to find the value of f(26) and f(27) from the
given table:
x 15 20 25 30 35 40
f(x) 12.849 16.351 19.524 22.396 24.999 27.356
10. Using Lagrange’s interpolation formula, find the value of f (1) from the following
table:
x -1 0 2 3
f(x) -8 3 1 12
11. Using Newton’s divided difference interpolation formula, find the values of f (8) and
f(15) from the following table:
x 4 5 7 10 11 13
f(x) 48 100 294 900 1210 2028
CBNST ASSIGNMENT---2
Q1) Construct the forward difference table for a given set of data
values :
X 5 10 15 20 25 30
#include<iostream>
int main()
int i,j, n;
/* Input Section */
return 0;
OUTPUT:-
Q2) Construct the backward difference table for a given set of data
values:
X 0 1 2 3
Y 1 2 1 10
#include<iostream>
int main()
int i,j, n;
/* Input Section */
}
/* Generating Backward Difference Table
return 0;
OUTPUT:-
Enter number of data?
4
Enter data:
x[0] = 0
y[0] = 1
x[1] = 1
y[1] = 2
x[2] = 2
y[2] = 1
x[3] = 3
y[3] = 10
BACKWARD DIFFERENCE
TABLE 0 1
1 2 1
2 1 -1 -2
3 10 9 10 12
POPULATIO 46 66 81 93 101
N(Y)
(IN
THOUSAND
S)
#include<iostream>
#include<iomanip>
#define ORDER 4
int main()
float ax[MAXN+1],ay[MAXN+1],diff[MAXN+1]
n,i,j,k;
cin>>n;
for(i=0;i<n;i++)
cin>>ax[i]>>ay[i];
cout<<"Enter the value of x";
cin>>x;
cout<<fixed;
h=ax[1]-ax[0];
for(i=0;i<n-1;i++)
diff[i][1]=ay[i+1]-ay[i];
for(j=2;j<=ORDER;j++)
for(i=0;i<n-j;i++)
diff[i][j]=diff[i+1][j-1]-diff[i][j-1];
//Finding x0
while (!(ax[i]>x))
i++;
i--;
p=(x-ax[i])/h;
yp=ay[i];
for(k=1;k<=ORDER;k++)
nr += p-k+1;
dr *= k;
yp += (nr/dr)*diff[i][k];
cout<<"When x="<<setw(6)<<setprecision(1)<<x;
cout<<" y="<<setw(6)<<setprecision(2)<<yp<<endl;
return 0;
OUTPUT:-
4. The population of a town was as given below. Estimate the population for the year
1925
Population
(y) 46 66 81 93 101
(In
thousands)
#include<iostream>
#include<iomanip>
#include<math.h>
int main(){
float ax[maxn+1],ay[maxn+1],diff[maxn+1][order+1],nr=1,dr=1,x,p,h,yp;
int n,i,j,k;
cin>>n;
for(i=1;i<=n;i++)
{cin>>ax[i]>>ay[i];}
cin>>x;
h=ax[2]-ax[1];
for(i=n;i>=1;i--)
{diff[i][1]=ay[i]-ay[i-1];}
for(j=2;j<=order;j++)
for(i=n;i>j;i--)
{diff[i][j]=diff[i][j-1]-diff[i-1][j-1];}
i=n;
p=(x-ax[i])/h;
yp=ay[i];
for(k=1;k<=order;k++)
{
nr*=p+k-1;
dr*=k;
yp+=(nr/dr)*diff[i][k];
OUTPUT:-
enter the value of n
5
enter the values in form x,y
1891 46
1901 66
1911 81
1921 93
1931 101
enter the value of x for which value of y is
wanted 1925
when x = 1925 y = 96.8368
x 0 4 8 12 16
y 14 24 32 35 40
#include<iostream>
#include<math.h>
int main()
int n;
int i,j;
float ax[10];
float ay[10];
float x;
float nr,dr;
float y=0;
float h;
float p;
float diff[20][20];
float y1,y2,y3,y4;
cout<<"GAUSS' FORWARD INTERPOLATION FORMULA ";
cin>>n;
for(i=0;i<n;i++)
cin>>ax[i];
for(i=0;i<n;i++)
; cin>>ay[i];
"; cin>>x;
h=ax[1]-ax[0];
for(i=0;i<n-1;i++)
diff[i][1]=ay[i+1]-ay[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;
do
i++;
}
while(ax[i]<x);
i--;
p=(x-ax[i])/h;
y1=p*diff[i][1];
y2=p*(p-1)*diff[i-1][2]/2;
y3=(p+1)*p*(p-1)*diff[i-2][3]/6;
y4=(p+1)*p*(p-1)*(p-2)*diff[i-3][4]/24;
y=ay[i]+y1+y2+y3+y4;
cout<<"\t\t"<<x<<"\t\t"<<y;
OUTPUT:-
#include <iostream>
#include <cmath>
int main()
int n;
int i, j;
float ax[10];
float ay[10];
float x;
float y = 0;
float h;
float p;
float diff[20][20];
cout << "\nEnter the value of x for which you want the value of y -
h = ax[1] - ax[0];
{
for (i = 0; i < n - j; i++)
{
diff[i][j] = diff[i + 1][j - 1] - diff[i][j - 1];
i = 0;
do
i++;
x); i--;
p = (x - ax[i]) / h;
y1 = p * diff[i - 1][1];
y2 = p * (p + 1) * diff[i - 1][2] / 2;
y3 = (p + 1) * p * (p - 1) * diff[i - 2][3] / 6;
y4 = (p + 2) * (p + 1) * p * (p - 1) * diff[i - 3][4] /
cout << "\nwhen x = " << x << ", y = " << y << endl;
return 0;
OUTPUT:-
x4 - 1931
Enter the value of x5 - 1941
of y5 - 39 Enter the
value of y6 - 52
7. Given
x(degrees) 0 5 10 15 20 25 30
Y=tan(x) 0 0.0875 0.1763 0.2679 0.364 0.4664 0.5774
#include <iostream>
#include <cmath>
int main()
int n;
int i, j;
float ax[10];
float ay[10];
float h;
float p;
float diff[20][20];
float x, y;
float y1,y2,y3,y4;
cin >> n;
cout << "Enter the value for x" << i + 1 << ":
cout << "Enter the value for y" << i + 1 << ":
cout << "Enter the value of x for which you want the value of y:
h = ax[1] - ax[0];
i = 0;
do
i++;
i--;
p = (x - ax[i]) / h;
y2 = p * p * diff[i - 1][2] / 2;
y = ay[i] + y1 + y2 + y3 + y4;
cout << "When x = " << x << ", y = " << y << endl;
return 0;
OUTPUT:-
#include <iostream>
#include <cmath>
int main() {
int n; // no. of terms.
// Input section.
cout << "\n\n Enter the no. of terms ->
cout << "\n\n Enter the value in the form of x -> ";
cout << "\n Enter the value of x" << i + 1 << " ->
cout << "\n\n Enter the value in the form of y -> ";
cout << "\n Enter the value of y" << i + 1 << " ->
h = ax[1] - ax[0];
i = 0;
do {
i++;
x); i--;
p = (x - ax[i]) / h;
y1 = p * diff[i][1];
// Taking sum
y = ay[i] + y1 + y2 + y3 + y4;
// Output Section
cout << "\n When x = " << x << " , y = " << y << endl;
return 0;
OUTPUT:-
x 15 20 25 30 35 40
y=f(x) 12.849 16.351 19.524 22.396 24.999 27.356
# include <iostream>
# include <math.h> # include <string.h> #include<iomanip>
int main()
int n;
int i,j;
ay[10]; float x;
float nr,dr;
float y=0;
float h;
float p,q;
float diff[20][20];
float y1,y2,y3,y4;
float py1,py2,py3,py4;
// Input section.
cout << "\n\n Enter the value in the form of x -> ";
cout << "\n\n Enter the value in the form of y -> ";
cout << "\n Enter the value of y" << i + 1 << " ->
h=ax[1]-ax[0];
for(i=0;i<n-1;i++)
diff[i][1]=ay[i+1]-ay[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;
do {
i++;
}while(ax[i]<x);
i--;
p=(x-ax[i])/h;
q=1-p;
y1=q*(ay[i]);
y2=q*(q*q-1)*diff[i-1][2]/6;
y3=q*(q*q-1)*(q*q-4)*(diff[i-2][4])/120;
py1=p*ay[i+1];
py2=p*(p*p-1)*diff[i][2]/6; py3=p*(p*p-1)*(p*p-4)*(diff[i-1][4])/120;
y=y1+y2+y3+y4+py1+py2+py3;
cout << "\n When x = " << setprecision(9) << x << " , y = "
<< setprecision(9)<< y << endl;
return 0; }
OUTPUT:-
Enter the no. of terms -> 6
When x = 26 , y = 20.1214314
OUTPUT:-
Enter the no. of terms -> 6
When x = 27 , y = 20.7070789
10. Using Lagrange’s interpolation formula, find the value of f (1) from
the following table:
x -1 0 2 3
y=f(x) -8 3 1 12
#include<iostream>
#include<iomanip>
#include<math.h>
int main()
int i,j,n;
/* Input Section */
cin>>n;
for(i=1;i<=n;i++)
cin>>xp;
for(i=1;i<=n;i++)
p=1;
for(j=1;j<=n;j++)
if(i!=j)
yp = yp + p * y[i];
return 0;
OUTPUT:-
Enter number of value:
4 Enter data:
x[1] = -1
y[1] = -8
x[2] = 0
y[2] = 3
x[3] = 2
y[3] = 1
x[4] = 3
y[4] = 12
Enter interpolation point:
1 Interpolated value at 1
is 2
11. Using Newton’s divided difference interpolation formula, find the
values of f
(8) and f(15) from the following table:
x 4 5 7 10 11 13
y=f(x) 48 100 294 900 1210 2028
#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
int x[10],y[10],p[10];
int k,f,n,i,j=1,f1=1,f2=0;
n"; cin>>n;
for(i=1;i<=n;i++)
{
cin>>x[i];
}
for(i=1;i<=n;i++)
{
cin>>y[i];
}
f=y[1];
n"; cin>>k;
do
{
for(i=1;i<=n-1;i++)
{
p[i]=((y[i+1]-y[i])/(x[i+j]-x[i]));
y[i]=p[i];
}
for(i=1;i<=j;i++)
{
f1 *= (k-x[i]);
}
f2 += (y[1]*f1);
f1=1;
n--;
j++;
}while(n!=1);
f += f2;
cout<<"f("<<k<<")="<<f;
return 0;
}
OUTPUT:-
Enter the no. of observations
6
Enter the different values of x
4
5
7
10
11
13
Enter the corresponding values of y
48
100
294
900
1210
2028
Enter the value of 'k' for f(k)
evaluation 8
f(8)=448
OUTPUT:-
Enter the no. of observations
6
Enter the different values of x
4
5
7
10
11
13
Enter the corresponding values of y
48
100
294
900
1210
2028
Enter the value of 'k' for f(k)
evaluation 15
f(15)=3150