Tp rule
#include <stdio.h>
float f(float x) {
return x * x; // Example function f(x) = x^2
}
float trapezoidalRule(float a, float b, int n) {
float h = (b - a) / n;
float sum = f(a) + f(b);
for (int i = 1; i < n; i++) {
sum += 2 * f(a + i * h);
}
return (h / 2) * sum;
}
int main() {
float a, b;
int n;
printf("Enter lower limit (a): ");
scanf("%f", &a);
printf("Enter upper limit (b): ");
scanf("%f", &b);
printf("Enter number of subintervals (n): ");
scanf("%d", &n);
printf("Result: %f\n", trapezoidalRule(a, b, n));
return 0;
}
simpson 1/3
#include <stdio.h>
float f(float x) {
return x * x; // Example function f(x) = x^2
}
float simpsonsOneThirdRule(float a, float b, int n) {
if (n % 2 != 0) {
printf("Number of intervals must be even.\n");
return -1;
}
float h = (b - a) / n;
float sum = f(a) + f(b);
for (int i = 1; i < n; i++) {
if (i % 2 == 0) {
sum += 2 * f(a + i * h);
} else {
sum += 4 * f(a + i * h);
}
}
return (h / 3) * sum;
}
int main() {
float a, b;
int n;
printf("Enter lower limit (a): ");
scanf("%f", &a);
printf("Enter upper limit (b): ");
scanf("%f", &b);
printf("Enter number of subintervals (n): ");
scanf("%d", &n);
float result = simpsonsOneThirdRule(a, b, n);
if (result != -1) {
printf("Result: %f\n", result);
}
return 0;
}
weddls Ruls
#include <stdio.h>
float f(float x) {
return x * x; // Example function f(x) = x^2
}
float weddlesRule(float a, float b) {
float h = (b - a) / 6;
return (3 * h / 10) * (f(a) + 5 * f(a + h) + f(a + 2 * h) + 6 * f(a + 3 * h) + f(a + 4 * h) + 5 * f(a + 5 * h) + f(b));
}
int main() {
float a, b;
printf("Enter lower limit (a): ");
scanf("%f", &a);
printf("Enter upper limit (b): ");
scanf("%f", &b);
printf("Result: %f\n", weddlesRule(a, b));
return 0;
}
booles rule
#include <stdio.h>
float f(float x) {
return x * x; // Example function f(x) = x^2
}
float boolesRule(float a, float b) {
float h = (b - a) / 4;
return (2 * h / 45) * (7 * f(a) + 32 * f(a + h) + 12 * f(a + 2 * h) + 32 * f(a + 3 * h) + 7 * f(b));
}
int main() {
float a, b;
printf("Enter lower limit (a): ");
scanf("%f", &a);
printf("Enter upper limit (b): ");
scanf("%f", &b);
printf("Result: %f\n", boolesRule(a, b));
return 0;
}
bisectoin method
#include <stdio.h>
#include <math.h>
float f(float x) {
return x * x - 4; // Example function f(x) = x^2 - 4
}
void bisectionMethod(float a, float b, float tol) {
if (f(a) * f(b) >= 0) {
printf("Invalid interval. f(a) and f(b) must have opposite signs.\n");
return;
}
float c;
while ((b - a) >= tol) {
c = (a + b) / 2;
if (f(c) == 0.0) {
break;
} else if (f(c) * f(a) < 0) {
b = c;
} else {
a = c;
}
}
printf("Root: %f\n", c);
}
int main() {
float a, b, tol;
printf("Enter interval [a, b]: ");
scanf("%f %f", &a, &b);
printf("Enter tolerance: ");
scanf("%f", &tol);
bisectionMethod(a, b, tol);
return 0;
}
newton rep
#include <stdio.h>
#include <math.h>
float f(float x) {
return x * x - 4; // Example function f(x) = x^2 - 4
}
float f_derivative(float x) {
return 2 * x; // Derivative of f(x)
}
void newtonRaphsonMethod(float x0, float tol) {
float x1;
while (1) {
x1 = x0 - f(x0) / f_derivative(x0);
if (fabs(x1 - x0) < tol) {
printf("Root: %f\n", x1);
return;
}
x0 = x1;
}
}
int main() {
float x0, tol;
printf("Enter initial guess: ");
scanf("%f", &x0);
printf("Enter tolerance: ");
scanf("%f", &tol);
newtonRaphsonMethod(x0, tol);
return 0;
}
g elements method
#include <stdio.h>
void gaussElimination(float a[10][10], int n) {
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
float ratio = a[j][i] / a[i][i];
for (int k = 0; k <= n; k++) {
a[j][k] -= ratio * a[i][k];
}
}
}
float x[10];
for (int i = n - 1; i >= 0; i--) {
x[i] = a[i][n];
for (int j = i + 1; j < n; j++) {
x[i] -= a[i][j] * x[j];
}
x[i] /= a[i][i];
}
printf("Solution:\n");
for (int i = 0; i < n; i++) {
printf("x[%d] = %f\n", i, x[i]);
}
}
int main() {
int n;
float a[10][10];
printf("Enter the number of equations: ");
scanf("%d", &n);
printf("Enter the augmented matrix:\n");
for (int i = 0; i < n; i++) {
for (int j = 0; j <= n; j++) {
scanf("%f", &a[i][j]);
}
}
gaussElimination(a, n);
return 0;
}
regular falsi meetod
#include <stdio.h>
#include <math.h>
float f(float x) {
return x * x - 4; // Example function f(x) = x^2 - 4
}
void regulaFalsiMethod(float a, float b, float tol) {
if (f(a) * f(b) >= 0) {
printf("Invalid interval. f(a) and f(b) must have opposite signs.\n");
return;
}
float c;
while (fabs(b - a) >= tol) {
c = (a * f(b) - b * f(a)) / (f(b) - f(a));
if (f(c) == 0.0) {
break;
} else if (f(c) * f(a) < 0) {
b = c;
} else {
a = c;
}
}
printf("Root: %f\n", c);
}
int main() {
float a, b, tol;
printf("Enter interval [a, b]: ");
scanf("%f %f", &a, &b);
printf("Enter tolerance: ");
scanf("%f", &tol);
regulaFalsiMethod(a, b, tol);
return 0;
}