Advertisement
Guest User

Untitled

a guest
Sep 20th, 2017
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.75 KB | None | 0 0
  1. %initiation values
  2. accuracy = abs(xr - xl);
  3. tolerance = 1e-8;
  4. xl = -1;
  5. xr = 1;
  6. funct = (6425*x.^8 - 12012*x.^6 + 6930*x.^4 - 1260*x.^2 + 35)/128;
  7. %call the bisect function
  8. x = bisect(funct, xl, xr, accuracy, tolerance);
  9.  
  10.  
  11. function root = bisect(f, xl, xr, accuracy, tolerance)
  12.  
  13.  
  14. %start with midpoint
  15. xmid = (xl + xr)/2;
  16. i = 0;
  17.  
  18. while (accuracy > tolerance)
  19. i = i + 1;
  20. %if we found the root
  21. if f(xmid) < tolerance
  22. root = (xl + xr)/2;
  23. else
  24. %check which side the root is on
  25. if f(xl)*f(xmid) < 0
  26. xr = xmid;
  27. else
  28. xl = xmid;
  29. accuracy = abs(xr - xl);
  30. end
  31. end
  32. end
  33. print root
  34. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement