1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
|
typedef double (*func) (double);
double myFunction(double x)
{
return 6 * xn * xn - 17 * xn -14;
}
double myFunctionsDerivative(double x)
{
return 12 * xn - 17;
}
double newt(double xn, func f, func fDash, double accu)
{
double xnplus1 = xn - ( f(xn) / fDash(xn) );
if (abs(xn - xnplus1) >= accu)
return newt(xnplus1, f, fDash, accu);
return xnplus1;
}
int main(){
double guess1=10, guess2=-10, accuracy=0.1;
double result1=newt(guess1, myFunction, myFunctionsDerivative, accuracy);
double result2=newt(guess2, myFunction, myFunctionsDerivative, accuracy);
cout<<"The roots are"<<'\t'<<result1<<","<<'\t'<<result2<<endl;
return 0;
}
|