Advertisement
Guest User

Untitled

a guest
Apr 22nd, 2019
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.71 KB | None | 0 0
  1. double getRiddersRoot(const std::function<double(double)>& f, const double& a, const double& b, const double epsilon)
  2. {
  3. double xl=a;
  4. double xr=b;
  5. while(std::abs(xl-xr)>epsilon)
  6. {
  7. double xn=(xl+xr)/2.;
  8. double xnn=xn+(xn-xl)*(std::abs(f(xl))/f(xl))*f(xn)/(sqrt(f(xn)*f(xn) -f(xl)*f(xr)));
  9. double xnnn=0.; //why did I have to initialize the xnnn here, instead of being able to separately declare it inside the ifs?
  10. if(f(xn)*f(xnn)<0)
  11. {
  12. xnnn=xn;
  13. }
  14. else if(f(xl)*f(xnn)<0)
  15. {
  16. xnnn=xl;
  17. }
  18. else
  19. {
  20. xnnn=xr;
  21. }
  22.  
  23. if(xnn>xnnn)
  24. {
  25. xr=xnn;
  26. xl=xnnn;
  27. }
  28. else
  29. {
  30. xr=xnnn;xl=xnn;
  31. }
  32.  
  33. }
  34.  
  35. return xr;
  36. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement