I have a project for my programming class that I am stuck on. It requires using arrays, pointers, structures, and unions to combine the Direct Search method and the Newton-Raphson method with precision of .001 in finding a root. So far I have the Direct Search portion done, but cannot figure out how to begin the Newton-Raphson portion. Please help!
Here is my working code for the direct search portion that I have done so far.
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 = 3.0;
int n = 12;
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) - 0.2589 * pow(x,2) + 0.02262 * x - 0.001122;
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;
}