0% found this document useful (0 votes)
14 views18 pages

Faaizcm

The document describes programs to implement different interpolation methods: Gauss elimination, power method for largest eigenvalue, forward interpolation, backward interpolation, divided difference interpolation, and Lagrange interpolation. The programs are written in C++ and demonstrate how to calculate the interpolation polynomial and interpolate values for each method.

Uploaded by

Nilesh Kumar
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)
14 views18 pages

Faaizcm

The document describes programs to implement different interpolation methods: Gauss elimination, power method for largest eigenvalue, forward interpolation, backward interpolation, divided difference interpolation, and Lagrange interpolation. The programs are written in C++ and demonstrate how to calculate the interpolation polynomial and interpolate values for each method.

Uploaded by

Nilesh Kumar
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/ 18

Practical 7

Aim: Write a program to implement Gauss – Elimination method.


//Faaiz Khan //00116407223

#include <iostream>
#include <vector>
// Function to perform Gauss Elimination
std::vector<double> gauss_elimination(std::vector<std::vector<double>>& A, std::vector<double>&
B) { int n = A.size();

// Forward elimination for (int i = 0; i < n - 1; ++i) { for


(int j = i + 1; j < n; ++j) { double ratio = A[j][i]
/ A[i][i]; for (int k = i; k < n; ++k) {
A[j][k] -= ratio * A[i][k];
}
B[j] -= ratio * B[i];
}
}

// Back substitution std::vector<double>


X(n);
X[n - 1] = B[n - 1] / A[n - 1][n - 1]; for (int i = n
- 2; i >= 0; --i) { double sum = 0; for (int j = i +
1; j < n; ++j) { sum += A[i][j] * X[j];
}
X[i] = (B[i] - sum) / A[i][i]; }

return X;
}

int main() {
// Example system of linear equations:
// 2x + y - z = 8
// -3x - y + 2z = -11
// -2x + y + 2z = -3 std::vector<std::vector<double>> A = {{2, 1, -1},
{-3, -1, 2},
{-2, 1, 2}};
std::vector<double> B = {8, -11, -3}; std::vector<double> solution = gauss_elimination(A,

B);

// Output the solution std::cout <<


"Solution:\n";
for (int i = 0; i < solution.size(); ++i) { std::cout << "x" << i + 1
<< " = " << solution[i] << std::endl;
}
return 0;
}

Practical 12
Aim: Write a program to implement Power method-
largest Eigen value.
//Faaiz Khan //00116407223

#include <iostream>

#include <vector> #include


<cmath>

using namespace std;


// Function to multiply a matrix with a vector
vector<double> matrixVectorMultiply(vector<vector<double>>& matrix,
vector<double>& vec) { int n = matrix.size(); vector<double>
result(n, 0);

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


n; ++j) { result[i] += matrix[i][j] * vec[j]; }
}

return result;
}

// Function to find the largest eigenvalue using the Power Method double
powerMethod(vector<vector<double>>& matrix, double epsilon = 1e-6, int maxIterations
=
1000) { int n = matrix.size(); vector<double> eigenVec(n, 1); //
Initial guess for eigenvector

double prevEigenValue = 0; double eigenValue


= 0;

for (int i = 0; i < maxIterations; ++i) { vector<double> nextEigenVec =


matrixVectorMultiply(matrix, eigenVec);

// Normalizing eigenvector double norm


= 0;
for (int j = 0; j < n; ++j) { norm +=
nextEigenVec[j] * nextEigenVec[j];
}
norm = sqrt(norm);
for (int j = 0; j < n; ++j) {
nextEigenVec[j] /= norm;
}

eigenValue = 0;
for (int j = 0; j < n; ++j) { eigenValue +=
nextEigenVec[j] * eigenVec[j];
}

if (fabs(eigenValue - prevEigenValue) < epsilon) { break; }

eigenVec = nextEigenVec; prevEigenValue


= eigenValue;
}

return eigenValue;
}

int main() { //
Example usage
vector<vector<double>> matrix = {{4, -2, 1},
{3, 6, -4},
{2, 1, 8}};

double largestEigenValue = powerMethod(matrix);

cout << "Largest eigenvalue: " << largestEigenValue << endl;

return 0;
}
Practical 13

Aim: Write a program to implement Forward


Interpolation.
//Faaiz Khan //00116407223

// CPP Program to interpolate using


// newton forward interpolation
#include <bits/stdc++.h>
using namespace std;

// calculating u mentioned in the formula float u_cal(float


u, int n)
{ float temp = u;
for (int i = 1; i < n; i++) temp = temp *
(u - i);
return temp;
}

// calculating factorial of given number n int fact(int n)


{ int f = 1;
for (int i = 2; i <= n; i++) f *= i;
return f;
}

int main()
{
// Number of values given int n =
4; float x[] = { 45, 50, 55,
60 };

// y[][] is used for difference table //


with y[][0] used for input float y[n][n];
y[0][0] = 0.7071;
y[1][0] = 0.7660;
y[2][0] = 0.8192;
y[3][0] = 0.8660;
// Calculating the forward difference
// table
for (int i = 1; i < n; i++) { for (int j = 0; j < n - i;
j++) y[j][i] = y[j + 1][i - 1] - y[j][i - 1];
}

// Displaying the forward difference table for (int i = 0; i


< n; i++) { cout << setw(4) << x[i]
<< "\t"; for (int j = 0; j < n - i; j++)
cout << setw(4)
<< y[i][j]
<< "\t";
cout << endl;
}

// Value to interpolate at float value =


52;

// initializing u and sum float sum =


y[0][0];
float u = (value - x[0]) / (x[1] - x[0]); for (int i = 1; i < n;
i++) {
sum = sum + (u_cal(u, i) * y[0][i]) / fact(i);
}

cout << "\n Value at " << value << " is "
<< sum << endl;
return 0;
}
Practical 14
Aim: Write a program to implement Backward
interpolation.
//Faaiz Khan //00116407223

// CPP Program to interpolate using


// newton backward interpolation
#include <bits/stdc++.h>
using namespace std;

// Calculation of u mentioned in formula float u_cal(float


u, int n)
{ float temp = u;
for (int i = 1; i < n; i++) temp = temp *
(u + i);
return temp;
}

// Calculating factorial of given n int fact(int n)


{ int f = 1;
for (int i = 2; i <= n; i++) f *= i;
return f;
}

int main()
{
// number of values given int n =
5;
float x[] = { 1891, 1901, 1911,
1921, 1931 };

// y[][] is used for difference


// table and y[][0] used for input float
y[n][n]; y[0][0] = 46; y[1][0] = 66; y[2][0] =
81; y[3][0] = 93; y[4][0] = 101;

// Calculating the backward difference table for (int i = 1; i <


n; i++) { for (int j = n - 1; j >= i; j--)
y[j][i] = y[j][i - 1] - y[j - 1][i - 1];
}
// Displaying the backward difference table for (int i = 0; i
< n; i++) { for (int j = 0; j <= i; j++) cout << setw(4)
<< y[i][j]
<< "\t";
cout << endl;
}

// Value to interpolate at float value =


1925;

// Initializing u and sum float sum =


y[n - 1][0];
float u = (value - x[n - 1]) / (x[1] - x[0]); for (int i = 1; i < n; i++)
{ sum = sum + (u_cal(u, i) * y[n - 1][i]) / fact(i);
}

cout << "\n Value at " << value << " is "
<< sum << endl;
return 0; }
Practical 15
Aim: Write a program to implement Divided
difference interpolation.
//Faaiz Khan //00116407223

#include <iostream>

#include <vector> class


DividedDifferenceInterpolation { private:
std::vector<double> x_values;
std::vector<double> y_values;

public:
DividedDifferenceInterpolation(const std::vector<double>& x, const
std::vector<double>& y) {
x_values = x;
y_values = y;
}

double divided_difference(int start, int end) { if (start == end)


return y_values[start]; else
return (divided_difference(start + 1, end) - divided_difference(start, end -
1)) /
(x_values[end] - x_values[start]);
}

double interpolate(double x) { double


result = 0;
for (int i = 0; i < x_values.size(); i++) { double term =
divided_difference(0, i); for (int j = 0; j < i; j++) { term
*= (x - x_values[j]);
}
result += term;
}
return result;
}
};

int main() { std::vector<double> x = {1, 2,


3, 4, 5};
std::vector<double> y = {0, 1, 8, 27, 64}; // Example data: y =
x^3

DividedDifferenceInterpolation interp(x, y);


// Interpolating at x = 2.5 double x_interpolated = 2.5;
double y_interpolated =
interp.interpolate(x_interpolated);

std::cout << "Interpolated value at x = " << x_interpolated << " is " << y_interpolated <<
std::endl;

return 0;
}
Practical 16
Aim: Write a program to implement
Lagrange’s interpolation.
//Faaiz Khan //00116407223

#include <iostream>

#include <vector> using


namespace std;

class LagrangeInterpolation { private:


vector<double> x_values;
vector<double> y_values; int n;

public:
// Constructor
LagrangeInterpolation(vector<double> x, vector<double> y) { x_values = x;
y_values = y; n = x.size();
}

// Calculate the interpolation polynomial double


interpolate(double x) { double result = 0;
for (int i = 0; i < n; i++) { double term = y_values[i]; for (int j
= 0; j < n; j++) { if (j != i) { term = term * (x -
x_values[j]) / (x_values[i] -
x_values[j]);
}
}
result += term;
}
return result;
}
}; f

int main() {
// Sample data points vector<double> x = {1,
2, 3, 4};
vector<double> y = {5, 9, 3, 7};
// Create an instance of LagrangeInterpolation LagrangeInterpolation interpolation(x,
y);
// Test interpolation at x = 2.5
double interpolated_value = interpolation.interpolate(2.5); cout << "Interpolated value
at x = 2.5: " << interpolated_value
<< endl;

return 0;
}
Practical 17
Aim: Write a program to implement
Trapezoidal rule.
//Faaiz Khan //00116407223
#include <iostream>
#include <cmath>
#include <functional> using

namespace std;

// Function to integrate double f(double x) {


return sin(x); // Example function: sin(x)
}

// Trapezoidal Rule for numerical integration double


trapezoidal_rule(double a, double b, int n, function<double(double)>
func) { double h = (b - a) / n;
double sum = 0.5 * (func(a) + func(b)); for (int i = 1; i <
n; ++i) { double x = a + i * h; sum += func(x);
}
return sum * h;
}

int main() { double a = 0; // Lower limit of integration double b =


M_PI; // Upper limit of integration int n = 1000; // Number of
intervals

double integral = trapezoidal_rule(a, b, n, f);

cout << "Approximation of integral using Trapezoidal Rule: " << integral << endl;

return 0;

}
Practical 18
Aim: Write a program to implement Simpson 1/3 rule
//Faaiz Khan //00116407223
#include <iostream>
#include <cmath>
#include <functional>
using namespace std;

// Function to integrate double f(double x) {


return sin(x); // Example function: sin(x)
}

// Simpson's 1/3 Rule for numerical integration double


simpson_one_third(double a, double b, int n,
function<double(double)> func) { double h = (b - a) /
n;
double sum = func(a) + func(b); for (int i = 1;
i < n; ++i) { double x = a + i * h; if (i % 2
== 0) { sum += 2 *
func(x);
} else { sum += 4 *
func(x);
}
}
return sum * h / 3;
}

int main() { double a = 0; // Lower limit of integration double b =


M_PI; // Upper limit of integration int n = 1000; // Number of
intervals

double integral = simpson_one_third(a, b, n, f);

cout << "Approximation of integral using Simpson's 1/3 Rule: " <<
integral << endl;

return 0;
}
Practical 19
Aim : Write a program to implement Simpson 3/8 rule.
//Faaiz Khan //00116407223
#include <iostream>
#include <cmath>
#include <functional> using

namespace std;

// Function to integrate double f(double x) {


return sin(x); // Example function: sin(x)
}

// Simpson's 3/8 Rule for numerical integration


double simpson_three_eighth(double a, double b, int n, function<double(double)> func) {
double h = (b - a) / n;
double sum = func(a) + func(b); for (int i = 1;
i < n; ++i) { double x = a + i * h; if (i % 3
== 0) { sum += 2 *
func(x);
} else { sum += 3 *
func(x);
}
}
return sum * 3 * h / 8;
}

int main() { double a = 0; // Lower limit of integration double b =


M_PI; // Upper limit of integration int n = 1000; // Number of
intervals

double integral = simpson_three_eighth(a, b, n, f);

cout << "Approximation of integral using Simpson's 3/8 Rule: " <<
integral << endl;

return 0;
}

You might also like