Cmlab
Cmlab
double func(double x)
{
return x * x;
}
double fibNo(int x)
{
if (x == 0 || x == 1)
{
return x;
}
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>
int main() {
vector<double> x = {1, 7, 3, 4};
vector<double> y = {2, 4, 5, 8};
return 0;
}
Output
8. Write a Program to Implement Backward Interploation.
Code:
#include <iostream>
#include <iomanip>
#include <vector>
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
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;
int main() {
vector<double> x = {1, 2, 3, 4};
vector<double> y = {2, 3, 5, 8};
double xi = 2.5;
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);
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
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
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
return 0;
}
Output
14. Write a Program to Implement Romberg’s method.
Code:
#include <iostream>
#include <cmath>
#include <vector>
using namespace std;
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)
return 0;
}
Output