Code
Code
#include <stdio.h>
#include <math.h>
double f(double x) {
return x * x * x * x - x - 10;
}
double df(double x) {
return 4 * x * x * x - 1;
}
void newtonRaphson(double x0, double tol) {
double x1;
int itr = 0;
if (tol <= 0) {
printf("Tolerance must be a positive number.\n");
return 0;
}
newtonRaphson(x0, tol);
return 0;
}
#include <stdio.h>
#include <math.h>
while (fabs(f(x0)) >= tol && fabs(f(x1)) >= tol && itr < 100) {
x2 = x1 - (f(x1) * (x1 - x0)) / (f(x1) - f(x0));
printf("Iter %d: x0 = %.6f, x1 = %.6f, x2 = %.6f, f(x2) = %.6f\n", ++itr, x0, x1, x2, f(x2));
x0 = x1;
x1 = x2;
}
int main() {
double x0, x1, tol;
printf("x0: ");
scanf("%lf", &x0);
printf("x1: ");
scanf("%lf", &x1);
printf("Tolerance: ");
scanf("%lf", &tol);
if (tol <= 0) {
printf("Tolerance must be positive.\n");
return 1;
}
secant(x0, x1, tol);
return 0;
}
#include <stdio.h>
#include <math.h>
double f(double x) {
return x * x * x - 2 * x - 5;
}
void bisection(double a, double b, double tol) {
if (f(a) * f(b) >= 0) {
printf("Bisection failed because both values should be of opposite signs.\n");
return;
}
double c;
int itr = 0;
do {
c = (a + b) / 2;
if (f(c) == 0) {
break;
} else if (f(a) * f(c) < 0) {
b = c;
} else {
a = c;
}
itr++;
printf("Iterations %d: a = %f, b = %f, c = %f, f(c) = %f\n", itr, a, b, c, f(c));
} while (fabs(b - a) >= tol);
printf("Root: %f\n", c);
printf("f(c): %f\n", f(c));
printf("Total iterations: %d\n", itr);
}
int main() {
double a, b, tol;
printf("Enter the value of lower bound (a): ");
scanf("%lf", &a);
printf("Enter the value of upper bound (b): ");
scanf("%lf", &b);
printf("Enter the value of tolerance: ");
scanf("%lf", &tol);
if (tol <= 0) {
printf("Tolerance must be a positive number.\n");
return 1;
}
bisection(a, b, tol);
return 0;
}
#include <stdio.h>
#include <math.h>
int main() {
double x_true, x_approx, absolute_error, relative_error, percentage_error;
x_true = 1.855585;
x_approx = 1.855586;
if (x_true != 0) {
relative_error = absolute_error / fabs(x_true);
printf("Relative Error: %.10f\n", relative_error);
return 0;
}