Advertisement
canezzy

Untitled

Nov 11th, 2017
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.41 KB | None | 0 0
  1. function [ x,fx,n ] = KubniMetod( fun,fund,x1,x2,tol )
  2. %KUBNIMETOD Summary of this function goes here
  3. % Detailed explanation goes here
  4. n = 1;
  5. X = [x1 x2]';
  6. F = feval(fun,X);
  7. dF = feval(fund, X);
  8. f = [F(1);F(2);dF(1);dF(2)];
  9. 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];
  10. abcd = Y \ f;
  11. b = abcd(2);
  12. c = abcd(3);
  13. d = abcd(4);
  14.  
  15. % -b +- sq(b^2 - 4ac) / 2a;
  16. xa = (-2*c + sqrt((2*c)^2 - 4 * b * 3*d)) / (2 * b);
  17. xb = (-2*c - sqrt((2*c)^2 - 4 * b * 3*d)) / (2 * b);
  18. if ~(xa > x1 && xa<x2)
  19. x = xb;
  20. elseif ~(xb > x1 && xb < x2)
  21. x = xa;
  22. else
  23. if xa < xb
  24. x = xa;
  25. else
  26. x = xb;
  27. end
  28. end
  29. while abs([1 x x^2 x^3]*abcd - feval(fun,x))>tol
  30. n = n + 1;
  31. if feval(fund,x) < 0
  32. x1 = x;
  33. else
  34. x2 = x;
  35. end
  36. X = [x1 x2]';
  37. F = feval(fun,X);
  38. dF = feval(fund, X);
  39. f = [F(1);F(2);dF(1);dF(2)];
  40. 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];
  41. abcd = Y \ f;
  42. b = abcd(2);
  43. c = abcd(3);
  44. d = abcd(4);
  45. xa = (-2*c + sqrt(c^2 - 4 * b * 3* d)) / 2 * b;
  46. xb = (-2*c - sqrt(c^2 - 4 * b * 3 * d)) / 2 * b;
  47. if ~(xa > x1 && xa<x2)
  48. x = xb;
  49. elseif ~(xb > x1 && xb < x2)
  50. x = xa;
  51. else
  52. if xa < xb
  53. x = xa;
  54. else
  55. x = xb;
  56. end
  57. end
  58.  
  59. end
  60. fx = feval(fun,x);
  61. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement