Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function [x, fx] = goldenRatio(fun, a, b, tol)
- c = (3 - sqrt(5)) / 2;
- x1 = a + c * (b - a); f1 = feval(fun, x1);
- x2 = a + b - x1; f2 = feval(fun, x2);
- while abs(b - a) > tol
- if f1 <= f2
- b = x2;
- x2 = x1;
- f2 = f1;
- x1 = a + c * (b - a);
- f1 = feval(fun, x1);
- else
- a = x1;
- x1 = x2;
- f1 = f2;
- x2 = b - c * (b - a);
- f2 = feval(fun, x2);
- end
- if f1 < f2
- x = x1; fx = f1;
- else
- x = x2; fx = f2;
- end
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement