Guest User

Untitled

a guest
Jan 21st, 2019
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.34 KB | None | 0 0
  1. %Define variables used in function
  2. a=0;
  3. b=1;
  4. tol=0.00001;
  5. p=0;
  6. i=0;
  7.  
  8. %Define function
  9. f=@(x)(exp(x)-x^2+3*x-2);
  10.  
  11. %Set to loop while p is above the desired tolerance
  12. while abs(0.257530-p)>=tol
  13. p=(a+b)/2;
  14. if f(p)>0 && f(a)>0 || f(p)<0 && f(a)<0
  15. a=p;
  16. else
  17. b=p;
  18. end
  19. i=i+1;
  20. end
  21. disp(p);
  22. disp(i);
Add Comment
Please, Sign In to add comment