/* This program will take an initial guess for the root of the equation (x^3)-3=0 and will use Newton's Method to approximate an approximate root after 500 iterations, or once the margin of error is within 0.000001. The result will the be printed to the standard output. */
Code:
#include <stdio.h>
#include <math.h>
#define E 0.000001
double Fx(double x1)
{
return pow(x1,3) - 3;
}
double df (double x2)
{
return 3*pow(x2,2);
}
int main() {
double x, F, Fd;
int count = 0;
printf("Input Initial approximation, x: ");
scanf("%f",&x); // Address specifier
if (x != 0) {
/*do
{
F = Fx(x)/df(x);
Fd = x-F;
count+=1;
if (fabs(F) < E) {
printf("The root is: %f\n", x);
printf("The number of iterations is: %d",count);
}
x = Fd;
*/
while(count < 1) {
if(df(x)>0)
F = Fx(x)/df(x);
Fd = x-F;
count+=1;
if ((count == 1) || (fabs(F)<=E)) {
printf("The root is: %f\n", x);
printf("The number of iterations is: %d",count);
}
x = Fd;
}
/*printf("The root is: %f\n", x);
printf("The number of iterations is: %d",count); //pow(x,3);
*/
}
else
printf("That is not a valid input!");
return 0;
}