Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- double getRiddersRoot(const std::function<double(double)>& f, const double& a, const double& b, const double epsilon)
- {
- double xl=a;
- double xr=b;
- while(std::abs(xl-xr)>epsilon)
- {
- double xn=(xl+xr)/2.;
- double xnn=xn+(xn-xl)*(std::abs(f(xl))/f(xl))*f(xn)/(sqrt(f(xn)*f(xn) -f(xl)*f(xr)));
- double xnnn=0.; //why did I have to initialize the xnnn here, instead of being able to separately declare it inside the ifs?
- if(f(xn)*f(xnn)<0)
- {
- xnnn=xn;
- }
- else if(f(xl)*f(xnn)<0)
- {
- xnnn=xl;
- }
- else
- {
- xnnn=xr;
- }
- if(xnn>xnnn)
- {
- xr=xnn;
- xl=xnnn;
- }
- else
- {
- xr=xnnn;xl=xnn;
- }
- }
- return xr;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement