Advertisement
Guest User

Untitled

a guest
Jun 20th, 2019
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.89 KB | None | 0 0
  1. %%%Author: Jacob Joshua Shila
  2. %%%Golden Search Algorithm
  3. clear all
  4. clc
  5. syms x
  6. %%Input
  7. fx = 0.4/sqrt(1+x^2)-sqrt(1+x^2)*(1-.4/(1+x^2))+x;
  8. maxit = 50;
  9. es = 10^-5;
  10. R = (5^.5-1)/2;
  11. %%Determine the Interval for the Initial Guess
  12. x=[-10:10];
  13. f = subs(fx,x);
  14. xlow = 0.5;
  15. xhigh = 1.5;
  16. %%Perform Golden Search
  17. xl = xlow;
  18. xu = xhigh;
  19. iter = 1;
  20. d = R*(xu-xl);
  21. x1 = xl+d;
  22. x2 = xu-d;
  23. f1 = subs(fx,x1);
  24. f2 = subs(fx,x2);
  25. if f1>f2
  26. xopt = x1;
  27. else
  28. xopt = x2;
  29. end
  30. while(1)
  31. d = R*d;
  32. if f1>f2
  33. xl = x2;
  34. x2 = x1;
  35. x1 = xl+d;
  36. f2 = f1;
  37. f1 = subs(fx,x1);
  38. else
  39. xu = x1;
  40. x1 = x2;
  41. x2 = xu-d;
  42. f1 = f2;
  43. f2 = subs(fx,x2);
  44. end
  45. iter = iter+1;
  46. if f1>f2
  47. xopt = x1;
  48. else
  49. xopt = x2;
  50. end
  51. if xopt~=0
  52. ea = (1-R)*abs((xu-xl)/xopt)*100;
  53. end
  54. if ea<=es||iter>=maxit,break
  55. end
  56. end
  57. Gold = xopt
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement