Hello,
I'm working on a program that need to fnd multiple roots to an equation using the direct search method. I have the program that finds the first one, but I have no clue on how to modify it to find all of them in the range.
Code:
#include <stdio.h>
#include <math.h>
#define PI 3.14159265
double f(double x);
double direct(double a, double b, int n);
int main()
{
double x0 = 0.0, xn = 10;
int n = 1000;
printf( "The root using the Direct Search Method is %f\n", direct(x0, xn, n) );
return 0;
}
double f(double x)
{
double y;
y = pow(x,3) - 12.8 * pow(x,2) + 49.55 * x - 59.50;
return y;
}
double direct(double a, double b, int n)
{
int i;
double dx, x = 0.0, f1, f2;
dx = (b - a) / n;
f1 = f(a);
for (i = 1;i <= n; i++)
{
x = a + dx * i; f2 = f(x);
if (f1 * f2 <= 0)
break; f1 = f2;
}
return x - dx/2;
}