0% found this document useful (0 votes)
3 views13 pages

NT Practical 1

The document contains multiple C++ programs demonstrating various numerical methods for solving mathematical problems, including root-finding methods (Bisection, Secant, Newton-Raphson), solving systems of nonlinear equations (Newton's method), and solving linear systems (Gauss Thomas, Jacobi, Gauss-Seidel). It also includes cubic spline interpolation, Gaussian quadrature for definite integrals, and the finite difference method for boundary value problems. Each section provides code examples and explanations for the respective numerical methods.

Uploaded by

ksahu33334
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views13 pages

NT Practical 1

The document contains multiple C++ programs demonstrating various numerical methods for solving mathematical problems, including root-finding methods (Bisection, Secant, Newton-Raphson), solving systems of nonlinear equations (Newton's method), and solving linear systems (Gauss Thomas, Jacobi, Gauss-Seidel). It also includes cubic spline interpolation, Gaussian quadrature for definite integrals, and the finite difference method for boundary value problems. Each section provides code examples and explanations for the respective numerical methods.

Uploaded by

ksahu33334
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 13

1.

Find the roots of the equation by Bisection method

#include <iostream>

#include <cmath>

using namespace std;

double f(double x) {

return x*x*x - x - 2;

void bisection(double a, double b, double tolerance = 0.0001, int maxIterations = 100) {

if (f(a) * f(b) >= 0) {

cout << "Invalid interval. f(a) and f(b) must have opposite signs." << endl;

return;

double c;

int iteration = 0;

cout << "Iter\ta\t\tb\t\tc\t\tf(c)" << endl;

while ((b - a) >= tolerance && iteration < maxIterations) {

c = (a + b) / 2;

cout << iteration + 1 << "\t" << a << "\t" << b << "\t" << c << "\t" << f(c) << endl;

if (fabs(f(c)) < tolerance) {

break;

if (f(c) * f(a) < 0) {

b = c;

} else {

a = c;

iteration++;

cout << "\nApproximate root: " << c << endl;


cout << "f(root) = " << f(c) << endl;

cout << "Iterations: " << iteration << endl;

int main() {

double a = 1, b = 2; // Change according to your problem

bisection(a, b);

return 0;

2. Find the roots of the equation by Secant / Regula-Falsi method

#include <iostream>

#include <cmath>

using namespace std;

double f(double x) {

return x*x*x - x - 2;

void regulaFalsi(double a, double b, double tolerance = 0.0001, int maxIterations = 100) {

if (f(a) * f(b) >= 0) {

cout << "Invalid interval. f(a) and f(b) must have opposite signs.\n";

return;

double c;

int iteration = 0;

cout << "Iter\ta\t\tb\t\tc\t\tf(c)\n";

do {

c = (a * f(b) - b * f(a)) / (f(b) - f(a));

cout << iteration + 1 << "\t" << a << "\t" << b << "\t" << c << "\t" << f(c) << endl;
if (fabs(f(c)) < tolerance)

break;

if (f(c) * f(a) < 0)

b = c;

else

a = c;

iteration++;

} while (iteration < maxIterations);

cout << "\nApproximate root: " << c << "\n";

3. Find the roots of the equation by Newton-Raphson method

#include <iostream>

#include <cmath>

using namespace std;

// Define the function f(x)

double f(double x) {

return x*x*x - x - 2; // Example: f(x) = x³ - x - 2

// Define the derivative f'(x)

double df(double x) {

return 3*x*x - 1; // Derivative: f'(x) = 3x² - 1

int main() {

double x = 1.5; // Initial guess

double tol = 0.0001; // Tolerance

int max_iter = 20; // Safety limit


for (int i = 0; i < max_iter; i++) {

double fx = f(x);

double dfx = df(x);

if (fabs(dfx) < 1e-6) {

cout << "Derivative too small, method fails." << endl;

return -1;

double x_new = x - fx / dfx;

if (fabs(x_new - x) < tol) {

cout << "Root found: " << x_new << endl;

return 0;

x = x_new;

cout << "Did not converge in " << max_iter << " iterations." << endl;

return -1;

4. Find the solution of a system of nonlinear equations using Newton’s method

#include <iostream>

#include <cmath>

using namespace std;

// Define functions

double f1(double x, double y) {

return x*x + y*y - 4;

double f2(double x, double y) {


return x*y - 1;

// Partial derivatives

double df1_dx(double x, double y) {

return 2*x;

double df1_dy(double x, double y) {

return 2*y;

double df2_dx(double x, double y) {

return y;

double df2_dy(double x, double y) {

return x;

int main() {

double x = 1.0, y = 1.0; // Initial guesses

double tol = 0.0001;

int max_iter = 20;

for (int i = 0; i < max_iter; i++) {

double J11 = df1_dx(x, y), J12 = df1_dy(x, y);

double J21 = df2_dx(x, y), J22 = df2_dy(x, y);

double F1 = f1(x, y), F2 = f2(x, y);


// Determinant of Jacobian

double det = J11 * J22 - J12 * J21;

if (fabs(det) < 1e-6) {

cout << "Jacobian is singular. Method fails." << endl;

return -1;

// Compute inverse Jacobian and update

double dx = (F1 * J22 - F2 * J12) / det;

double dy = (F2 * J11 - F1 * J21) / det;

x = x - dx;

y = y - dy;

if (fabs(dx) < tol && fabs(dy) < tol) {

cout << "Solution found:\n";

cout << "x = " << x << "\n";

cout << "y = " << y << endl;

return 0;

cout << "Did not converge in " << max_iter << " iterations." << endl;

return -1;

5. Find the solution of a tri-diagonal system using Gauss Thomas method

#include <iostream>

using namespace std;


int main() {

const int n = 4;

double a[n] = {0, -1, -1, -1}; // Lower diagonal (a[0] unused)

double b[n] = {2, 2, 2, 2}; // Main diagonal

double c[n] = {-1, -1, -1, 0}; // Upper diagonal (c[n-1] unused)

double d[n] = {1, 0, 0, 1}; // Right-hand side

// Forward elimination

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

double m = a[i] / b[i - 1];

b[i] = b[i] - m * c[i - 1];

d[i] = d[i] - m * d[i - 1];

// Back substitution

double x[n];

x[n - 1] = d[n - 1] / b[n - 1];

for (int i = n - 2; i >= 0; i--) {

x[i] = (d[i] - c[i] * x[i + 1]) / b[i];

// Print result

cout << "Solution:\n";

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

cout << "x[" << i << "] = " << x[i] << endl;

return 0;

6. Find the solution of a system of equations using Jacobi / Gauss-Seidel method


#include <iostream>
#include <cmath>
#include <vector>

using namespace std;

void jacobiMethod(vector<vector<double>> &A, vector<double> &b, vector<double> &x, int


n, double tol, int maxIter) {
vector<double> x_new(n, 0);
int iter = 0;
double sum = 0;

while (iter < maxIter) {


for (int i = 0; i < n; i++) {
sum = 0;
for (int j = 0; j < n; j++) {
if (i != j)
sum += A[i][j] * x[j];
}
x_new[i] = (b[i] - sum) / A[i][i];
}

// Check for convergence


double error = 0;
for (int i = 0; i < n; i++) {
error += fabs(x_new[i] - x[i]);
}

if (error < tol) {


cout << "Solution found in " << iter + 1 << " iterations." << endl;
break;
}

x = x_new;
iter++;
}

if (iter == maxIter) {
cout << "Max iterations reached!" << endl;
}
}

int main() {
int n = 3;
vector<vector<double>> A = {{4, -1, 0},
{-1, 4, -1},
{0, -1, 3}};
vector<double> b = {15, 10, 10};
vector<double> x = {0, 0, 0}; // Initial guess
double tol = 1e-6; // Tolerance for convergence
int maxIter = 1000; // Maximum number of iterations

jacobiMethod(A, b, x, n, tol, maxIter);

cout << "Solution: ";


for (int i = 0; i < n; i++) {
cout << x[i] << " ";
}
cout << endl;

return 0;
}

7. Find the cubic spline interpolating function


#include <iostream>
#include <vector>
#include <cmath>

using namespace std;

struct Spline {
vector<double> a, b, c, d, x; // Coefficients
};

Spline cubicSplineInterpolation(const vector<double>& x, const vector<double>& y) {


int n = x.size();
Spline spline;
spline.x = x;
spline.a = y;

vector<double> h(n - 1), alpha(n - 1), l(n), mu(n), z(n);

// Calculate the h, alpha, l, mu, and z


for (int i = 0; i < n - 1; i++) {
h[i] = x[i + 1] - x[i];
alpha[i] = (3.0 / h[i]) * (y[i + 1] - y[i]) - (3.0 / h[i - 1]) * (y[i] - y[i - 1]);
}

l[0] = 1;
mu[0] = 0;
z[0] = 0;

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


l[i] = 2 * (x[i + 1] - x[i - 1]) - h[i - 1] * mu[i - 1];
mu[i] = h[i] / l[i];
z[i] = (alpha[i] - h[i - 1] * z[i - 1]) / l[i];
}
l[n - 1] = 1;
z[n - 1] = 0;
spline.c.resize(n);
spline.d.resize(n);

for (int j = n - 2; j >= 0; j--) {


spline.c[j] = z[j] - mu[j] * spline.c[j + 1];
spline.b[j] = (y[j + 1] - y[j]) / h[j] - h[j] * (spline.c[j + 1] + 2 * spline.c[j]) / 3;
spline.d[j] = (spline.c[j + 1] - spline.c[j]) / (3 * h[j]);
}

return spline;
}

double evaluateSpline(const Spline& spline, double t, int i) {


double dx = t - spline.x[i];
return spline.a[i] + spline.b[i] * dx + spline.c[i] * dx * dx + spline.d[i] * dx * dx * dx;
}

int main() {
vector<double> x = {0, 1, 2, 3}; // x-coordinates
vector<double> y = {1, 2, 0, 2}; // y-coordinates

Spline spline = cubicSplineInterpolation(x, y);

double t = 1.5; // Example point to evaluate


int i = 1; // Corresponding interval

double result = evaluateSpline(spline, t, i);


cout << "Spline evaluated at " << t << " is " << result << endl;

return 0;
}
8. Evaluate the approximate value of definite integrals using Gaussian / Romberg integration
#include <iostream>
#include <cmath>

using namespace std;

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

double gaussianQuadrature(double (*f)(double), double a, double b, int n) {


double pi = 3.14159265358979323846;
double result = 0.0;
double weight[] = {0.5773502692, -0.5773502692}; // Example weights for 2-point
Gaussian quadrature
double nodes[] = {0.5773502692, -0.5773502692}; // Example nodes for 2-point Gaussian
quadrature

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


double xi = 0.5 * (b - a) * nodes[i] + 0.5 * (b + a); // Transform nodes to the integration
interval
result += weight[i] * f(xi);
}

result *= 0.5 * (b - a); // Scale by the interval length


return result;
}

int main() {
double a = 0.0, b = 1.0; // Integration limits
int n = 2; // Number of points in the Gaussian quadrature

double result = gaussianQuadrature(f, a, b, n);


cout << "Integral result using Gaussian Quadrature: " << result << endl;

return 0;
}
9.Solve the boundary value problem using finite difference method

#include <iostream>

#include <vector>

using namespace std;

double f(double x) {

return -2.0; // Example: f(x) = -2

vector<double> finiteDifferenceMethod(double (*f)(double), int n, double a, double b) {

double h = (b - a) / (n + 1); // Step size

vector<double> x(n + 2), y(n + 2, 0); // x and y arrays

// Initialize x array
for (int i = 0; i <= n + 1; i++) {

x[i] = a + i * h;

// Set up the matrix for the linear system (A * y = b)

vector<vector<double>> A(n, vector<double>(n, 0));

vector<double> b(n, 0);

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

A[i][i] = -2;

if (i > 0) A[i][i - 1] = 1;

if (i < n - 1) A[i][i + 1] = 1;

b[i] = -f(x[i + 1]) * h * h;

// Solve the linear system using Gaussian elimination or any other method

// For simplicity, we will assume you use a built-in solver here

return y; // Return the solution

int main() {

int n = 10; // Number of intervals

double a = 0.0, b = 1.0; // Boundary conditions

vector<double> y = finiteDifferenceMethod(f, n, a, b);

cout << "Solution at the grid points: ";

for (int i = 0; i < y.size(); i++) {

cout << y[i] << " ";

}
cout << endl;

return 0;

You might also like