My program uses a while loop to eventually get to an error of zero and a root of sqrt(3). I'm not understand why after the third iteration the program fails to compute a new x value. I'm using Visual Studio 2013. Your help is greatly appreciated!The code tag instructions were dubious, so forgive me on this first post.
Code:
#include <stdio.h>
#include <math.h>
main()
{
/*This program uses the Newton-Raphson method to solve y = (x^3)-3 for it's roots.*/
printf("This program uses the Newton-Raphson method to solve y = (x^3)-3 for it's roots. Enter your estimate of the root.\n");
float x,y,z;
int num;
num = 0;
/*User inputs an initial estimate of root (around 1.6)*/
scanf_s("%f", &x);
/*Program prints initial estimate*/
printf("initial x = %f\n", x);
/*If error is greater than .000001, then program may continue*/
y = abs(((x - sqrt(3)) / x)*100);
z = 3 * pow(x, 2);
/*If derivative is not equal to zero and the number of iterations has not exceed 500, then the program may continue*/
while (y >= .000001, z != 0, num <= 499)
{
num++;
x = x - ((pow(x, 3)-3) / (3 * pow(x, 2)));
printf("number of iterations:%d\n", num);
printf("estimate of root: x = %f\n", x);
y = abs(((x - sqrt(3)) / x)*100);
printf("value of error = %f\n", y);
system("pause");
z = 3 * pow(x, 2);
}
/*If the initial estimate produces an error less than or equal to .000001, then the program states the initial guess and stops the program from running*/
printf("estimate of root: x = %f/n", x);
system("pause");
return 0;}