Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function [ x,fx,n ] = KubniMetod( fun,fund,x1,x2,tol )
- %KUBNIMETOD Summary of this function goes here
- % Detailed explanation goes here
- n = 1;
- X = [x1 x2]';
- F = feval(fun,X);
- dF = feval(fund, X);
- f = [F(1);F(2);dF(1);dF(2)];
- Y = [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];
- abcd = Y \ f;
- b = abcd(2);
- c = abcd(3);
- d = abcd(4);
- % -b +- sq(b^2 - 4ac) / 2a;
- xa = (-2*c + sqrt((2*c)^2 - 4 * b * 3*d)) / (2 * b);
- xb = (-2*c - sqrt((2*c)^2 - 4 * b * 3*d)) / (2 * b);
- if ~(xa > x1 && xa<x2)
- x = xb;
- elseif ~(xb > x1 && xb < x2)
- x = xa;
- else
- if xa < xb
- x = xa;
- else
- x = xb;
- end
- end
- while abs([1 x x^2 x^3]*abcd - feval(fun,x))>tol
- n = n + 1;
- if feval(fund,x) < 0
- x1 = x;
- else
- x2 = x;
- end
- X = [x1 x2]';
- F = feval(fun,X);
- dF = feval(fund, X);
- f = [F(1);F(2);dF(1);dF(2)];
- Y = [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];
- abcd = Y \ f;
- b = abcd(2);
- c = abcd(3);
- d = abcd(4);
- xa = (-2*c + sqrt(c^2 - 4 * b * 3* d)) / 2 * b;
- xb = (-2*c - sqrt(c^2 - 4 * b * 3 * d)) / 2 * b;
- if ~(xa > x1 && xa<x2)
- x = xb;
- elseif ~(xb > x1 && xb < x2)
- x = xa;
- else
- if xa < xb
- x = xa;
- else
- x = xb;
- end
- end
- end
- fx = feval(fun,x);
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement