CBNT File
CBNT File
#include <iostream>
#include <cmath>
using namespace std;
double f(double x) {
return x*x - 4; // Example function: f(x) = x^2 - 4
}
int main() {
double a = 0, b = 3, tol = 0.001;
double root = bisection(a, b, tol);
if (root != -1) {
cout << "The root is approximately: " << root << endl;
}
return 0;
}
Q.2 write a program in c++ for newton rephson method.
#include <iostream>
#include <cmath>
using namespace std;
double f(double x) {
return x*x - 4; // Example function: f(x) = x^2 - 4
}
double f_prime(double x) {
return 2*x; // Derivative of f(x) = x^2 - 4 is f'(x) = 2x
}
int main() {
double x0 = 2.0; // Initial guess
double tol = 0.0001; // Tolerance
return 0;
}
Q.3 write a program in c++ for regula falsi method.
#include <iostream>
#include <cmath>
using namespace std;
double f(double x) {
return x * x - 4; // Example function: f(x) = x^2 - 4
}
while (true) {
c = (a * f(b) - b * f(a)) / (f(b) - f(a)); // Compute the root approximation
if (fabs(f(c)) < tol) // If f(c) is sufficiently close to zero, we have found the root
break;
int main() {
double a = 0, b = 3, tol = 0.001; // Initial interval and tolerance
double root = regulaFalsi(a, b, tol);
if (root != -1) {
cout << "The root is approximately: " << root << endl;
}
return 0;
}
Q.4 write a program in c++ for forward difference method.
#include <iostream>
#include <cmath>
using namespace std;
int main() {
double x = 2.0; // Point at which the derivative is to be calculated
double h = 0.001; // Step size for forward difference
cout << "The derivative at x = " << x << " is approximately: " << derivative << endl;
return 0;
}
Q.5 write a program in c++ for backward difference method.
#include <iostream>
#include <cmath>
using namespace std;
int main() {
double x = 2.0; // Point at which the derivative is to be calculated
double h = 0.001; // Step size for backward difference
cout << "The derivative at x = " << x << " is approximately: " << derivative << endl;
return 0;
}
Q.6 write a program in c++ for central difference method.
#include <iostream>
#include <cmath>
int main() {
double x; // Point at which to compute the derivative
double h = 0.001; // Step size for approximation
return 0;
}
Q.7 write a program in c++ for runga kutta method.
#include <iostream>
#include <iomanip>
#include <cmath>
// Update y and x
y += (k1 + 2 * k2 + 2 * k3 + k4) / 6.0;
x += h;
int main() {
// Initial conditions
double x0 = 0.0; // Initial x value
double y0 = 1.0; // Initial y value (y(x0))
double xEnd = 2.0; // Endpoint of x
double h = 0.1; // Step size
return 0;
}
Q.8 write a program in c++ for gauss forward formula method.
#include <iostream>
#include <vector>
#include <iomanip>
long long factorial(int n) {
if (n <= 1) return 1;
return n * factorial(n – 1)
void gaussForwardInterpolation(std::vector<double> x, std::vector<double> y, double value) {
int n = x.size();
std::vector<std::vector<double>> diffTable(n, std::vector<double>(n))
for (int i = 0; i < n; i++) {
diffTable[i][0] = y[i];
}
std::cout << "\nEstimated value at x = " << value << " is " << result << "\n";
}
int main()
std::vector<double> x = {1, 2, 3, 4, 5};
std::vector<double> y = {1, 8, 27, 64, 125}; // Example: y = x^3
double value;
std::cout << "Enter the value of x for interpolation: ";
std::cin >> value;
gaussForwardInterpolation(x, y, value);
return 0;
}
Q.9 write a program in c++ for gauss backward formula method.
#include <iostream>
#include <vector>
#include <iomanip>
long long factorial(int n) {
if (n <= 1) return 1;
return n * factorial(n - 1);
}
void gaussBackwardInterpolation(std::vector<double> x, std::vector<double> y, double value) {
int n = x.size();
std::vector<std::vector<double>> diffTable(n, std::vector<double>(n));
for (int i = 0; i < n; i++) {
diffTable[i][0] = y[i];
}
for (int j = 1; j < n; j++) {
for (int i = n - 1; i >= j; i--) {
diffTable[i][j] = diffTable[i][j - 1] - diffTable[i - 1][j - 1];
}
}
std::cout << "\nDifference Table:\n";
for (int i = 0; i < n; i++) {
for (int j = 0; j <= i; j++) {
std::cout << std::setw(10) << diffTable[i][j] << " ";
}
std::cout << "\n";
}
// Calculating p
double h = x[1] - x[0];
double p = (value - x[n - 1]) / h;
#include <iostream>
#include <vector>
#include <iomanip>
#include <cmath>
void gaussElimination(std::vector<std::vector<double>>& matrix, std::vector<double>& results) {
int n = matrix.size()
for (int i = 0; i < n; i++) {
int maxRow = i;
for (int k = i + 1; k < n; k+s+) {
if (fabs(matrix[k][i]) > fabs(matrix[maxRow][i])) {
maxRow = k;}}
for (int k = i; k < n; k++) {
std::swap(matrix[i][k], matrix[maxRow][k]);
}
std::swap(results[i], results[maxRow]);
for (int k = i + 1; k < n; k++) {
double factor = matrix[k][i] / matrix[i][i];
for (int j = i; j < n; j++) {
matrix[k][j] -= factor * matrix[i][j];}
results[k] -= factor * results[i];}
}
std::vector<double> solution(n);
for (int i = n - 1; i >= 0; i--) {
solution[i] = results[i];
for (int j = i + 1; j < n; j++) {
solution[i] -= matrix[i][j] * solution[j];}
solution[i] /= matrix[i][i];
}
std::cout << "\nSolution:\n";
for (int i = 0; i < n; i++) {
std::cout << "x" << i + 1 << " = " << std::setprecision(6) << solution[i] << "\n";
}}
int main() {
int n;
std::cout << "Enter the number of equations: ";
std::cin >> n;
std::vector<std::vector<double>> matrix(n, std::vector<double>(n));
std::vector<double> results(n);
std::cout << "Enter the coefficients of the matrix row-wise (including RHS values):\n"; for (int i = 0; i
< n; i++) {
for (int j = 0; j < n; j++) {
std::cin >> matrix[i][j];}
std::cin >> results[i];}
gaussElimination(matrix, results);
return 0;
}