#include <stdio.h>
#include <math.h>
int main()
{
// ax^3 + bx^2 + cx + d = 0
float a,b,c,d,guess,derivative;
double functional_value, accuracy;
printf("Please enter values for a,b,c,d respectively :\n");
scanf("%f %f %f %f", &a, &b, &c, &d);
printf("Please enter your best guess for a root:\n");
scanf("%f", &guess);
printf("How close you want the value to be ?\n");
scanf("%f", &accuracy);
while (fabs(functional_value) <= accuracy && derivative != 0)
{
functional_value = a*pow(guess,3) + b*pow(guess,2) + c*(guess) + d ;
// 3ax^2 + 2bx + c
derivative = 3*a* pow(guess,2) + 2*b*(guess) + c ;
guess -= ((functional_value)/(derivative)) ;
}
printf("A root of the equation is approximately equal to %f", guess);
return 0;
}
I am new to programming, I was just learning C in college. And I wanted to make a program that would calculate roots using newton's method for up to cubic polynomials. So I wrote the above code.
But when I run it, the loop seems to run infinitely. I even tried giving the inputs as 0 1 -2 1 (a quadratic equation whose root is 1) but the loop is still running.
I don't notice any mistakes. Can anyone please help me in finding my mistake ? like what's wrong ?
At first I didn't have any parameter for the accuracy i.e. how close one would want the root to be, so I added that as a double.
I have looked at somewhat similar questions but I don't know all the things in C they are using.
Edit#1 : Thanks for the comments and pointing out my foolish syntax mistakes
Edit#2 : Now the program runs but it doesn't calculate a root still. I get the following output
Please enter values for a,b,c,d respectively :
0 1 -2 1
Please enter your best guess for a root:
0.5
How close you want the value to be ?
0.01
A root of the equation is approximately equal to nan
Process finished with exit code 0
Edit#3 : Thanks to Eric Postpichilli now the program does calculate a root after using fabs() and comparing the functional_value to be less than or equal to accuracy. But I still can't understand why will a division by zero occur, I have used an "AND" operator in the loop condition to try and prevent that