0% found this document useful (0 votes)
18 views34 pages

CBNST 1

Computer based numerical and statistical techniques notes for more information please visit the profile
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)
18 views34 pages

CBNST 1

Computer based numerical and statistical techniques notes for more information please visit the profile
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/ 34

/*

Name : Saurav Kumar


University Roll Number: 2103058
Section: F ( 63 )
Q.1) WAP in C to find the Absolute Error, Relative Error and Percentage Error.

*/

#include <stdio.h>

#include <math.h>

int main() {

float actual_value, measured_value;

float absolute_error, relative_error, percentage_error;

printf("Enter the actual value: ");

scanf("%f", &actual_value);

printf("Enter the measured value: ");

scanf("%f", &measured_value);

absolute_error = fabs(actual_value - measured_value);

relative_error = absolute_error / fabs(actual_value);

percentage_error = relative_error * 100;

printf("Absolute Error: %f\n", absolute_error);

printf("Relative Error: %f\n", relative_error);

printf("Percentage Error: %f%%\n", percentage_error);

return 0;

}
OUTPUT :
/*
Name : Saurav Kumar
University Roll Number: 2103058
Section: F ( 63 )
Q.2) WAP in C to find the approximate root for the equation x3-4x-9=0 up to 2 decimal
places using Bisection method.

*/

#include <stdio.h>

#include <math.h>

#define EPSILON 0.01

float f(float x) {

return x*x*x - 4*x - 9;

void bisection(float a, float b) {

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

printf("Invalid input! Root does not exist between given intervals.\n");

return;

float c = a;

while ((b - a) >= EPSILON) {

c = (a + b) / 2;

if (f(c) == 0.0) {

break;

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

b = c;

} else {

a = c;

}
printf("The approximate root is: %.2f\n", c);

int main() {

float a = 2.0, b = 3.0; // Initial intervals

printf("Approximate root of x^3 - 4x - 9 = 0 up to 2 decimal places using Bisection


method:\n");

bisection(a, b);

return 0;

}
OUTPUT :
/*
Name : Saurav Kumar
University Roll Number: 2103058
Section: F ( 63 )
Q.3) WAP in C to find the real root for the equation cosx-3x+1=0 for 7 iterations using
iteration method.

*/

#include <stdio.h>

#include <math.h>

#define MAX_ITER 7

double equation(double x) {

return cos(x) - 3*x + 1;

double iterate(double x) {

return cos(x)/3 + 1.0/3;

int main() {

int iter;

double x0, x1, error;

x0 = 0.5;

printf("Initial guess: %.6f\n", x0);

printf("------------------------------------------\n");

printf("Iteration | x(i) | x(i+1) | Error \n");

printf("------------------------------------------\n");

for (iter = 0; iter < MAX_ITER; iter++) {

x1 = iterate(x0);

error = fabs((x1 - x0) / x1);

printf(" %2d | %.6f | %.6f | %.6f\n", iter+1, x0, x1, error);

x0 = x1;

}
printf("------------------------------------------\n");

printf("Approximate root after %d iterations: %.6f\n", MAX_ITER, x1);

return 0;

}
OUTPUT :
/*
Name : Saurav Kumar
University Roll Number: 2103058
Section: F ( 63 )
Q.4) WAP in C to find the root for the equation x3-3x+1=0 up to 4 decimal places using
Regula Falsi Method.

*/

#include <stdio.h>

#include <math.h>

#define EPSILON 0.0001

double f(double x) {

return x*x*x - 3*x + 1;

void regula_falsi(double a, double b) {

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

printf("Invalid input! Root does not exist between given intervals.\n");

return;

double c = a;

while (fabs(f(c)) >= EPSILON) {

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

if (fabs(f(c)) < EPSILON)

break;

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

b = c;

else

a = c;

printf("The approximate root is: %.4lf\n", c);

}
int main() {

double a = -2.0, b = 2.0;

printf("Approximate root of x^3 - 3x + 1 = 0 up to 4 decimal places using Regula Falsi


method:\n");

regula_falsi(a, b);

return 0;

}
OUTPUT :
/*
Name : Saurav Kumar
University Roll Number: 2103058
Section: F ( 63 )
Q.5) WAP in C to find the root for the equation x3-5x+1=0 up to 3 decimal points between
the interval (2,2.5) using Secant method.

*/

#include <stdio.h>

#include <math.h>

#define EPSILON 0.001 // Tolerance for convergence

#define MAX_ITER 100 // Maximum number of iterations

double f(double x) {

return x*x*x - 5*x + 1;

void secant(double x0, double x1) {

double x2, fx0, fx1, fx2;

int iteration = 0;

do {

fx0 = f(x0);

fx1 = f(x1);

x2 = x1 - (fx1 * (x1 - x0)) / (fx1 - fx0); // Secant formula

fx2 = f(x2);

x0 = x1;

x1 = x2;

iteration++;

if (fabs(fx2) < EPSILON) {

printf("Root found after %d iterations: %.3lf\n", iteration, x2);

return;

} while (iteration < MAX_ITER);


printf("Root not found within the maximum number of iterations.\n");

int main() {

double x0 = 2.0, x1 = 2.5; // Initial guesses

printf("Approximate root of x^3 - 5x + 1 = 0 up to 3 decimal points using Secant


method:\n");

secant(x0, x1);

return 0;

}
OUTPUT :
/*
Name : Saurav Kumar
University Roll Number: 2103058
Section: F ( 63 )
Q.6) WAP in C to find the root for the equation x- 𝑒 −𝑥 up to 3 decimal points for the interval
(1,2) using Newton Raphson method.

*/

#include <stdio.h>

#include <math.h>

#define EPSILON 0.001

#define MAX_ITER 100

double func(double x) {

return x - exp(-x);

double derivative(double x) {

return 1 + exp(-x);

void newtonRaphson(double x0) {

double x1, fx, fprime;

int iteration = 0;

do {

fx = func(x0);

fprime = derivative(x0);

x1 = x0 - fx / fprime;

if (fabs(x1 - x0) < EPSILON) {

printf("Root found after %d iterations: %.3lf\n", iteration, x1);

return;

x0 = x1;

iteration++;
} while (iteration < MAX_ITER);

printf("Root not found within the maximum number of iterations.\n");

int main() {

double x0 = 1.5;

printf("Approximate root of x - e^(-x) up to 3 decimal points using Newton-Raphson


method:\n");

newtonRaphson(x0);

return 0;

}
OUTPUT :
/*
Name : Saurav Kumar
University Roll Number: 2103058
Section: F ( 63 )
Q.7) WAP in C to implement Newton Forward Interpolation Method by displaying the
difference table for the given set of intervals.

x: 1921, 1931, 1941, 1951, 1961, 1971, 1981


y: 35, 42, 58, 84, 120, 165, 220

*/

#include <stdio.h>

#include <math.h>

int fact(int n) {

if (n <= 1)

return 1;

return n * fact(n - 1);

void forwardDifferenceTable(int n, double x[], double y[][n]) {

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

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

y[j][i] = y[j + 1][i - 1] - y[j][i - 1];

void printDifferenceTable(int n, double y[][n]) {

printf("Forward Difference Table:\n");

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

printf("%.4f\t", y[i][0]);

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

printf("%.4f\t", y[i][j]);
}

printf("\n");

double newtonForwardInterpolation(double x0, double h, int n, double x[],


double y[][n]) {

double result = y[0][0];

double u = (x0 - x[0]) / h;

double temp = 1;

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

temp *= (u - i + 1) / i;

result += temp * y[0][i];

return result;

int main() {

double x[] = {1921, 1931, 1941, 1951, 1961, 1971, 1981};

double y[][7] = {{35, 42, 58, 84, 120, 165, 220}};

int n = sizeof(x) / sizeof(x[0]);

forwardDifferenceTable(n, x, y);

printDifferenceTable(n, y);

double x0 = 1935;

double h = x[1] - x[0];

double interpolation_result = newtonForwardInterpolation(x0, h, n, x, y);

printf("\nInterpolated value at x=%.2f is %.4f\n", x0, interpolation_result);

return 0;

}
OUTPUT :
/*
Name : Saurav Kumar
University Roll Number: 2103058
Section: F ( 63 )
Q.8) WAP in C for Newton Backward Interpolation Method for the given table .

x: 1921, 1931, 1941, 1951, 1961, 1971, 1981


y: 35, 42, 58, 84, 120, 165, 220

*/

#include <stdio.h>

#include <math.h>

void backwardDifferenceTable(int n, double x[], double y[][n]) {

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

for (int j = n - 1; j >= i; j--) {

y[j][i] = y[j][i - 1] - y[j - 1][i - 1];

void printDifferenceTable(int n, double y[][n]) {

printf("Backward Difference Table:\n");

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

printf("%.4f\t", y[i][0]);

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

printf("%.4f\t", y[i][j]);

printf("\n");

double newtonBackwardInterpolation(double x0, double h, int n, double x[], double y[][n]) {

double result = y[n - 1][0];

double u = (x0 - x[n - 1]) / h;


double temp = 1;

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

temp *= (u + i - 1) / i;

result += temp * y[n - 1][i];

return result;

int main() {

double x[] = {1921, 1931, 1941, 1951, 1961, 1971, 1981};

double y[][7] = {{35, 42, 58, 84, 120, 165, 220}};

int n = sizeof(x) / sizeof(x[0]);

backwardDifferenceTable(n, x, y);

printDifferenceTable(n, y);

double x0 = 1935;

double h = x[1] - x[0];

double interpolation_result = newtonBackwardInterpolation(x0, h, n, x, y);

printf("\nInterpolated value at x=%.2f is %.4f\n", x0, interpolation_result);

return 0;

\
OUTPUT :
/*
Name : Saurav Kumar
University Roll Number: 2103058
Section: F ( 63 )
Q.9) WAP in C for Lagrange Interpolation Method by displaying the difference table for
the given set of intervals.

x: 1921, 1931, 1941, 1951, 1961, 1971, 1981


y: 35, 42, 58, 84, 120, 165, 220

*/

#include <stdio.h>

#include <math.h>

double lagrangeInterpolation(int n, double x[], double y[], double x0) {

double result = 0.0;

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

double term = y[i];

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

if (j != i) {

term = term * (x0 - x[j]) / (x[i] - x[j]);

result += term;

return result;

void printDifferenceTable(int n, double x[], double y[]) {

printf("Difference Table:\n");

printf("x\t\tf(x)\n");

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

printf("%.2f\t\t%.2f\n", x[i], y[i]);

}
int main() {

double x[] = {1921, 1931, 1941, 1951, 1961, 1971, 1981};

double y[] = {35, 42, 58, 84, 120, 165, 220};

int n = sizeof(x) / sizeof(x[0]);

printf("Given set of intervals:\n");

printDifferenceTable(n, x, y);

double x0 = 1935; // Value for interpolation

double interpolation_result = lagrangeInterpolation(n, x, y, x0);

printf("\nInterpolated value at x=%.2f is %.4f\n", x0, interpolation_result);

return 0;

}
OUTPUT :
/*
Name : Saurav Kumar
University Roll Number: 2103058
Section: F ( 63 )
6 1
Q.10) WAP in C for Trapezoidal Rule for the equation ∫0 1+𝑥 2 for 6 intervals.

*/

#include <stdio.h>

#include <math.h>

double func(double x) {

return 1 / (1 + x * x);

double trapezoidal_rule(double a, double b, int n) {

double h = (b - a) / n;

double sum = func(a) + func(b);

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

double x = a + i * h;

sum += 2 * func(x);

return (h / 2) * sum;

int main() {

double a = 0.0;

double b = 6.0;

int n = 6;

double result = trapezoidal_rule(a, b, n);

printf("Approximate value of the integral: %.6f\n", result);

return 0;

}
OUTPUT :
/*
Name : Saurav Kumar
University Roll Number: 2103058
Section: F ( 63 )
6 1
Q.11) WAP in C for Simpson 1/3 Rule for the equation ∫0 1+𝑥2 for 6 intervals.

*/

#include <stdio.h>

#include <math.h>

double func(double x) {

return 1 / (1 + x * x);

double simpson_one_third(double a, double b, int n) {

double h = (b - a) / n;

double sum = func(a) + func(b);

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

double x = a + i * h;

sum += (i % 2 == 0) ? 2 * func(x) : 4 * func(x);

return (h / 3) * sum;

int main() {

double a = 0.0;

double b = 6.0;

int n = 6;

double result = simpson_one_third(a, b, n);

printf("Approximate value of the integral: %.6f\n", result);

return 0;

}
OUTPUT :
/*
Name : Saurav Kumar
University Roll Number: 2103058
Section: F ( 63 )
6 1
Q.12) WAP in C for Simpson 3/8 Rule for the equation ∫0 1+𝑥2 for 6 intervals.

*/

#include <stdio.h>

#include <math.h>

double func(double x) {

return 1 / (1 + x * x);

double simpson_three_eighth(double a, double b, int n) {

double h = (b - a) / n;

double sum = func(a) + func(b);

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

double x = a + i * h;

sum += (i % 3 == 0) ? 2 * func(x) : 3 * func(x);

return (3 * h / 8) * sum;

int main() {

double a = 0.0;

double b = 6.0;

int n = 6;

double result = simpson_three_eighth(a, b, n);

printf("Approximate value of the integral: %.6f\n", result);

return 0;

}
OUTPUT :
/*
Name : Saurav Kumar
University Roll Number: 2103058
Section: F ( 63 )
𝑑𝑦
Q.13) WAP in C for Euler’s Method for the equation 𝑑𝑥 = 𝑦 − 2𝑥 start at (x, y) = (0, 1)
with subinterval (h) = 0.4 for range 0 ≤ 𝑥 ≤ 2.

*/

#include <stdio.h>

#include <math.h>

double f(double x, double y) {

return y - 2 * x;

int main() {

double x0 = 0.0, y0 = 1.0;

double h = 0.4;

double x_end = 2.0;

double x = x0, y = y0;

printf("Euler's Method for dy/dx = y - 2x\n");

printf("Starting at (x, y) = (%.1f, %.1f) with h = %.1f\n", x0, y0, h);

printf("x\t\ty\n");

printf("%.1f\t\t%.6f\n", x, y);

while (x < x_end) {

y = y + h * f(x, y);

x = x + h;

printf("%.1f\t\t%.6f\n", x, y);

return 0;

}
OUTPUT :

You might also like