#include<stdio.h>
#include<conio.h>
#include<math.h>
double valOfFuncAt(double );
double derivativeAt(double );
double a,b,c,d;
int main(){
double xo;
double x1;
double fx,f_x;
printf("Enter The Coefficients Of The Cubic Equation");
scanf("%f%f%f%f",&a,&b,&c,&d);
printf("Enter The First Approximate Root");
scanf("%f",&xo);
fx=valOfFuncAt(xo);
f_x=derivativeAt(xo);
x1= xo-(fx/f_x);
while(valOfFuncAt(x1) >= 0.0001){
fx=valOfFuncAt(x1);
f_x=derivativeAt(x1);
x1= x1 - (fx/f_x);
}
printf("\nApproximate Root Of The Equation Is : %f",x1);
getch();
return 0;
}
double valOfFuncAt(double x){
double fx1;
fx1 = (a*x*x*x) + (b*x*x) + (c*x) +d;
return fx1;
}
double derivativeAt(double x){
double f_x1;
f_x1 = (3*a*x*x) + (2*b*x) + c;
return f_x1;
}
When I am running it for any cubic equation in code blocks, for instance x^3-3x+1=0
, and giving 0.3 be the first approximate root, it is producing the wrong output and I am not getting what I did wrong in the logic!