KEMBAR78
CBNMST Assignment 1-2 | PDF | Namespace | Function (Mathematics)
0% found this document useful (0 votes)
23 views63 pages

CBNMST Assignment 1-2

This document contains assignments from the Motilal Nehru National Institute of Technology, detailing various mathematical problems and their solutions using programming. It includes finding derivatives, roots of equations using the Bisection method, Regula-falsi method, and Fixed Point Iteration method. Each section provides code snippets alongside outputs demonstrating the solutions to the problems.

Uploaded by

snoihik2004
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views63 pages

CBNMST Assignment 1-2

This document contains assignments from the Motilal Nehru National Institute of Technology, detailing various mathematical problems and their solutions using programming. It includes finding derivatives, roots of equations using the Bisection method, Regula-falsi method, and Fixed Point Iteration method. Each section provides code snippets alongside outputs demonstrating the solutions to the problems.

Uploaded by

snoihik2004
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 63

ीीनेहर राषट ीी

य पौदयोगगकी संसथान इलाहाबाद


पयागराज-211004 (भारत)
Motilal Nehru National Institute of Technology
Allahabad
Prayagraj-211004 (India)

Assignment 1
Academic Year 2023-24 (EVEN Semester)

Student’s Name: Shiv Sharan Shukla.


Registration No.: 20236210.
Branch: Mechanical Engineering.
Section: H1
Date of Submission: 04-04-2024.

Signature: ……………………………
CBNST ASSIGNMENT---1

1. Find the 1st derivative of ()=54+33+62


+ + 7 at the point = 1.

#include<iostream>

#include<iomanip>

#include<math.h>

using namespace std;

int main() {

int secder = 0;

int ans = 0;

int n;

cout << "enter point at which u want the derivative :";

cin >> n;

int t;

cout << "enter the degree of

polynomial :"; cin >> t;

for (int i = 0; i <= t; i++) {

int pow = i;
int c;

cout << "enter the coeffiecient for power " << i <<

":"; cin >> c;

int diff1 = pow * c * std::pow(n, pow - 1);

int diff2 = pow * (pow - 1) * c * std::pow(n, pow - 2);

ans = ans + diff1;

secder = secder + diff2;

}
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

2.Find a root of the equation f(x)=x3-x-1=0 by Bisection method


correct to three decimal places

#include<iostream>

#include<iomanip>

#include<math.h>

using namespace std;

/*

Defining equation to be solved.

*/

#define f(x) pow(x,3)-x-1

int main()

/* Declaring required variables */

float x0, x1, x, f0, f1, f, e; int

step = 1;

/* Setting precision and writing floating point values in


fixed-point notation. */
cout<< setprecision(3)<< fixed;

/* Inputs */
up:

cout<<"Enter first guess: ";

cin>>x0;

cout<<"Enter second guess: ";

cin>>x1;

cout<<"Enter tolerable error: ";

cin>>e;

/* Calculating Functional Value */

f0 = f(x0);

f1 = f(x1);

/* Checking whether given guesses brackets the root or not.

*/ if( f0 * f1 > 0.0)

cout<<"Incorrect Initial Guesses."<<

endl; goto up;

/* Implementing Bisection Method */

cout<< endl<<"****************"<< endl;

cout<<"Bisection Method"<< endl;

cout<<"****************"<< endl;

do
{
/* Bisecting Interval */

x = (x0 + x1)/2;
f = f(x);

cout<<"Iteration-"<< step<<":\t x = "<< setw(10)<< x<<"


and f(x) = "<< setw(10)<< f(x)<< endl;

if( f0 * f < 0)

x1 = x;

}
else

{
x0 = x;

step = step + 1;

}while(fabs(f)>e);

cout<< endl<< "Root is: "<< x<< endl;

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

Root is: 1.325

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>

using namespace std;

/*

Defining equation to be solved.

*/

#define f(x) 4*exp(-x)*sin(x)-1

int main()

/* Declaring required variables */

float x0, x1, x, f0, f1, f, e; int

step = 1;

/* Setting precision and writing floating point values in fixed-


point notation. */
cout<< setprecision(3)<< fixed;

/* Inputs */

up:

cout<<"Enter first guess: ";

cin>>x0;

cout<<"Enter second guess: ";

cin>>x1;

cout<<"Enter tolerable error: ";

cin>>e;

/* Calculating Functional Value */

f0 = f(x0);

f1 = f(x1);

/* Checking whether given guesses brackets the root or not.

*/ if( f0 * f1 > 0.0)


{

cout<<"Incorrect Initial Guesses."<<

endl; goto up;

}
/* Implementing Bisection Method */ cout<<

endl<<"****************"<< endl;

cout<<"Bisection Method"<< endl;

cout<<"****************"<< endl;

do

/* Bisecting Interval */

x = (x0 + x1)/2;

f = f(x);

cout<<"Iteration-"<< step<<":\t x = "<< setw(10)<< x<<" and f(x)


= "<< setw(10)<< f(x)<< endl;

if( f0 * f < 0)

x1 = x;

else

x0 = x;

step = step + 1;

}while(fabs(f)>e);

cout<< endl<< "Root is: "<< x<< endl;

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

Root is: 1.365

4. The equation 2x=log10x +7 has a root between 3 and 4. Find this


root, correct to three decimal places, by Regula-falsi method

#include<iostream>

#include<iomanip>

#include<math.h>

/*

Defining equation to be solved.

Change this equation to solve another problem.

*/

#define f(x) 2*x-log10(x)-7

using namespace std;

int main()

{
/* Declaring required variables */

float x0, x1, x, f0, f1, f, e; int

step = 1;

/* Setting precision and writing floating point values in fixed-


point notation. */
cout<< setprecision(3)<< fixed;

/* Inputs */

cout<<"Enter first guess: ";

cin>>x0;

cout<<"Enter second guess: ";

cin>>x1;

cout<<"Enter tolerable error: ";

cin>>e;

/* Calculating Functional Value */

f0 = f(x0);
f1 = f(x1);

/* Checking whether given guesses brackets the root or not.

*/ if( f0 * f1 > 0.0)

cout<<"Incorrect Initial Guesses."<< endl;

}
/* Implementing False Position Method */ cout<<

endl<<"*********************"<< endl;

cout<<"False Position Method"<< endl;

cout<<"*********************"<< endl;

do

/* Applying False Position Method */

/* x is next approximated root using False Position method */


x = x0 - (x0-x1) * f0/ (f0-f1);

f = f(x);

cout<<"Iteration-"<< step<<":\t x = "<< setw(10)<< x<<" and f(x)


= "<< setw(10)<< f(x)<< endl;

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: 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

Root is: 3.789


5. Find a root of the equation f(x)= 4e-x sin x -1=0 Regula-falsi method
given that the root lies between 0 and 0.5.
#include<iostream>

#include<iomanip>

#include<math.h>

/*

Defining equation to be solved.

Change this equation to solve another problem.

*/

#define f(x) 4*sin(x)*exp(-x)-1

using namespace std;

int main()

/* Declaring required variables */

float x0, x1, x, f0, f1, f, e; int

step = 1;

/* Setting precision and writing floating point values in fixed-


point notation. */
cout<< setprecision(3)<< fixed;

/* Inputs */

cout<<"Enter first guess: ";

cin>>x0;

cout<<"Enter second guess: ";

cin>>x1;

cout<<"Enter tolerable error: ";

cin>>e;

/* Calculating Functional Value */


f0 = f(x0);

f1 = f(x1);

/* Checking whether given guesses brackets the root or not. */

if( f0 * f1 > 0.0)

cout<<"Incorrect Initial Guesses."<< endl;

}
/* Implementing False Position Method */ cout<<

endl<<"*********************"<< endl;

cout<<"False Position Method"<< endl;

cout<<"*********************"<< endl;

do
{
/* Applying False Position Method */

/* x is next approximated root using False Position method

*/ x = x0 - (x0-x1) * f0/ (f0-f1);

f = f(x);

cout<<"Iteration-"<< step<<":\t x = "<< setw(10)<< x<<" and f(x)


= "<< setw(10)<< f(x)<< endl;

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

Root is: 0.371

6. Use the method of iteration to find a positive root of the equation


xex =1, given that a root lies between 0 to 1

#include<iostream>

#include<iomanip>

#include<math.h>

#include<stdlib.h>

/* Define function f(x) which

is to be solved */
#define f(x) x*exp(x)-1

/* Write f(x) as x = g(x) and

define g(x) here */


#define g(x) exp(-x)

using namespace std;

int main()

int step=1, N;

float x0, x1, e;

/* Setting precision and writing floating point values in fixed-


point notation. */
cout<< setprecision(3)<< fixed;

/* Inputs */

cout<<"Enter initial guess: ";

cin>>x0;

cout<<"Enter tolerable error: ";

cin>>e;

cout<<"Enter maximum iteration: ";

cin>>N;

/* Implementing Fixed Point Iteration */

cout<< endl<<"**************************"<<

endl; cout<<"Fixed Point Iteration Method"<<

endl; cout<<"**************************"<<

endl; do

x1 = g(x0);

cout<<"Iteration-"<< step<<":\t x1 = "<< setw(10)<< x1<<" and f(x1)


= "<< setw(10)<< f(x1)<< endl;

step = step + 1;

if(step>N)
{

cout<<"Not Convergent.";

exit(0);
}

x0 = x1;

}while( fabs(f(x1)) > e);

cout<< endl<<"Root is "<< 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>

/* Define function f(x) which

is to be solved */

#define f(x) sin(x)-10*x+10

/* Write f(x) as x = g(x) and

define g(x) here */

#define g(x) (sin(x)+10)/10

using namespace std;

int main()

int step=1, N;

float x0, x1, e;

/* Setting precision and writing floating point values in fixed-


point notation. */
cout<< setprecision(3)<< fixed;

/* Inputs */

cout<<"Enter initial guess: ";

cin>>x0;

cout<<"Enter tolerable error: ";

cin>>e;

cout<<"Enter maximum iteration: ";

cin>>N;

/* Implementing Fixed Point Iteration */


cout<< endl<<"**************************"<< endl;

cout<<"Fixed Point Iteration Method"<< endl;

cout<<"**************************"<< endl;
do

x1 = g(x0);

cout<<"Iteration-"<< step<<":\t x1 = "<< setw(10)<< x1<<" and f(x1)


= "<< setw(10)<< f(x1)<< endl;

step = step + 1;

if(step>N)

cout<<"Not Convergent.";

exit(0);

x0 = x1;

}while( fabs(f(x1)) > e);

cout<< endl<<"Root is "<< 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

8. Using Newton-Raphson method, find a real root, correct to 3 decimal


places, of the equation sin x =x/2 given that the root lies between π/2 and π.

#include<iostream>

#include<iomanip>

#include<math.h>

#include<stdlib.h>

/* Defining equation to be solved.

Change this equation to solve another problem. */

#define f(x) 2*sin(x)-x

/* Defining derivative of g(x).

As you change f(x), change this function also.

*/ #define g(x) 2*cos(x)-1

using namespace std;

int main()

float x0, x1, f0, f1, g0, e;

int step = 1, N;

/* Setting precision and writing floating point values in


fixed-point notation. */
cout<< setprecision(3)<< fixed;

/* Inputs */

cout<<"Enter initial guess: ";

cin>>x0;
cout<<"Enter tolerable error: ";
cin>>e;

cout<<"Enter maximum iteration: ";

cin>>N;

/* Implementing Newton Raphson Method */

cout<< endl<<"*********************"<< endl;

cout<<"Newton Raphson Method"<< endl;

cout<<"*********************"<< endl;

do

g0 = g(x0);

f0 = f(x0);

if(g0 == 0.0)

cout<<"Mathematical Error.";

exit(0);

x1 = x0 - f0/g0;

cout<<"Iteration-"<< step<<":\t x = "<< setw(10)<< x1<<"


and f(x) = "<< setw(10)<< f(x1)<< endl;
x0 = x1;

step = step+1;

if(step > N)

cout<<"Not Convergent.";

exit(0);

f1 = f(x1);
}while(fabs(f1)>e);

cout<< endl<<"Root is: "<< x1;

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

Root is: 1.896

9. Using Newton-Raphson method, find a real root, correct to 4 decimal


places, of the equation x sin x + cos x =0.

#include<iostream>

#include<iomanip>

#include<math.h>

#include<stdlib.h>

/* Defining equation to be solved.

Change this equation to solve another problem. */

#define f(x) x*sin(x)+cos(x)

/* Defining derivative of g(x).


As you change f(x), change this function also.

*/ #define g(x) x*cos(x)

using namespace std;

int main()

float x0, x1, f0, f1, g0, e;

int step = 1, N;

/* Setting precision and writing floating point values in


fixed-point notation. */
cout<< setprecision(4)<< fixed;

/* Inputs */

cout<<"Enter initial guess: ";

cin>>x0;

cout<<"Enter tolerable error: ";

cin>>e;

cout<<"Enter maximum iteration: ";

cin>>N;

/* Implementing Newton Raphson Method */

cout<< endl<<"*********************"<<

endl; cout<<"Newton Raphson Method"<<

endl; cout<<"*********************"<<

endl; do

g0 = g(x0);

f0 = f(x0);

if(g0 == 0.0)

cout<<"Mathematical Error.";

exit(0);
}
x1 = x0 - f0/g0;

cout<<"Iteration-"<< step<<":\t x = "<< setw(10)<< x1<<"


and f(x) = "<< setw(10)<< f(x1)<< endl;
x0 = x1;

step = step+1;

if(step > N)

cout<<"Not Convergent.";

exit(0);

f1 = f(x1);

}while(fabs(f1)>e);

cout<< endl<<"Root is: "<< x1;

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

Root is: 56.5310

10. Using Muller’s method, find the root of the equation f(x)=x3-2x-5=0
which lies between 2 and 3.

/ C++ Program to find root of a function, f(x) #include<iostream>

#include<iomanip>
#include<math.h>

using namespace std;

const int MAX_ITERATIONS = 10000;

/ Function to calculate f(x) float f(float x)

/ Taking f(x) = x ^ 3 + 2x ^ 2 + 10x - 20 return 1*pow(x, 3) -2*x-5;

void Muller(float a, float b, float c)

int i;

float res;

for (i = 0;;++i)

/ Calculating various constants required

/ 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)));
float a2 = (((d1*h2) - (d2*h1))/((h1*h2) * (h1-h2)));

float x = ((-2*a0) / (a1 + abs(sqrt(a1*a1-4*a0*a2))));

float y = ((-2*a0) / (a1-abs(sqrt(a1*a1-4*a0*a2))));

/ Taking the root which is closer to x2 if (x >= y)

res = x + c;
else

res = y + c;

/ checking for resemblance of x3 with x2 till

/ two decimal places

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)

cout << "Root cannot be found using"

<< " Muller's method";

break;
}

if (i <= MAX_ITERATIONS)

cout << "The value of the root is " << res;

int main()

float a,b,c;

cout<<"enter value of a : ";

cin>>a;

cout<<"enter value of b : ";

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

using namespace std;


const int MAX_ITERATIONS = 10000;

/ Function to calculate f(x) float f(float x)

/ Taking f(x) = x ^ 3 + 2x ^ 2 + 10x - 20

return 1*pow(x, 3) + 2*x*x + 10*x - 20;


}

void Muller(float a, float b, float c)

int i;

float res;

for (i = 0;;++i)

/ Calculating various constants required

/ 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)));

float a2 = (((d1*h2) - (d2*h1))/((h1*h2) * (h1-h2)));

float x = ((-2*a0) / (a1 + abs(sqrt(a1*a1-4*a0*a2))));

float y = ((-2*a0) / (a1-abs(sqrt(a1*a1-4*a0*a2))));

/ Taking the root which is closer to x2 if (x >= y)


res = x + c;

else
res = y + c;

/ checking for resemblance of x3 with x2 till

/ two decimal places

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)

cout << "Root cannot be found using"

<< " Muller's method";

break;

if (i <= MAX_ITERATIONS)

cout << "The value of the root is " << res;

int main()

float a,b,c;

cout<<"enter value of a : ";

cin>>a;

cout<<"enter value of b : ";

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>

#define MAX_ITER 100

#define EPSILON 1e-6

/ Function to define the equation f(x) double f(double x) {

return pow(x, 3) - 6 * pow(x, 2) + 11 * x - 6;

/ Function to calculate the derivative of f(x) at a specific root double

fprime(double roots[], int n, int k) {

double result = 1.0;

for (int i = 0; i < n; i++)

{ if (i != k) {

result *= (roots[k] - roots[i]);

}
return result;

// Quotient-Difference Method implementation

void quotientDifferenceMethod(double roots[], int n)

{ double newRoots[n];

for (int i = 0; i < MAX_ITER; i++) {

for (int j = 0; j < n; j++) {

newRoots[j] = roots[j] - f(roots[j]) / fprime(roots, n, j);

double maxError = 0.0;

for (int j = 0; j < n; j++) {

double error = fabs(newRoots[j] -

roots[j]); if (error > maxError) {

maxError = error;
}

roots[j] = newRoots[j];

std::cout << "Iteration " << i + 1 << ":

"; for (int j = 0; j < n; j++) {

std::cout << "Root" << j + 1 << " = " << roots[j] << ", ";

std::cout << "Max Error = " << maxError <<

std::endl; if (maxError < EPSILON)

break;

int main() {

int n = 3; // Number of roots


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

13. Apply the quotient-difference method to obtain the approximate


roots of the equation f(x)= x3-7x2+10x-2=0
#include <iostream>

#include <cmath>

#define MAX_ITER 100

#define EPSILON 1e-6

/ Function to define the equation f(x) double f(double x) {

return pow(x, 3) - 7 * pow(x, 2) + 10*x - 2;}

/ Function to calculate the derivative of f(x) at a specific root double

fprime(double roots[], int n, int k) {

double result = 1.0;


for (int i = 0; i < n; i++)

{ if (i != k) {

result *= (roots[k] - roots[i]);

return result;

/ Quotient-Difference Method implementation

void quotientDifferenceMethod(double roots[], int n) {


double newRoots[n];

for (int i = 0; i < MAX_ITER; i++) {

for (int j = 0; j < n; j++) {


newRoots[j] = roots[j] - f(roots[j]) / fprime(roots, n, j);
}

double maxError = 0.0;

for (int j = 0; j < n; j++) {

double error = fabs(newRoots[j] -

roots[j]); if (error > maxError) {

maxError = error;

roots[j] = newRoots[j];

std::cout << "Iteration " << i + 1 << ":

"; for (int j = 0; j < n; j++) {

std::cout << "Root" << j + 1 << " = " << roots[j] << ", ";

std::cout << "Max Error = " << maxError <<

std::endl; if (maxError < EPSILON)

break;

int main() {

int n = 3; // Number of roots

/ 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

Y 9962 9848 9659 9397 9063 8660

#include<iostream>

using namespace std;

int main()

float x[20], y[20][20];

int i,j, n;

/* Input Section */

cout << "Enter number of data? " <<

endl; cin >> n;

cout << "Enter data: " << endl;

for(i = 0; i < n ; i++)

cout << "x[" << i << "] = ";

cin >> x[i];


cout << "y[" << i <<"] = ";

cin >> y[i][0];

/* Generating Forward Difference Table

*/ for(i = 1; i < n; i++) {

for(j = 0; j < n-i; j++)


{

y[j][i] = y[j+1][i-1] - y[j][i-1];

/* Displaying Forward Difference Table */


cout << endl << "FORWARD DIFFERENCE TABLE" << endl;

for(i = 0; i < n; i++)

cout << x[i];

for(j = 0; j < n-i ; j++)

cout << "\t" << y[i][j];

cout << endl;

return 0;

OUTPUT:-

Enter number of data?


6
Enter data:
x[0] = 5
y[0] = 9962
x[1] = 10
y[1] = 9848
x[2] = 15
y[2] = 9659
x[3] = 20
y[3] = 9397
x[4] = 25
y[4] = 9063
x[5] = 30
y[5] = 8660
FORWARD DIFFERENCE TABLE
5 9962 -114 -75 2 -1 3
10 9848 -189 -73 1 2
15 9659 -262 -72 3
20 9397 -334 -69
25 9063 -403
30 8660

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>

using namespace std;

int main()

float x[20], y[20][20];

int i,j, n;

/* Input Section */

cout << "Enter number of data? " <<

endl; cin >> n;

cout << "Enter data: " << endl;

for(i = 0; i < n ; i++)

cout << "x[" << i << "] = ";

cin >> x[i];


cout << "y[" << i <<"] = ";

cin >> y[i][0];

}
/* Generating Backward Difference Table

*/ for(i = 1; i < n; i++) {

for(j = n-1; j > i-1; j--)

y[j][i] = y[j][i-1] - y[j-1][i-1];

/* Displaying Backward Difference Table */


cout << endl << "BACKWARD DIFFERENCE TABLE" << endl;

for(i = 0; i < n; i++)

cout << x[i];

for(j = 0; j <= i ; j++)

cout << "\t" << y[i][j];

cout << endl;

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

Q3) . The population of a town was as given below. Estimate the


population for the year 1895:

YEAR(X) 1891 1901 1911 1921 1931

POPULATIO 46 66 81 93 101
N(Y)

(IN
THOUSAND
S)

#include<iostream>

#include<iomanip>

using namespace std;

#define MAXN 100

#define ORDER 4

int main()

float ax[MAXN+1],ay[MAXN+1],diff[MAXN+1]

[ORDER+1]; float nr=1.0,dr=1.0,x,p,h,yp; int

n,i,j,k;

cout<<"Enter the value of n"<<endl;

cin>>n;

cout<<"Enter the value of x,y"<<endl;

for(i=0;i<n;i++)
cin>>ax[i]>>ay[i];
cout<<"Enter the value of x";

cout<<"for which value of y is wanted"<<endl;

cin>>x;

cout<<fixed;

h=ax[1]-ax[0];

//Making diff. table calculating the 1st order differences

for(i=0;i<n-1;i++)

diff[i][1]=ay[i+1]-ay[i];

// calculating the 2nd & higher order differences

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++;

//Here ax[i] is x0 & ay[i] is y0

i--;

p=(x-ax[i])/h;

yp=ay[i];

//carrying out interpolation

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:-

Enter the value of n


5
Enter the value of x,y
1891 46
1901 66
1911 81
1921 93
1931 101
Enter the value of xfor which value of y is
wanted 1895
When x=1895.0 y= 72.16

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)

//program for newton backward interpolation formula

#include<iostream>

#include<iomanip>

#include<math.h>

using namespace std;

#define maxn 100


#define order 4

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;

cout<<"enter the value of n\n";

cin>>n;

cout<<"enter the values in form x,y\n";

for(i=1;i<=n;i++)

{cin>>ax[i]>>ay[i];}

cout<<"enter the value of x for which value of y is wanted\n";

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];

cout<<"when x = " <<x << " y = "<<yp ;

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

Q5) 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

#include<iostream>

#include<math.h>

using namespace std;

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 ";

cout<<"\nEnter the no. of terms \n";

cin>>n;

cout<<"\n\n Enter the value in the form of x : \n ";

for(i=0;i<n;i++)

cout<<"\n Enter the value of x "<<i+1<<" " ;

cin>>ax[i];

cout<<"\n Enter the value in the form of y : ";

for(i=0;i<n;i++)

cout<<"\n Enter the value of y "<<i+1<<" "

; cin>>ay[i];

cout<<"\n\n Enter the value of x for which u want the value of y

"; 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:-

GAUSS' FORWARD INTERPOLATION


FORMULA Enter the no. of terms
5

Enter the value in the form of x :

Enter the value of x 1 0

Enter the value of x 2 4


Enter the value of x 3 8

Enter the value of x 4 12

Enter the value of x 5 16

Enter the value in the form of y :


Enter the value of y 1 14

Enter the value of y 2 24

Enter the value of y 3 32

Enter the value of y 4 35

Enter the value of y 5 40

Enter the value of x for which u want the value of y 9


9 33.3359

6. Apply Gauss’ backward interpolation formula to find the population


for the year 1936 given that

Year (x) 1901 1911 1921 1931 1941 1951


Populatio
n 12 15 20 27 39 52
(y) (In
thousand
s

#include <iostream>

#include <cmath>

using namespace std;

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];

float y1, y2, y3, y4;

cout << "Enter the number of terms -

"; cin >> n;

cout << "Enter the value in the form of x -

"; for (i = 0; i < n; i++) {

cout << "\nEnter the value of x" << i + 1 << " -

"; cin >> ax[i];

cout << "\nEnter the value in the form of y - ";

for (i = 0; i < n; i++)

cout << "\nEnter the value of y" << i + 1 << " -

"; cin >> ay[i];

cout << "\nEnter the value of x for which you want the value of y -

"; 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][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] /

24; y = ay[i] + y1 + y2 + y3 + y4;

cout << "\nwhen x = " << x << ", y = " << y << endl;

return 0;

OUTPUT:-

Enter the number of terms - 6


Enter the value in the form of x -
Enter the value of x1 - 1901

Enter the value of x2 -

1911 Enter the value of x3

- 1921 Enter the value of

x4 - 1931
Enter the value of x5 - 1941

Enter the value of x6 - 1951

Enter the value in the form of y


- Enter the value of y1 - 12

Enter the value of y2 -

15 Enter the value of y3

- 20 Enter the value of

y4 - 27 Enter the value

of y5 - 39 Enter the

value of y6 - 52

Enter the value of x for which you want the value of y -

1936 when x = 1936, y = 32.0703

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

Find the value of tan 16degrees using Stirling


interpolation formula.

#include <iostream>

#include <cmath>

using namespace std;

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;

cout << "Enter the value of terms: ";

cin >> n;

cout << "Enter the values for x: " <<

endl; for (i = 0; i < n; i++) {

cout << "Enter the value for x" << i + 1 << ":

"; cin >> ax[i];

cout << "Enter the values for y: " <<

endl; for (i = 0; i < n; i++) {

cout << "Enter the value for y" << i + 1 << ":

"; cin >> ay[i];

cout << "Enter the value of x for which you want the value of y:

"; 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] + diff[i - 1][1]) / 2;

y2 = p * p * diff[i - 1][2] / 2;

y3 = p * (p * p - 1) * (diff[i - 1][3] + diff[i - 2][3]) / 6;


y4 = p * p * (p * p - 1) * diff[i - 2][4] / 24;

y = ay[i] + y1 + y2 + y3 + y4;

cout << "When x = " << x << ", y = " << y << endl;

return 0;

OUTPUT:-

Enter the value of terms: 7


Enter the values for x:
Enter the value for x1: 0
Enter the value for x2: 5
Enter the value for x3: 10
Enter the value for x4: 15
Enter the value for x5: 20
Enter the value for x6: 25
Enter the value for x7: 30
Enter the values for y:
Enter the value for y1: 0
Enter the value for y2:
0.0875 Enter the value for
y3: 0.1763 Enter the value
for y4: 0.2679 Enter the
value for y5: 0.364 Enter the
value for y6: 0.4663 Enter
the value for y7: 0.5774
Enter the value of x for which you want the value of y:
16 When x = 16, y = 0.286651

8. Probability distribution function values of a normal distribution are


given as follows:

x 0.2 0.6 1.0 1.4 1.8


Y=p(x) 0.39104 0.33322 0.24197 0.14973 0.07895
Apply Bessel’s formula to find the value of p(1.2).

#include <iostream>

#include <cmath>

using namespace std;

int main() {
int n; // no. of terms.

int i, j; // Loop variables

float ax[10]; // 'X' array limit 9

float ay[10]; // 'Y' array limit 9

float x; // User Query for what value of X

float y; // Calculated value for corresponding


X.

float h; // Calc. Section

float p; // Calc. Section

float diff[20][20]; // to store Y

float y1, y2, y3, y4; // Formula variables.

cout << "\t\t !! BESSELS INTERPOLATION FORMULA !! " << endl;

// Input section.
cout << "\n\n Enter the no. of terms ->

"; cin >> n;

// Input Sequel for array X

cout << "\n\n Enter the value in the form of x -> ";

// Input loop for X.

for (i = 0; i < n; i++) {

cout << "\n Enter the value of x" << i + 1 << " ->

"; cin >> ax[i];

// Input sequel for array Y.

cout << "\n\n Enter the value in the form of y -> ";

// Input loop for Y.

for (i = 0; i < n; i++) {

cout << "\n Enter the value of y" << i + 1 << " ->

"; cin >> ay[i];

/ Inputting the required value quarry

cout << "\n\n Enter the value of x for ";

cout << "\n which u want the value of y ->

"; cin >> x;

/ Calculation and processing section.

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][2] + diff[i - 1][2]) / 4;

y3 = p * (p - 1) * (p - 0.5) * diff[i - 1][3] / 6;

y4 = (p + 1) * p * (p - 1) * (p - 2) * (diff[i - 2][4] + diff[i


- 1][4]) / 48;

// Taking sum

y = ay[i] + y1 + y2 + y3 + y4;

// Output Section

cout << "\n When x = " << x << " , y = " << y << endl;

return 0;

OUTPUT:-

!! BESSELS INTERPOLATION FORMULA !!

Enter the no. of terms -> 5

Enter the value in the form of x -


> Enter the value of x1 -> 0.2

Enter the value of x2 -> 0.6

Enter the value of x3 -> 1.0

Enter the value of x4 -> 1.4

Enter the value of x5 -> 1.8


Enter the value in the form of y -
> Enter the value of y1 -> 0.39104

Enter the value of y2 -> 0.33322

Enter the value of y3 -> 0.24197

Enter the value of y4 -> 0.14973

Enter the value of y5 -> 0.07895

Enter the value of x for


which u want the value of y -> 1.2

When x = 1.2 , y = 0.194454

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

using namespace std;

int main()

//... Variable declaration Field

//... Integer Type

int n;

int i,j;

//... Floating Type

float ax[10]; float

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 no. of terms ->

"; cin >> n;

// Input Sequel for array X

cout << "\n\n Enter the value in the form of x -> ";

// Input loop for X.

for (i = 0; i < n; i++) {


cout << "\n Enter the value of x" << i + 1 << " ->

"; cin >> ax[i];

// Input sequel for array Y.

cout << "\n\n Enter the value in the form of y -> ";

// Input loop for Y.

for (i = 0; i < n; i++) {

cout << "\n Enter the value of y" << i + 1 << " ->

"; cin >> ay[i];

/ Inputting the required value quarry

cout << "\n\n Enter the value of x for ";

cout << "\n which u want the value of y ->

"; cin >> x;


//... Calculation and Processing Section

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;

//... Taking sum

y=y1+y2+y3+y4+py1+py2+py3;

//... Output Section

cout << "\n When x = " << setprecision(9) << x << " , y = "
<< setprecision(9)<< y << endl;
return 0; }

OUTPUT:-
Enter the no. of terms -> 6

Enter the value in the form of x ->


Enter the value of x1 -> 15

Enter the value of x2 -> 20

Enter the value of x3 -> 25

Enter the value of x4 -> 30

Enter the value of x5 -> 35

Enter the value of x6 -> 40

Enter the value in the form of y ->


Enter the value of y1 -> 12.849

Enter the value of y2 -> 16.351

Enter the value of y3 -> 19.524

Enter the value of y4 -> 22.396

Enter the value of y5 -> 24.999

Enter the value of y6 -> 27.356

Enter the value of x for


which u want the value of y -> 26

When x = 26 , y = 20.1214314

OUTPUT:-
Enter the no. of terms -> 6

Enter the value in the form of x ->


Enter the value of x1 -> 15

Enter the value of x2 -> 20

Enter the value of x3 -> 25

Enter the value of x4 -> 30

Enter the value of x5 -> 35

Enter the value of x6 -> 40

Enter the value in the form of y ->


Enter the value of y1 -> 12.849
Enter the value of y2 -> 16.351

Enter the value of y3 -> 19.524

Enter the value of y4 -> 22.396

Enter the value of y5 -> 24.999

Enter the value of y6 -> 27.356

Enter the value of x for


which u want the value of y -> 27

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>

using namespace std;

int main()

float x[100], y[100], xp, yp=0, p;

int i,j,n;

/* Input Section */

cout<<"Enter number of value: ";

cin>>n;

cout<<"Enter data:"<< endl;

for(i=1;i<=n;i++)

cout<<"x["<< i<<"] = ";


cin>>x[i]; cout<<"y["<<

i<<"] = "; cin>>y[i];


}

cout<<"Enter interpolation point: ";

cin>>xp;

/* Implementing Lagrange Interpolation */

for(i=1;i<=n;i++)

p=1;

for(j=1;j<=n;j++)

if(i!=j)

p = p* (xp - x[j])/(x[i] - x[j]);

yp = yp + p * y[i];

cout<< endl<<"Interpolated value at "<< xp<< "


is "<<setprecision(18)<< yp;

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;

cout<<"Enter the no. of observations\

n"; cin>>n;

cout<<"Enter the different values of x\n";

for(i=1;i<=n;i++)
{
cin>>x[i];
}

cout<<"Enter the corresponding values of y\n";

for(i=1;i<=n;i++)
{
cin>>y[i];
}
f=y[1];

cout<<"Enter the value of 'k' for f(k) evaluation\

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

You might also like