C Programming Algorithms and Codes for Numerical Methods
1. Round-Off Error and Significant Digits
#include <stdio.h>
int main() {
float a = 1.0e10, b = 1.0, result;
result = (a + b) - a;
printf("Result of (a + b) - a: %.10f\n", result);
return 0;
}
2. Bisection Method
#include <stdio.h>
#include <math.h>
float f(float x) {
return x*x*x - 4*x - 9;
}
int main() {
float a = 2, b = 3, c;
int i, max_iter = 20;
if (f(a)*f(b) >= 0) {
printf("Invalid interval.\n");
return 1;
}
for (i = 0; i < max_iter; i++) {
c = (a + b)/2;
printf("Iteration %d: c = %f, f(c) = %f\n", i+1, c, f(c));
if (f(c) == 0.0) break;
else if (f(c)*f(a) < 0) b = c;
else a = c;
}
return 0;
}
3. Newton-Raphson Method
#include <stdio.h>
#include <math.h>
float f(float x) {
return x*x - 2;
}
C Programming Algorithms and Codes for Numerical Methods
float df(float x) {
return 2*x;
}
int main() {
float x = 1.0, x_new;
int i, max_iter = 10;
for (i = 0; i < max_iter; i++) {
x_new = x - f(x)/df(x);
printf("Iteration %d: x = %f\n", i+1, x_new);
if (fabs(x_new - x) < 0.0001) break;
x = x_new;
}
return 0;
}
4. Gauss Elimination
#include <stdio.h>
int main() {
int i, j, k, n = 3;
float a[3][4] = {
{2, 3, 1, 1},
{4, 1, 2, 2},
{3, 2, 3, 3}
}, x[3];
for (i = 0; i < n; i++) {
for (k = i+1; k < n; k++) {
float factor = a[k][i]/a[i][i];
for (j = 0; j <= n; j++)
a[k][j] -= factor * a[i][j];
}
}
for (i = n-1; i >= 0; i--) {
x[i] = a[i][n];
for (j = i+1; j < n; j++)
x[i] -= a[i][j]*x[j];
x[i] /= a[i][i];
}
printf("Solution:\n");
for (i = 0; i < n; i++) printf("x%d = %f\n", i+1, x[i]);
return 0;
C Programming Algorithms and Codes for Numerical Methods
}
5. Trapezoidal Rule
#include <stdio.h>
float f(float x) {
return x*x;
}
int main() {
float a = 0, b = 2, sum = 0.0, h;
int i, n = 4;
h = (b - a)/n;
sum = f(a) + f(b);
for (i = 1; i < n; i++)
sum += 2*f(a + i*h);
sum *= h/2;
printf("Integral using Trapezoidal Rule = %f\n", sum);
return 0;
}
6. Simpson's 1/3 Rule
#include <stdio.h>
float f(float x) {
return 1 / (1 + x*x);
}
int main() {
float a = 0, b = 1, h, sum = 0.0;
int i, n = 6;
if (n % 2 != 0) {
printf("n must be even\n");
return 1;
}
h = (b - a)/n;
sum = f(a) + f(b);
for (i = 1; i < n; i++) {
float x = a + i*h;
sum += (i % 2 == 0) ? 2*f(x) : 4*f(x);
C Programming Algorithms and Codes for Numerical Methods
}
sum *= h/3;
printf("Integral using Simpson's Rule = %f\n", sum);
return 0;
}
7. Finite Difference Method
#include <stdio.h>
#include <math.h>
int main() {
int i, n = 6;
float T[6], T_new[6], tol = 0.001;
float error;
T[0] = 100.0;
T[5] = 200.0;
for (i = 1; i < 5; i++) T[i] = 0;
do {
error = 0.0;
for (i = 1; i < 5; i++) {
T_new[i] = 0.5 * (T[i-1] + T[i+1]);
if (error < fabs(T_new[i] - T[i]))
error = fabs(T_new[i] - T[i]);
}
for (i = 1; i < 5; i++) T[i] = T_new[i];
} while (error > tol);
printf("Temperature Distribution:\n");
for (i = 0; i < n; i++)
printf("T[%d] = %.2f\n", i, T[i]);
return 0;
}
8. Euler's Method
#include <stdio.h>
float f(float x, float y) {
return y - x;
}
int main() {
float x = 0.0, y = 1.0, h = 0.2;
int i, n = 10;
C Programming Algorithms and Codes for Numerical Methods
printf("x\t\ty (Euler)\n");
for (i = 0; i <= n; i++) {
printf("%.2f\t\t%.4f\n", x, y);
y = y + h * f(x, y);
x = x + h;
}
return 0;
}