Advertisement
UF6

Muller secant

UF6
Oct 6th, 2016
292
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MatLab 1.01 KB | None | 0 0
  1. function p = muller(p0, p1, p2, TOL, N)
  2.     h1 = p1-p0;
  3.     h2 = p2-p1;
  4.     g1 = (f(p1)-f(p0))/h1;
  5.     g2 = (f(p2)-f(p1))/h2;
  6.     d = (g2-g1)/(h2+h1);
  7.  
  8.     i = 3;
  9. while i <= N
  10.     b =g2+h2*d;
  11.     D=(b^(2) -4*f(p2)*d)^(1/2);
  12.     if abs(b-D)<abs(b+D)
  13.          E=b+D;
  14.     else
  15.         E=b-D;
  16.     end
  17.             h=-2*f(p2)/E;
  18.             p=p2+h;
  19.      if abs(h)<TOL
  20.          disp(p)
  21.          return
  22.      end
  23.  
  24. p0=p1;
  25. p1=p2;
  26. p2=p;
  27. h1=p1-p0;
  28. h2=p2-p1;
  29. g1=((f(p1))-(f(p0)))/h1;
  30. g2=((f(p2))-(f(p1)))/h2;
  31. d=(g2-g1)/(h2+h1);
  32. i=i+1;
  33.  
  34. end
  35. disp('Fail')
  36.  
  37.  
  38.  
  39.  
  40. function p = secant(p0,p1, TOL, N)
  41. i=2;
  42. q0=f(p0);
  43. q1=f(p1);
  44. while i<=N
  45.     p = p1-(q1*(p1-p0)/(q1-q0));
  46.     if abs(p-p1)<TOL
  47.         disp(['The error is less than the given tolerance after ' num2str(i) ' iterations'])
  48.         return
  49.     end
  50.     disp(p)
  51.     i = i+1;
  52.     p0=p1;
  53.     p1=p;
  54.     q0=q1;
  55.     q1=f(p);
  56. end
  57. disp(['The method failed after ' num2str(N) ' iterations'])
  58. end
  59. function y = f(x)
  60.     y = 2.7*(1+x)^(-361)-1;
  61. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement