0% found this document useful (0 votes)
18 views

Cmlab

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)
18 views

Cmlab

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

UNIVERSITY SCHOOL OF INFORMATION,

COMMUNICATION AND TECHNOLOGY

COMPUTATIONAL METHODS (ICT 256)

BACHELOR OF TECHNOLOGY IN COMPUTER


SCIENCE ENGINEERING
PRACTICAL FILE

UNDER THE GUIDANCE OF: SUBMITTED BY:


Name: Santosh Prajapati
Mr. Ashish Payal Enroll no.: 41316403222
USICT, GGSIPU Course: B.Tech. (CSE), 4th sem.
Batch: 2022-2026
5. Write a program to implement Fibonacci Search Method.
Code:
#include <iostream>

using namespace std;

double func(double x)
{
return x * x;
}

double fibNo(int x)
{
if (x == 0 || x == 1)
{
return x;
}

return fibNo(x - 1) + fibNo(x - 2);


}

double FibSearch(double L, double R, double n)


{
int fib[10] = {-1};
int k = 1;
double phi = fibNo(n - k) / (double)fibNo(n - k + 1);
double x2 = L + phi * (R - L);
double x1 = L + R - x2;
double f1 = func(x1);
double f2 = func(x2);

while (k < n)
{
if (f1 < f2)
{
R = x2;
x2 = x1;
f2 = f1;
x1 = L + R - x2;
f1 = func(x1);
}
else
{
L = x1;
x1 = x2;
f1 = f2;
x2 = L + R - x1;
f2 = func(x2);
}
k++;
}

return (L + R) / 2;
}

int main()
{
double a, b, n;
cout << "Enter the interval [a, b]: ";
cin >> a >> b;
cout << "Enter the no of iterations: ";
cin >> n;
double minimum = FibSearch(a, b, n);
cout << "Minimum point: " << minimum << endl;
cout << "Minimum value: " << func(minimum) << endl;
return 0;
}

Output
7. Write a Program to implement Forward Interpolation.
Code:
#include <iostream>
#include <iomanip>
#include <vector>

using namespace std;

// Function to calculate the forward difference table


void calculateForwardTable(const vector<double>& x, const vector<double>& y,
vector<vector<double>>& forwardTable) {
int n = x.size();
forwardTable.resize(n, vector<double>(n));

// Initialize the first column of the table with y values


for (int i = 0; i < n; ++i) {
forwardTable[i][0] = y[i];
}

// Fill up the rest of the table using forward difference formula


for (int j = 1; j < n; ++j) {
for (int i = 0; i < n - j; ++i) {
forwardTable[i][j] = forwardTable[i + 1][j - 1] - forwardTable[i][j - 1];
}
}
}

// Function to interpolate using the forward difference table


double interpolate(const vector<double>& x, const vector<vector<double>>&
forwardTable, double xi) {
double result = forwardTable[0][0];
double u = (xi - x[0]) / (x[1] - x[0]);
double term = 1;
int n = forwardTable.size();

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


term *= (u - i + 1) / i;
result += term * forwardTable[0][i];
}
return result;
}

// Function to print the forward difference table


void printTable(const vector<double>& x, const vector<vector<double>>&
forwardTable) {
int n = x.size();
cout << "Forward Difference Table:\n\n";
cout << setw(10) << "x" << setw(15) << "f(x)";

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


cout << setw(15) << "Δ^" << i << "f(x)";
}

cout << "\n\n";

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


cout << setw(10) << x[i];

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


cout << setw(15) << forwardTable[i][j];
}

cout << "\n";


}
}

int main() {
vector<double> x = {1, 7, 3, 4};
vector<double> y = {2, 4, 5, 8};

vector<vector<double>> forwardTable; // Table to store forward differences

// Calculate the forward difference table


calculateForwardTable(x, y, forwardTable);

// Print the forward difference table


printTable(x, forwardTable);
double xi = 2.5;
double interpolatedValue = interpolate(x, forwardTable, xi);
cout << "\nInterpolated value at x = " << xi << " is " << interpolatedValue << endl;

return 0;
}

Output
8. Write a Program to Implement Backward Interploation.
Code:
#include <iostream>
#include <iomanip>
#include <vector>

using namespace std;

// Function to calculate the backward difference table


void calculateBackwardTable(const vector<double>& x, const vector<double>& y,
vector<vector<double>>& backwardTable) {
int n = x.size();
backwardTable.resize(n, vector<double>(n));

// Initialize the first column of the table with y values


for (int i = 0; i < n; ++i) {
backwardTable[i][0] = y[i];
}

// Fill up the rest of the table using backward difference formula


for (int j = 1; j < n; ++j) {
for (int i = n - 1; i >= j; --i) {
backwardTable[i][j] = backwardTable[i][j - 1] - backwardTable[i - 1][j - 1];
}
}
}

// Function to interpolate using the backward difference table


double interpolate(const vector<double>& x, const vector<vector<double>>&
backwardTable, double xi) {
double result = backwardTable.back()[0];
double u = (xi - x.back()) / (x[1] - x[0]);
double term = 1;
int n = backwardTable.size();

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


term *= (u + i - 1) / i;
result += term * backwardTable[n - 1][i];
}
return result;
}

// Function to print the backward difference table


void printTable(const vector<double>& x, const vector<vector<double>>&
backwardTable) {
int n = x.size();
cout << "Backward Difference Table:\n\n";
cout << setw(10) << "x" << setw(15) << "f(x)";

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


cout << setw(15) << "Δ^" << i << "f(x)";
}

cout << "\n\n";

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


cout << setw(10) << x[i];

for (int j = 0; j < i + 1; ++j) {


cout << setw(15) << backwardTable[i][j];
}

cout << "\n";


}
}

int main() {
vector<double> x = {1, 7, 3, 4};
vector<double> y = {2, 4, 5, 8};
vector<vector<double>> backwardTable; // Table to store backward differences

// Calculate the backward difference table


calculateBackwardTable(x, y, backwardTable);

// Print the backward difference table


printTable(x, backwardTable);

double xi = 2.5;
double interpolatedValue = interpolate(x, backwardTable, xi);
cout << "\nInterpolated value at x = " << xi << " is " << interpolatedValue << endl;

return 0;
}

Output
9. Write a program to Implement Divided difference Interpolation.
Code:
#include <iostream>
#include <vector>
using namespace std;

// Function to calculate divided differences


double dividedDifference(const vector<double>& x, const vector<double>& y, int i, int j)
{
if (j == 0)
return y[i];
else
return (dividedDifference(x, y, i + 1, j - 1) - dividedDifference(x, y, i, j - 1)) / (x[i + j] -
x[i]);
}

// Function to calculate interpolated value at a given point


double interpolate(const vector<double>& x, const vector<double>& y, double xi) {
double result = 0;
int n = x.size();
for (int i = 0; i < n; i++) {
double term = dividedDifference(x, y, 0, i);
for (int j = 0; j < i; j++) {
term *= (xi - x[j]);
}
result += term;
}
return result;
}

int main() {
vector<double> x = {1, 2, 3, 4};
vector<double> y = {2, 3, 5, 8};
double xi = 2.5;

// Calculate interpolated value


double interpolatedValue = interpolate(x, y, xi);

// Print interpolated value


cout << "Interpolated value at x = " << xi << " is " << interpolatedValue << endl;

return 0;
}

Output
10. Write a Program to Implement Lagrange’s Interpolation.
Code:
#include <iostream>
#include <vector>
using namespace std;
double lagrangeInterpolation(const vector<double>& x, const vector<double>& y,
double xi) {
double result = 0;
int n = x.size();
for (int i = 0; i < n; ++i) {
double term = y[i];
for (int j = 0; j < n; ++j) {
if (j != i) {
term *= (xi - x[j]) / (x[i] - x[j]);
}
}
result += term;
}
return result;
}
int main() {
// Given data points
vector<double> x = {3, 6, 1, 9};
vector<double> y = {7, 9, 2, 4};
double xi = 2.4; // Point to interpolate
double interpolatedValue = lagrangeInterpolation(x, y, xi);

// Print interpolated value


cout << "Interpolated value at x = " << xi << " is " << interpolatedValue << endl;

return 0;
}
Output
11. Write a program to implement Trapezoidal rule.
Code:
#include <iostream>
#include <cmath>
using namespace std;
double f(double x) {
return sin(x);
}
// Trapezoidal Rule for numerical integration
double trapezoidalRule(double a, double b, int n) {
double h = (b - a) / n; // Step size
double sum = 0.5 * (f(a) + f(b)); // Sum of endpoints

// Summation loop for the middle points


for (int i = 1; i < n; ++i) {
double x = a + i * h; // Current point
sum += f(x);
}
return h * sum; // Return the approximate integral value
}

int main() {
double a = 0.0; // Lower limit of integration
double b = M_PI; // Upper limit of integration (π in this case)
int n = 100; // Number of subintervals
double integral = trapezoidalRule(a, b, n);
// Print the result
cout << "Approximate integral using Trapezoidal Rule: " << integral << endl;

return 0;
}

Output
12. Write a Program to Implement Simpson 1/3 rule.
Code:
#include <iostream>
#include <cmath>
using namespace std;
double f(double x) {
return sin(x)+cos(x);
}
// Simpson's 1/3 Rule for numerical integration
double simpsonsRule(double a, double b, int n) {
double h = (b - a) / n; // Step size
double sum = f(a) + f(b);
for (int i = 1; i < n; i += 2) {
double x = a + i * h; // Current point
sum += 4 * f(x); // Add the function value at current point (with weight 4)
}
// Summation loop for the odd points
for (int i = 2; i < n - 1; i += 2) {
double x = a + i * h; // Current point
sum += 2 * f(x); // Add the function value at current point (with weight 2)
}
return (h / 3) * sum; // Return the approximate integral value
}
int main() {
double a = 0.0; // Lower limit of integration
double b = M_PI; // Upper limit of integration (π in this case)
int n = 100; // Number of subintervals

// Calculate integral using Simpson's 1/3 Rule


double integral = simpsonsRule(a, b, n);
cout << "Approximate integral using Simpson's 1/3 Rule: " << integral << endl;
return 0;
}

Output
13. Write a Program to Implement Simpson 3/8 rule.
Code:
#include <iostream>
#include <cmath>
using namespace std;

double f(double x) {
return sin(x)+cos(x); // Change the function here as needed
}
double simpsonThreeEighthRule(double a, double b, int n) {
double h = (b - a) / n; // Step size
double sum = f(a) + f(b); // Sum of endpoints

// Summation loop for the middle points


for (int i = 1; i < n; ++i) {
double x = a + i * h; // Current point
sum += (i % 3 == 0) ? 2 * f(x) : 3 * f(x); // Add the function value based on the
Simpson's rule
}
return (3 * h / 8) * sum; // Return the approximate integral value
}
int main() {
double a = 0.0; // Lower limit of integration
double b = M_PI; // Upper limit of integration (π in this case)
int n = 6; // Number of subintervals (should be a multiple of 3)

// Calculate integral using Simpson's 3/8 Rule


double integral = simpsonThreeEighthRule(a, b, n);
cout << "Approximate integral using Simpson's 3/8 Rule: " << integral << endl;

return 0;
}

Output
14. Write a Program to Implement Romberg’s method.
Code:
#include <iostream>
#include <cmath>
#include <vector>
using namespace std;

// Function to define the integrand


double f(double x) {
return sin(x)+cos(x);
}

// Trapezoidal Rule for numerical integration


double trapezoidalRule(double a, double b, int n) {
double h = (b - a) / n; // Step size
double sum = 0.5 * (f(a) + f(b)); // Sum of endpoints

// Summation loop for the middle points


for (int i = 1; i < n; ++i) {
double x = a + i * h; // Current point
sum += f(x); // Add the function value at current point
}

return h * sum; // Return the approximate integral value


}

// Romberg's Method for numerical integration


double rombergsMethod(double a, double b, int n) {
vector<vector<double>> R(n + 1, vector<double>(n + 1)); // Romberg table
double h = b - a; // Step size

// First column of the Romberg table (trapezoidal rule)


for (int i = 0; i <= n; ++i) {
R[i][0] = trapezoidalRule(a, b, pow(2, i));
}

// Remaining columns of the Romberg table


for (int j = 1; j <= n; ++j) {
for (int i = j; i <= n; ++i) {
R[i][j] = R[i][j - 1] + (R[i][j - 1] - R[i - 1][j - 1]) / (pow(4, j) - 1);
}
}

return R[n][n]; // Return the approximate integral value


}

int main() {
double a = 0.0; // Lower limit of integration
double b = M_PI; // Upper limit of integration (π in this case)
int n = 8; // Number of iterations (increasing this will improve accuracy)

// Calculate integral using Romberg's Method


double integral = rombergsMethod(a, b, n);

// Print the result


cout << "Approximate integral using Romberg's Method: " << integral << endl;

return 0;
}

Output

You might also like