Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function [xout yout] = kubna(f, fd, sstart, eend, e)
- x = [];
- x(1) = sstart;
- x(2) = eend;
- while true
- % a + bx + cx^2 + dx^3 = f(x)
- % b + 2cx + 3dx^2 = f'(x)
- amat = [
- 1 x(1) x(1)^2 x(1)^3;
- 1 x(2) x(2)^2 x(2)^3;
- 0 1 2*x(1) 3*x(1)^2;
- 0 1 2*x(2) 3*x(2)^2;
- ];
- bmat = [f(x(1)); f(x(2)); fd(x(1)); fd(x(2))];
- abcd = linsolve(amat, bmat);
- a = abcd(1);
- b = abcd(2);
- c = abcd(3);
- d = abcd(4);
- % Trazimo x12 za y'(x) = 0
- x12 = [];
- x12(1) = (-2*c + sqrt(4 * c^2 - 4*b*3*d)) / (2*3*d);
- x12(2) = (-2*c - sqrt(4 * c^2 - 4*b*3*d)) / (2*3*d);
- % Trazimo Xopt
- y = @(x)(a + b*x(1) + c*x^2 + d*x^3);
- xopt = x12(2);
- if (y(x12(1)) < y(x12(2)))
- xopt = x12(1);
- end
- % xopt postavljamo kao novu granicu
- if fd(xopt) < 0
- x(1) = xopt;
- else
- x(2) = xopt;
- end
- % Ponvaljamo dok se greska ne bude manje od e
- if abs(y(xopt) - f(xopt)) < e
- break;
- end
- end
- xout = xopt;
- yout = f(xopt);
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement