Advertisement
Guest User

Untitled

a guest
Nov 17th, 2019
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.62 KB | None | 0 0
  1. clc; clear all;
  2. xdel(winsid());
  3. x=-2:0.01:1.5;
  4. f1=x.^2-2.*x+1;
  5. fh=scf(1);
  6. plot2d(x,f1);
  7.  
  8. function [z]=f(x)
  9. z=x.^2-2.*x+1;
  10. endfunction
  11.  
  12. a=2;
  13. b=1.5;
  14. e=0.001;
  15.  
  16. L=b-a;
  17. q=(3-sqrt(5))/2;
  18. c=abs(b-a);
  19.  
  20. while c>e
  21.  
  22. fa=f(a);
  23. fb=f(b);
  24. if fa>fb then
  25. a=a+(1-q).*(b-a)
  26. fa=f(a);
  27. elseif fa<fb then
  28. b=a+q.*(b-a)
  29. fb=f(b);
  30. elseif fa==fb then
  31. a=a+(1-q).*(b-a)
  32. fa=f(a)
  33. b=a+q.*(b-a)
  34. fb=f(b)
  35. end
  36. c=abs(b-a);
  37. end
  38.  
  39. if c<e then
  40. printf('Wynik jest dobry %f<%f',c,e);
  41. else
  42. printf('Wynik jest zły');
  43. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement