#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
double func(double x) {
return sin(x) - log(x) + exp(x);
}
double trapezoidalRule(double a, double b, int n) {
double h = (b - a) / n;
double integral = func(a) + func(b);
for (int i = 1; i < n; i++) {
double x = a + i * h;
integral += 2 * func(x); // Coefficient 2 for intermediate terms
}
integral = (h / 2) * integral;
return integral;
}
int main() {
double a, b;
int n;
cout << "Enter the lower limit of integration (a): ";
cin >> a;
cout << "Enter the upper limit of integration (b): ";
cin >> b;
cout << "Enter the number of sub-intervals (n): ";
cin >> n;
double result = trapezoidalRule(a, b, n);
cout << fixed << setprecision(6);
cout << "The approximate value of the integral is: " << result << endl;
return 0;
}
Output:
PS C:\Users\aadar\Desktop\Coding\C++\BE 3rd Sem\Practice> cd "c:\Users\aadar\
Desktop\Coding\C++\BE 3rd Sem\Practice\" ; if ($?) { g++ [Link]
-o tempCodeRunnerFile } ; if ($?) { .\tempCodeRunnerFile }
Enter the lower limit of integration (a): 1
Enter the upper limit of integration (b): 2
Enter the number of sub-intervals (n): 4
The approximate value of the integral is: 5.262839
PS C:\Users\aadar\Desktop\Coding\C++\BE 3rd Sem\Practice>
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
double func(double x) {
return sin(x) - log(x) + exp(x);
}
double simpsonThreeEighth(double a, double b, int n) {
// Calculate step size
double h = (b - a) / n;
double integral = func(a) + func(b);
for (int i = 1; i < n; i++) {
double x = a + i * h;
if (i % 3 == 0) {
integral += 2 * func(x);
} else {
integral += 3 * func(x);
}
}
integral = (3 * h / 8) * integral;
return integral;
}
int main() {
double a, b;
int n;
cout << "Enter the lower limit of integration (a): ";
cin >> a;
cout << "Enter the upper limit of integration (b): ";
cin >> b;
cout << "Enter the number of sub-intervals (n must be a multiple of 3): ";
cin >> n;
if (n % 3 != 0) {
cout << "Error: Number of sub-intervals (n) must be a multiple of 3." <<
endl;
return 1;
}
double result = simpsonThreeEighth(a, b, n);
cout << fixed << setprecision(6);
cout << "The approximate value of the integral is: " << result << endl;
return 0;
}
Output:
PS C:\Users\aadar\Desktop\Coding\C++\BE 3rd Sem\Practice\Simpson_3> cd "c:\Users\aadar\Desktop\Coding\C++\BE
3rd Sem\Practice\Simpson_3\" ; if ($?) { g++ [Link] -o tempCodeRunnerFile } ; if ($?) { .\
tempCodeRunnerFile }
Enter the lower limit of integration (a): 1
Enter the upper limit of integration (b): 2
Enter the number of sub-intervals (n must be a multiple of 3): 6
The approximate value of the integral is: 5.240999
PS C:\Users\aadar\Desktop\Coding\C++\BE 3rd Sem\Practice\Simpson_3>
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
double func(double x) {
return 1 / (1 + x * x);
}
double simpsonOneThird(double a, double b, int n) {
double h = (b - a) / n;
double integral = func(a) + func(b);
for (int i = 1; i < n; i++) {
double x = a + i * h;
if (i % 2 == 0) {
integral += 2 * func(x);
} else {
integral += 4 * func(x);
}
}
integral = (h / 3) * integral;
return integral;
}
int main() {
double a, b;
int n;
cout << "Enter the lower limit of integration (a): ";
cin >> a;
cout << "Enter the upper limit of integration (b): ";
cin >> b;
cout << "Enter the number of sub-intervals (n must be even): ";
cin >> n;
if (n % 2 != 0) {
cout << "Error: Number of sub-intervals (n) must be even." << endl;
return 1;
}
double result = simpsonOneThird(a, b, n);
cout << fixed << setprecision(6);
cout << "The approximate value of the integral is: " << result << endl;
return 0;
}
Output:
S C:\Users\aadar\Desktop\Coding\C++\BE 3rd Sem\Practice\Simpson_3> cd "c:\Users\aadar\Desktop\Coding\C++\BE 3rd
Sem\Practice\Simpson_3\Simpson_1\" ; if ($?) { g++ [Link] -o tempCodeRunnerFile } ; if ($?) { .\
tempCodeRunnerFile }
Enter the lower limit of integration (a): 0
Enter the upper limit of integration (b): 1
Enter the number of sub-intervals (n must be even): 6
The approximate value of the integral is: 0.785398
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
// Define the differential equation dy/dx = f(x, y)
double func(double x, double y) {
return y - x * x + 1;
}
void eulerMethod(double x0, double y0, double h, int n) {
double x = x0;
double y = y0;
cout << fixed << setprecision(6);
cout << "Step\t" << "x\t\t" << "y\n";
cout << "-----------------------------------\n";
for (int i = 0; i <= n; ++i) {
cout << i << "\t" << x << "\t" << y << endl;
y = y + h * func(x, y); // Euler formula: y_{n+1} = y_n + h*f(x_n, y_n)
x = x + h; // Increment x by step size h
}
}
int main() {
double x0, y0, h;
int n;
cout << "Enter the initial value of x (x0): ";
cin >> x0;
cout << "Enter the initial value of y (y0): ";
cin >> y0;
cout << "Enter the step size (h): ";
cin >> h;
cout << "Enter the number of steps (n): ";
cin >> n;
eulerMethod(x0, y0, h, n);
return 0;
}
Output:
#include <iostream>
#include <iomanip>
using namespace std;
double func(double x, double y) {
return x * exp(-x * x) - 2 * y;
}
void rungeKutta(double x0, double y0, double h, int n) {
double x = x0;
double y = y0;
cout << fixed << setprecision(6);
cout << "Step\t" << "x\t\t" << "y\n";
cout << "-----------------------------------\n";
for (int i = 0; i <= n; ++i) {
cout << i << "\t" << x << "\t" << y << endl;
double k1 = h * func(x, y);
double k2 = h * func(x + h / 2, y + k1 / 2);
double k3 = h * func(x + h / 2, y + k2 / 2);
double k4 = h * func(x + h, y + k3);
y = y + (k1 + 2 * k2 + 2 * k3 + k4) / 6;
x = x + h;
}
}
int main() {
double x0 = 0.0; // Initial value of x
double y0 = 1.0; // Initial value of y
double h; // Step size
int n; // Number of steps
cout << "Enter the step size (h): ";
cin >> h;
cout << "Enter the number of steps (n): ";
cin >> n;
rungeKutta(x0, y0, h, n);
return 0;
}
Output:
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
void gaussElimination(double **matrix, double *result, int n) {
for (int i = 0; i < n; i++) {
for (int k = i + 1; k < n; k++) {
if (fabs(matrix[i][i]) < fabs(matrix[k][i])) {
for (int j = 0; j < n + 1; j++) {
swap(matrix[i][j], matrix[k][j]);
}
}
}
for (int k = i + 1; k < n; k++) {
double factor = matrix[k][i] / matrix[i][i];
for (int j = i; j < n + 1; j++) {
matrix[k][j] -= factor * matrix[i][j];
}
}
}
for (int i = n - 1; i >= 0; i--) {
result[i] = matrix[i][n];
for (int j = i + 1; j < n; j++) {
result[i] -= matrix[i][j] * result[j];
}
result[i] /= matrix[i][i];
}
}
int main() {
int n;
cout << "Enter the number of equations: ";
cin >> n;
double **matrix = new double *[n];
for (int i = 0; i < n; i++) {
matrix[i] = new double[n + 1];
}
double *result = new double[n];
cout << "Enter the coefficients of the augmented matrix (A|b):\n";
for (int i = 0; i < n; i++) {
for (int j = 0; j < n + 1; j++) {
cin >> matrix[i][j];
}
}
gaussElimination(matrix, result, n);
cout << fixed << setprecision(6);
cout << "Solution:\n";
for (int i = 0; i < n; i++) {
cout << "x" << i + 1 << " = " << result[i] << endl;
}
for (int i = 0; i < n; i++) {
delete[] matrix[i];
}
delete[] matrix;
delete[] result;
return 0;
}
Output: