0% found this document useful (0 votes)
30 views23 pages

Assignment 3 CBNST

The document provides the solution to find the third derivative of a function using finite difference method from a table of x and y values. It includes the code to calculate the nth derivative using divided differences, along with functions to calculate factorials and binomial coefficients. The main function applies this to the example data set and returns the third derivative at x=5.

Uploaded by

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

Assignment 3 CBNST

The document provides the solution to find the third derivative of a function using finite difference method from a table of x and y values. It includes the code to calculate the nth derivative using divided differences, along with functions to calculate factorials and binomial coefficients. The main function applies this to the example data set and returns the third derivative at x=5.

Uploaded by

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

2 |Page

Q1. Using Newton’s forward formula, find ‘ (1.1) and ‘’1.1) from the following table:

X 1.0 1.2 1.4 1.6 1.8 2.0


F(x) 0.0 0.128 0.554 1.296 2.432 4.0

Solution:

#include <iostream>

#include <conio.h>

using namespace std;

class DerivativeForward

public:

void askN();

void askX();

void askF();

void askXX();

void forwardTable();

void calcd1();

void calcd2();

void findH();

void solve();

void fillDelF();

void findS();

private:

double XX, x[10] , f[10][10] , p[10],diff[10][10],P1,delF[10],f1,f2;

int n;

double h,s;
3 |Page

};

void DerivativeForward::askX()

cout << endl;

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

cout << "ENter X[" << i << "] : ";

cin >> x[i];

cout << endl;

void DerivativeForward::askF()

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

cout << "ENter F[" << j << "] : ";

cin >> f[0][j];

cout << endl;

void DerivativeForward::askXX()

cout << "Enter X for which the value is to be found: ";

cin >> XX;


4 |Page

void DerivativeForward::forwardTable()

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

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

f[i][j] = (f[i-1][j+1]-f[i-1][j]);

if(f[i][j] < 0.0000009 && f[i][j] > 0 || f[i][j] >-0.0000009 && f[i][j]<0)

f[i][j] = 0;

cout << endl;

cout << "Sn\tXi\tf(Xi)\t";

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

cout << i+1 << " diff\t";

cout << endl;

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

cout <<i+1 <<"\t" << x[i]<< "\t";

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

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


5 |Page

cout << endl;

void DerivativeForward::findH()

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

void DerivativeForward::findS()

s = (XX - x[0])/h;

void DerivativeForward::solve()

findH();

findS();

fillDelF();

calcd1();

calcd2();

cout <<endl << "The value of f1 :" << f1;

cout <<endl << "The value of f2 :" << f2;

cout << endl << endl;

}
6 |Page

void DerivativeForward::fillDelF()

for(int i = 1;i<10;i++)

if(i<n)

delF[i]=f[i][0];

else

delF[i] = 0;

void DerivativeForward::calcd1()

f1 = 1 / h * ( delF[1] + 1/2.0 * (2 * s -1 ) * delF[2] + 1 / (6.0) * (3*s*s - 6 *s +2)*

delF[3] + 1 /(24.0) *( 4*s*s*s-18*s*s+22*s-6)*delF[4]);

void DerivativeForward::calcd2()

f2 = 1 / (h*h) * (delF[2] + 1/6.0 * (6*s-6) * delF[3] + 1/24.0 *(12*s*s-


36*s+22)*delF[4]);

void DerivativeForward::askN()

cout << "Enter the number of values: ";


7 |Page

cin >> n;

int main()

DerivativeForward d1;

d1.askN();

d1.askX();

d1.askF();

d1.askXX();

d1.forwardTable();

d1.solve();

Output:

[?2004l

Enter the number of values: 6

ENter X[0] : 1.0

ENter X[1] : 1.2

ENter X[2] : 1.4

ENter X[3] : 1.6

ENter X[4] : 1.8

ENter X[5] : 2.0

ENter F[0] : 0.0

ENter F[1] : 0.128

ENter F[2] : 0.554

ENter F[3] : 1.296


8 |Page

ENter F[4] : 2.432

ENter F[5] : 4.0

Enter X for which the value is to be found: 1.1

Sn Xi f(Xi) 1 diff 2 diff 3 diff 4 diff 5 diff

1 1 0 0.128 0.298 0.018 0.06 -0.1

2 1.2 0.128 0.426 0.316 0.078 -0.04

3 1.4 0.554 0.742 0.394 0.038

4 1.6 1.296 1.136 0.432

5 1.8 2.432 1.568

6 2 4

The value of f1 :0.64875

The value of f2 :7.6625

Question2: . The distance covered by an athlete for the 50 metre race is


given in the following table. by using Newton’s backward formula.
Time(sec) 0 1 2 3 4 5 6
Distance(meter) 0 2.5 8.5 15.5 24.5 36.5 50

Solution:

#include <iostream>

#include <conio.h>

using namespace std;

class DerivativeBackward

{
9 |Page

public:

void askN();

void askX();

void askF();

void askXX();

void forwardTable();

void calcd1();

void calcd2();

void findH();

void solve();

void fillDelF();

void findS();

private:

double XX, x[10] , f[10][10] , p[10],diff[10][10],P1,delF[10],f1,f2;

int n;

double h,s;

};

void DerivativeBackward::askX()

cout << endl;

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

cout << "ENter X[" << i << "] : ";

cin >> x[i];

cout << endl;


10 | P a g e

void DerivativeBackward::askF()

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

cout << "ENter F[" << j << "] : ";

cin >> f[0][j];

cout << endl;

void DerivativeBackward::askXX()

cout << "Enter X for which the value is to be found: ";

cin >> XX;

void DerivativeBackward::forwardTable()

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

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

f[i][j] = (f[i-1][j+1]-f[i-1][j]);

if(f[i][j] < 0.0000009 && f[i][j] > 0 || f[i][j] >-0.0000009 && f[i][j]<0)

{
11 | P a g e

f[i][j] = 0;

cout << endl;

cout << "Sn\tXi\tf(Xi)\t";

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

cout << i+1 << " diff\t";

cout << endl;

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

cout <<i+1 <<"\t" << x[i]<< "\t";

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

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

cout << endl;

void DerivativeBackward::findH()

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

cout << "h = " <<h;

void DerivativeBackward::findS()
12 | P a g e

s = (XX - x[n-1])/h;

cout << "s = " <<s;

void DerivativeBackward::solve()

findH();

findS();

fillDelF();

calcd1();

calcd2();

cout <<endl << "The value of speed at t=5:" << f1;

cout << endl << endl;

void DerivativeBackward::fillDelF()

for(int i = 1;i<10;i++)

if(i<n)

delF[i] = f[i][n-i-1];

else

delF[i] = 0;

for(int i = 1;i<10;i++)
13 | P a g e

cout<< delF[i];

void DerivativeBackward::calcd1()

f1 = 1 / h * ( delF[1] + 1/2.0 * (2 * s +1 ) * delF[2] + 1 / (6.0) * (3*s*s + 6 *s


+2)*delF[3]

+ 1 /(24.0) *( 4*s*s*s+18*s*s+22*s+6)*delF[4]);

void DerivativeBackward::calcd2()

f2 = 1 / (h*h) * (delF[2] + 1/6.0 * (6*s+6) * delF[3] + 1/24.0


*(12*s*s+36*s+22)*delF[4]);

void DerivativeBackward::askN()

cout << "Enter the number of values: ";

cin >> n;

int main()

DerivativeBackward d1;

d1.askN();
14 | P a g e

d1.askX();

d1.askF();

d1.askXX();

d1.forwardTable();

d1.solve();

Output:

Enter the number of values: 7

ENter X[0] : 0

ENter X[1] : 1

ENter X[2] : 2

ENter X[3] : 3

ENter X[4] : 4

ENter X[5] : 5

ENter X[6] : 6

ENter F[0] : 0

ENter F[1] : 2.5

ENter F[2] : 8.5

ENter F[3] : 15.5

ENter F[4] : 24.5

ENter F[5] : 9 36.5

ENter F[6] : 50

Enter X for which the value is to be found: 5


15 | P a g e

Sn Xi f(Xi) 1 diff 2 diff 3 diff 4 diff 5 diff 6 diff

1 0 0 2.5 3.5 -2.5 3.5 -3.5 1

2 1 2.5 6 1 1 0 -2.5

3 2 8.5 7 2 1 -2.5

4 3 15.5 9 3 -1.5

5 4 24.5 12 1.5

6 5 36.5 13.5

7 6 50

h = 1s = -113.51.5-1.5-2.5-2.51000

The value of speed at t=5 is :13.2083

Question 3: Using Stirling formula, find ′ (1.6) and ′′(1.6) from the
following table:

x 1.0 1.2 1.4 1.6 1.8 2.0 2.2


y 2.7183 3.3201 4.0552 4.9530 6.0496 7.3891 9.0250

Solution:

Output:

Question 4: Q4. Using Bessel’s formula, find ′ (7.5) from the following
table:
x 7.47 7.48 7.49 7.5 7.51 7.52 7.53
y 0.193 0.195 0.198 0.201 0.203 0.206 0.208

Solution:

Output:

Question4: Using Newton’s divided difference formula, find ’’’(5) from the
data given below: x 2 4 9 13 16 21 29 y 57 1345 66340 402052 1118209 4287844
21242820

x 2 4 9 13 16 21 29
y 57 1345 66340 402052 1118209 4287844 21242820
16 | P a g e

Solution:

#include <iostream>

#include <vector>

#include <cmath> // Include cmath for pow function

// Function to calculate factorial

unsigned long factorial(int n) {

if (n == 0 || n == 1)

return 1;

return n * factorial(n - 1);

// Function to calculate binomial coefficient

unsigned long binomialCoeff(int n, int k) {

return factorial(n) / (factorial(k) * factorial(n - k));

// Function to calculate the nth derivative using finite differences

double nthDerivative(std::vector<double>& x, std::vector<double>& y, int n, double


xVal) {

int m = x.size();

int k = n + 1;

double h = x[1] - x[0];

double sum = 0;

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

double coeff = binomialCoeff(k, j) * ((j % 2 == 0) ? 1 : -1);


17 | P a g e

coeff *= y[j] / std::pow(h, j); // Use std::pow

sum += coeff;

return sum / factorial(n);

int main() {

// Example data points

std::vector<double> x = {2,4,9,13,16,21,29};

std::vector<double> y = {57,1345,66340,402052,1158209,4287844,21242820};

// Value of x for which derivative is to be calculated

double xVal = 5;

// Calculate the first three derivatives at xVal

for (int n = 1; n <= 3; ++n) {

double derivative = nthDerivative(x, y, n, xVal);

std::cout << "The " << n << "th derivative at x = " << xVal << " is: " << derivative <<
std::endl;

return 0;

Output:

The 1th derivative at x = 5 is: 1.24054e+22

The 2th derivative at x = 5 is: 4.69737e+23

The 3th derivative at x = 5 is: 1.56579e+23


18 | P a g e

Question 6: Using Lagrange’s method, find ′ (4) from the data given
below
x 0 2 5 1
y 0 8 125 1
Solution:

#include<iostream>

#include <cmath>

#include <vector>

#include<iomanip>

using namespace std;

double lagrange_basis_polynomial(const std::vector<double>& x, int k, double


target_x) {

double result = 1;

for (size_t i = 0; i < x.size(); ++i) {

if (i != static_cast<size_t>(k)) {

result *= (target_x - x[i]) / (x[k] - x[i]);

return result;

double lagrange_interpolating_polynomial(const std::vector<double>& x, const


std::vector<double>& y, double target_x) {

double result = 0;

for (size_t i = 0; i < x.size(); ++i) {

result += y[i] * lagrange_basis_polynomial(x, static_cast<int>(i), target_x);

return result;

}
19 | P a g e

double lagrange_derivative(const std::vector<double>& x, const std::vector<double>&


y, double target_x) {

double h = 0.0001; // Step size for numerical differentiation

return (lagrange_interpolating_polynomial(x, y, target_x + h) -


lagrange_interpolating_polynomial(x, y, target_x - h)) / (2 * h);

int main() {

// Given data

std::vector<double> x_values = {0, 2, 5, 1};

std::vector<double> y_values = {0, 8, 125, 1};

// Calculate y'(4) using Lagrange's method

double derivative_at_4 = lagrange_derivative(x_values, y_values, 4);

std::cout << "value of y'(4)is: " << derivative_at_4 << std::endl;

return 0;

Output:

value of y'(4)is: 48


2
Q7. Evaluate ydxhere y is given by the following table:
0.6
x 0.6 0.8 1.0 1.2 1.4 1.6 1.8 2.0
y 1.23 1.58 2.03 4.32 6.25 8.36 10.23 12.45
Solution:
20 | P a g e


11
Q8. Evaluate, ydx where y is given by the following table, using a suitable integration
1
formula: x 1 2 3 4 5 6 7 8 9 10 11 y 543 512 501 489 453 400 352 310 250 172 95

x 1 2 3 4 5 6 7 8 9 10 11
y 543 512 501 489 453 400 352 310 250 172 95
Solution:

def trapezoidal_rule(x, y):

integral = 0

n = len(x)

h = x[1] - x[0] # Assuming equally spaced intervals

# Trapezoidal rule formula

integral += (y[0] + y[n - 1]) / 2

for i in range(1, n - 1):

integral += y[i]

integral *= h

return integral

# Given data

x_values = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]

y_values = [543, 512, 501, 489, 453, 400, 352, 310, 250, 172, 95]

# Calculate integral

result = trapezoidal_rule(x_values, y_values)

print("Approximate integral using trapezoidal rule:", result)

Output:

Approximate integral using trapezoidal rule: 3758.0


21 | P a g e

Q9. Use Simpson’s rule for evaluating ∫ 0.3 −0.6 from the table given
below

X -0.6 -0.5 -0.4 -0.3 -0.2 -0.1 0.0 0.1 0.2 0.3
y 4 2 5 3 -2 1 6 4 2 8
Solution:
#include <iostream>
#include<math.h>
using namespace std;
int main()
{
double h = 0.1;
int y0 = 4;
int y1 = 2;
int y2 = 5;
int y3 = 3;
int y4 = -2;
int y5 = 1;
int y6 = 6;
int y7 = 4;
int y8 = 2;
int y9 = 8;
double ans1 = (y0 + y9) + 3 * (y1 + y2 + y4 + y5 + y7 + y8) + 2 * (y3 + y6);
double ans2 = 3 * h * ans1;
double ans3 = ans2 / 8;
cout << "using three by eight simpson rule "<<ans3 <<endl;
}
22 | P a g e

Output:
using three by eight simpson rule 2.475
Q10. A river is 80 m wide. The depth ‘y’ of the river at a distance ‘x’ from
one bank is given by the following table: Find the approximate area of
cross-section of the river using Boole’s rule.

x 0 10 20 30 40 50 60 70 80
y 0 4 7 9 12 15 14 8 3
Solution:
#include <iostream>
using namespace std;
int main() {
int h = 10;
int y0 = 0;
int y1 = 4;
int y2 = 7;
int y3 = 9;
int y4 = 12;
int y5 = 15;
int y6 = 14;
int y7 = 8;
int y8 = 3;
int ans1 = 7 * (y0 + y8) + 32 * (y1 + y3 + y5 + y7) + 14 * y4 + 12 * (y2 +
y6);
int ans2 = 2 * 10 * ans1;
int ans3 = ans2 / 45;
cout << ans3;
23 | P a g e

return 0;
}
Output:
708


1.6
Q11. Find by Weddle’s rule the value of the integral, x / sinhxdx by
0.4
taking 12 sub-intervals.
Solution:
#include <iostream>
#include <cmath>

// Define the function here (replace with your specific function)


double f(double x) {
return x / sinh(x);
}

double WeedleRule(double a, double b, int n) {


double h = (b - a) / n;
double sum = 0.0;

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


double xi = a + i * h;
sum += (1.0 / 90) * h * (7 * f(xi) + 32 * f(xi + h / 6) + 12 * f(xi + h / 3)
+ 32 * f(xi + h / 2) + 7 * f(xi + 5 * h / 6));
}
24 | P a g e

return sum;
}

int main() {
double a = 0.4;
double b = 1.6;
int n = 12;

double result = WeedleRule(a, b, n);

std::cout << "Approximate integral value using Weddle's rule: " <<
result << std::endl;

return 0;
}
Output:
Approximate integral value using Weddle's rule: 1.01483

You might also like