NT Practical 1
NT Practical 1
#include <iostream>
#include <cmath>
double f(double x) {
return x*x*x - x - 2;
cout << "Invalid interval. f(a) and f(b) must have opposite signs." << endl;
return;
double c;
int iteration = 0;
c = (a + b) / 2;
cout << iteration + 1 << "\t" << a << "\t" << b << "\t" << c << "\t" << f(c) << endl;
break;
b = c;
} else {
a = c;
iteration++;
int main() {
bisection(a, b);
return 0;
#include <iostream>
#include <cmath>
double f(double x) {
return x*x*x - x - 2;
cout << "Invalid interval. f(a) and f(b) must have opposite signs.\n";
return;
double c;
int iteration = 0;
do {
cout << iteration + 1 << "\t" << a << "\t" << b << "\t" << c << "\t" << f(c) << endl;
if (fabs(f(c)) < tolerance)
break;
b = c;
else
a = c;
iteration++;
#include <iostream>
#include <cmath>
double f(double x) {
double df(double x) {
int main() {
double fx = f(x);
return -1;
return 0;
x = x_new;
cout << "Did not converge in " << max_iter << " iterations." << endl;
return -1;
#include <iostream>
#include <cmath>
// Define functions
// Partial derivatives
return 2*x;
return 2*y;
return y;
return x;
int main() {
return -1;
x = x - dx;
y = y - dy;
return 0;
cout << "Did not converge in " << max_iter << " iterations." << endl;
return -1;
#include <iostream>
const int n = 4;
double a[n] = {0, -1, -1, -1}; // Lower diagonal (a[0] unused)
double c[n] = {-1, -1, -1, 0}; // Upper diagonal (c[n-1] unused)
// Forward elimination
// Back substitution
double x[n];
// Print result
cout << "x[" << i << "] = " << x[i] << endl;
return 0;
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
return 0;
}
struct Spline {
vector<double> a, b, c, d, x; // Coefficients
};
l[0] = 1;
mu[0] = 0;
z[0] = 0;
return spline;
}
int main() {
vector<double> x = {0, 1, 2, 3}; // x-coordinates
vector<double> y = {1, 2, 0, 2}; // y-coordinates
return 0;
}
8. Evaluate the approximate value of definite integrals using Gaussian / Romberg integration
#include <iostream>
#include <cmath>
double f(double x) {
return x * x; // Example: f(x) = x^2
}
int main() {
double a = 0.0, b = 1.0; // Integration limits
int n = 2; // Number of points in the Gaussian quadrature
return 0;
}
9.Solve the boundary value problem using finite difference method
#include <iostream>
#include <vector>
double f(double x) {
// Initialize x array
for (int i = 0; i <= n + 1; i++) {
x[i] = a + i * h;
A[i][i] = -2;
if (i > 0) A[i][i - 1] = 1;
if (i < n - 1) A[i][i + 1] = 1;
// Solve the linear system using Gaussian elimination or any other method
int main() {
}
cout << endl;
return 0;