Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- %Define variables used in function
- a=0;
- b=1;
- tol=0.00001;
- p=0;
- i=0;
- %Define function
- f=@(x)(exp(x)-x^2+3*x-2);
- %Set to loop while p is above the desired tolerance
- while abs(0.257530-p)>=tol
- p=(a+b)/2;
- if f(p)>0 && f(a)>0 || f(p)<0 && f(a)<0
- a=p;
- else
- b=p;
- end
- i=i+1;
- end
- disp(p);
- disp(i);
Add Comment
Please, Sign In to add comment