Uploaded by Mahito

Untitled document

advertisement
#include <stdio.h>
#include <math.h>
#define EPSILON 0.0001
double function(double x) {
return cos(x) - x * exp(x);
}
double bisection(double a, double b) {
double c;
while ((b - a) >= EPSILON) {
c = (a + b) / 2;
if (fabs(function(c)) < EPSILON)
return c;
if (function(c) * function(a) < 0)
b = c;
else
a = c;
}
return (a + b) / 2;
}
int main() {
double a, b;
a = 0.0;
b = 1.0;
double root = bisection(a, b);
printf("Root of cos(x) - x * exp(x) = 0: %.4lf\n", root);
return 0;
}
#include <stdio.h>
#include <math.h>
#define EPSILON 0.0001
double function(double x) {
return cos(x) - x * exp(x);
}
double false_position(double a, double b) {
double c;
while (fabs(b - a) >= EPSILON) {
c = (a * function(b) - b * function(a)) / (function(b) - function(a));
if (fabs(function(c)) < EPSILON)
return c;
if (function(c) * function(a) < 0)
b = c;
else
a = c;
}
return (a + b) / 2;
}
int main() {
double a, b;
a = 2.0;
b = 1.0;
double root = false_position(a, b);
printf("Root of cos(x) - x * exp(x) = 0: %.4lf\n", root);
return 0;
}
#include <stdio.h>
#include <math.h>
#define EPSILON 0.0001
#define MAX_ITERATIONS 1000
double function(double x) {
return cos(x) - x * exp(x);
}
double derivative(double x) {
return -sin(x) - exp(x) - x * exp(x);
}
double newton_raphson(double initial_guess) {
double x = initial_guess;
for (int i = 0; i < MAX_ITERATIONS; ++i) {
double f = function(x);
double f_prime = derivative(x);
// Check if the derivative is close to zero to avoid division by a small number
if (fabs(f_prime) < EPSILON) {
printf("Derivative is close to zero. Cannot continue.\n");
return NAN; // Return NaN (Not a Number) to indicate failure
}
// Update x using the Newton-Raphson formula
x = x - (f / f_prime);
// Check if the root is found
if (fabs(f) < EPSILON)
return x;
}
// Return NaN if the maximum number of iterations is reached
printf("Maximum iterations reached. No convergence.\n");
return NAN;
}
int main() {
// Initial guess
double initial_guess = 0.0;
// Find the root using the Newton-Raphson method
double root = newton_raphson(initial_guess);
// Print the result up to 4 decimal points
if (!isnan(root))
printf("Root of cos(x) - x * exp(x) = 0: %.4lf\n", root);
return 0;
}
#include <stdio.h>
#include <math.h>
#define EPSILON 0.0001
#define MAX_ITERATIONS 1000
double function(double x) {
return cos(x) - x * exp(x);
}
double secant_method(double x0, double x1) {
double x2;
for (int i = 0; i < MAX_ITERATIONS; ++i) {
double f0 = function(x0);
double f1 = function(x1);
// Check if the difference is close to zero to avoid division by a small number
if (fabs(f1 - f0) < EPSILON) {
printf("Difference is close to zero. Cannot continue.\n");
return NAN; // Return NaN (Not a Number) to indicate failure
}
// Calculate the next approximation using the secant method
x2 = x1 - (f1 * (x1 - x0) / (f1 - f0));
// Check if the root is found
if (fabs(function(x2)) < EPSILON)
return x2;
// Update x0 and x1 for the next iteration
x0 = x1;
x1 = x2;
}
// Return NaN if the maximum number of iterations is reached
printf("Maximum iterations reached. No convergence.\n");
return NAN;
}
int main() {
// Initial guesses
double x0 = 0.0;
double x1 = 1.0;
// Find the root using the secant method
double root = secant_method(x0, x1);
// Print the result up to 4 decimal points
if (!isnan(root))
printf("Root of cos(x) - x * exp(x) = 0: %.4lf\n", root);
return 0;
}
Download