0% found this document useful (0 votes)
20 views11 pages

CBNT File

The document contains C++ programs for various numerical methods including the bisection method, Newton-Raphson method, regula falsi method, forward difference method, backward difference method, central difference method, Runge-Kutta method, Gauss forward and backward interpolation methods, and Gauss elimination method. Each program includes function definitions, main execution logic, and example usage. The methods are designed to solve equations, approximate derivatives, and perform interpolation.

Uploaded by

nitinsaini9923
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)
20 views11 pages

CBNT File

The document contains C++ programs for various numerical methods including the bisection method, Newton-Raphson method, regula falsi method, forward difference method, backward difference method, central difference method, Runge-Kutta method, Gauss forward and backward interpolation methods, and Gauss elimination method. Each program includes function definitions, main execution logic, and example usage. The methods are designed to solve equations, approximate derivatives, and perform interpolation.

Uploaded by

nitinsaini9923
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/ 11

Q.1 write a program in c++ for bisection method.

#include <iostream>
#include <cmath>
using namespace std;

double f(double x) {
return x*x - 4; // Example function: f(x) = x^2 - 4
}

double bisection(double a, double b, double tol) {


double c;
if (f(a) * f(b) >= 0) {
cout << "No root in this interval." << endl;
return -1;
}
while ((b - a) / 2.0 > tol) {
c = (a + b) / 2.0;
if (f(c) == 0.0) break;
if (f(c) * f(a) < 0) b = c;
else a = c;
}
return (a + b) / 2.0;
}

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
}

double newtonRaphson(double x0, double tol) {


double x1;
while (true) {
x1 = x0 - f(x0) / f_prime(x0);
if (fabs(x1 - x0) < tol) {
break;
}
x0 = x1;
}
return x1;
}

int main() {
double x0 = 2.0; // Initial guess
double tol = 0.0001; // Tolerance

double root = newtonRaphson(x0, tol);


cout << "The root is approximately: " << root << endl;

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
}

double regulaFalsi(double a, double b, double tol) {


double c;
if (f(a) * f(b) >= 0) {
cout << "The function does not have opposite signs at the endpoints." << endl;
return -1;
}

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;

// Update the interval [a, b] based on the sign of f(c)


if (f(a) * f(c) < 0) {
b = c;
} else {
a = c;
}
}

return c; // Return the root approximation


}

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;

// Define the function f(x) whose derivative is to be calculated


double f(double x) {
return x * x - 4; // Example function: f(x) = x^2 - 4
}

// Forward difference method to approximate the first derivative


double forwardDifference(double x, double h) {
return (f(x + h) - f(x)) / h; // Approximate the derivative
}

int main() {
double x = 2.0; // Point at which the derivative is to be calculated
double h = 0.001; // Step size for forward difference

// Calculate the forward difference approximation of the derivative at x


double derivative = forwardDifference(x, h);

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;

// Define the function f(x) whose derivative is to be calculated


double f(double x) {
return x * x - 4; // Example function: f(x) = x^2 - 4
}

// Backward difference method to approximate the first derivative


double backwardDifference(double x, double h) {
return (f(x) - f(x - h)) / h; // Approximate the derivative
}

int main() {
double x = 2.0; // Point at which the derivative is to be calculated
double h = 0.001; // Step size for backward difference

// Calculate the backward difference approximation of the derivative at x


double derivative = backwardDifference(x, h);

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>

// Function whose derivative we want to approximate


double f(double x) {
// Example function: f(x) = x^2
return x * x;
}

// Function to approximate the derivative using central difference


double central_difference(double (*func)(double), double x, double h) {
return (func(x + h) - func(x - h)) / (2 * h);
}

int main() {
double x; // Point at which to compute the derivative
double h = 0.001; // Step size for approximation

// Input the point where we want the derivative


std::cout << "Enter the point x where the derivative is to be computed: ";
std::cin >> x;

// Compute the derivative using central difference


double derivative = central_difference(f, x, h);

// Output the result


std::cout << "The approximate derivative at x = " << x << " is: " << derivative << std::endl;

return 0;
}
Q.7 write a program in c++ for runga kutta method.

#include <iostream>
#include <iomanip>
#include <cmath>

// Define the function f(x, y) = dy/dx


// Example: y' = x + y
// Modify this function for your specific ODE
inline double f(double x, double y) {
return x + y;
}

// Runge-Kutta 4th Order Method


void rungeKutta(double x0, double y0, double xEnd, double h) {
double x = x0;
double y = y0;

std::cout << "x\t\t y\n";


std::cout << std::fixed << std::setprecision(6);
std::cout << x << "\t" << y << "\n";

while (x < xEnd) {


double k1 = h * f(x, y);
double k2 = h * f(x + h / 2.0, y + k1 / 2.0);
double k3 = h * f(x + h / 2.0, y + k2 / 2.0);
double k4 = h * f(x + h, y + k3);

// Update y and x
y += (k1 + 2 * k2 + 2 * k3 + k4) / 6.0;
x += h;

std::cout << x << "\t" << y << "\n";


}
}

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

// Run the Runge-Kutta Method


rungeKutta(x0, y0, xEnd, h);

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];
}

// Fill the difference table


for (int j = 1; j < n; j++) {
for (int i = 0; i < n - j; i++) {
diffTable[i][j] = diffTable[i + 1][j - 1] - diffTable[i][j - 1];
}
}
std::cout << "\nDifference Table:\n";
for (int i = 0; i < n; i++) {
for (int j = 0; j < n - i; j++) {
std::cout << std::setw(10) << diffTable[i][j] << " ";
}
std::cout << "\n"
}
double h = x[1] - x[0];
double p = (value - x[0]) / h;
double result = diffTable[0][0];
double term = 1.0;

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


term *= (p - (i - 1)) / i;
result += term * diffTable[0][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;

// Gauss Backward Interpolation formula


double result = diffTable[n - 1][0];
double term = 1.0;

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


term *= (p + (i - 1)) / i;
result += term * diffTable[n - 1][i]; }
std::cout << "\nEstimated value at x = " << value << " is " << result << "\n";
}
int main() {
// Input data points (x and y values)
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;
gaussBackwardInterpolation(x, y, value);
return 0;
}
Q.10 write a program in c++ for gauss elimination method.

#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;
}

You might also like