Assignment 3 CBNST
Assignment 3 CBNST
Q1. Using Newton’s forward formula, find ‘ (1.1) and ‘’1.1) from the following table:
Solution:
#include <iostream>
#include <conio.h>
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:
int n;
double h,s;
3 |Page
};
void DerivativeForward::askX()
void DerivativeForward::askF()
void DerivativeForward::askXX()
void DerivativeForward::forwardTable()
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;
for(int i = 0; i <n-1;i++)
void DerivativeForward::findH()
h = x[1] - x[0];
void DerivativeForward::findS()
s = (XX - x[0])/h;
void DerivativeForward::solve()
findH();
findS();
fillDelF();
calcd1();
calcd2();
}
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()
void DerivativeForward::calcd2()
void DerivativeForward::askN()
cin >> n;
int main()
DerivativeForward d1;
d1.askN();
d1.askX();
d1.askF();
d1.askXX();
d1.forwardTable();
d1.solve();
Output:
[?2004l
6 2 4
Solution:
#include <iostream>
#include <conio.h>
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:
int n;
double h,s;
};
void DerivativeBackward::askX()
void DerivativeBackward::askF()
void DerivativeBackward::askXX()
void DerivativeBackward::forwardTable()
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;
for(int i = 0; i <n-1;i++)
void DerivativeBackward::findH()
h = x[1] - x[0];
void DerivativeBackward::findS()
12 | P a g e
s = (XX - x[n-1])/h;
void DerivativeBackward::solve()
findH();
findS();
fillDelF();
calcd1();
calcd2();
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()
+ 1 /(24.0) *( 4*s*s*s+18*s*s+22*s+6)*delF[4]);
void DerivativeBackward::calcd2()
void DerivativeBackward::askN()
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 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[6] : 50
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
Question 3: Using Stirling formula, find ′ (1.6) and ′′(1.6) from the
following table:
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>
if (n == 0 || n == 1)
return 1;
int m = x.size();
int k = n + 1;
double sum = 0;
sum += coeff;
int main() {
std::vector<double> x = {2,4,9,13,16,21,29};
std::vector<double> y = {57,1345,66340,402052,1158209,4287844,21242820};
double xVal = 5;
std::cout << "The " << n << "th derivative at x = " << xVal << " is: " << derivative <<
std::endl;
return 0;
Output:
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>
double result = 1;
if (i != static_cast<size_t>(k)) {
return result;
double result = 0;
return result;
}
19 | P a g e
int main() {
// Given data
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:
integral = 0
n = len(x)
integral += y[i]
integral *= h
return integral
# Given data
y_values = [543, 512, 501, 489, 453, 400, 352, 310, 250, 172, 95]
# Calculate integral
Output:
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>
return sum;
}
int main() {
double a = 0.4;
double b = 1.6;
int n = 12;
std::cout << "Approximate integral value using Weddle's rule: " <<
result << std::endl;
return 0;
}
Output:
Approximate integral value using Weddle's rule: 1.01483