Advertisement
Guest User

Untitled

a guest
Nov 24th, 2014
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.87 KB | None | 0 0
  1. function [x,y,i] = metodParabola(f,x1,x2,err,maxIter)
  2. x3 = x2;
  3. x2 = (x1+x3)/2;
  4. for i = 1 : maxIter;
  5. A = [1 x1 x1^2; 1 x2 x2^2; 1 x3 x3^2];
  6. b = [feval(f,x1); feval(f,x2); feval(f,x3)];
  7. p = A\b; % koeficijenti zeljene parabole
  8. xopt = -(p(2)/(2*p(3))); % vrednost optimuma
  9. y2 = feval(f,x2);
  10. yopt = feval(f,xopt)
  11. if( yopt >= y2)
  12. if ( xopt >= x2 )
  13. x1 = x1;
  14. x2 = x2;
  15. x3 = xopt;
  16. else
  17. x1 = xopt;
  18. x2 = x2;
  19. x3 = x3;
  20. end
  21. else
  22. if ( xopt >= x2 )
  23. x1 = x2;
  24. x2 = xopt;
  25. x3 = x3;
  26. else
  27. x1 = x1;
  28. x3 = x2;
  29. x2 = xopt;
  30. end
  31. end
  32. if( abs( polyval(p,xopt) - feval(f,xopt)) <= err)
  33. x = xopt;
  34. y = feval(f,xopt);
  35. break;
  36. end
  37. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement