i am trying to find such p,that for given function f(p),we have equality
p=f(p);
here is code
#include <iostream>
#include<math.h>
using namespace std;
float fixed(float x){
return (float)(pow(x,3)-4*pow(x,2)-10);
}
int main(){
float p=0.0;
float p0=1.5;
float tol=(float).001;
int N=25;
int i=1;
while(i<N){
p=(float)fixed(p0);
if((p-p0)<tol){
cout<<p<<endl;
break;
}
i=i+1;
p0=p;
if(i>N){
cout<<"solution not found ";
break;
}
}
return 0;
}
i have tried different initial point,also different tolerance,but result is very nonsense -16 or -15.something,so what is wrong?is code correct?please help
I think you simply don’t have a situation in which the iterative algorithm is applicable. See here for some conditions. Your function doesn’t have an attractive fixed point near 1.5, and the algorithm diverges.
But why the numerics: Your function is
f(x) = x^3 - 4x - 10, so solvingf(x) = xamounts to finding the zeros off(x) - x, and there is only one real zero near 5.35. Howevever,f'(x)at that point is very large, so even there the iterative algorithm isn’t usable.A numeric root-finding algorithm may be a more appropriate approach.