Advertisement
Voldemord

[Matlab] ZlotyPodzial

Apr 3rd, 2019
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MatLab 0.53 KB | None | 0 0
  1. function [ out ] = goldSplit(f,a,b,E) % Execute using command goldSplit('(x-100)^2' ,10, 150 , 0.001)/ Output 100.0001
  2. syms x
  3. f = eval(f);
  4.  
  5. T = 0.618;
  6. x1=(b-a)*(-1 * T) + b;
  7. x2 = (b-a) * T + a;
  8. f1 = subs(f,x1);
  9. f2 = subs(f,x2);
  10. while abs(b-a) > 2 * E
  11.     if f1 > f2
  12.         a = x1;
  13.         x1 = x2;
  14.         f1 = f2;
  15.         x2 = (b-a) * T + a;
  16.         f2 = subs(f,x2);
  17.     else
  18.         b = x2;
  19.         x2 = x1;
  20.         f2 = f1;
  21.         x1 = (b-a) * (-1*T) + b;
  22.         f1 = subs(f,x1);
  23.     end
  24. end
  25.     out =(a+b)/2;
  26. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement