Advertisement
encoree1996

test newton

Mar 16th, 2016
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MatLab 1.13 KB | None | 0 0
  1. function [ ] = newton(func, a, b, delta, maxd )
  2. %UNTITLED Summary of this function goes here
  3. %   Detailed explanation goes here
  4. funcd1 = diff(func,1);
  5. funcd2 = diff(func,2);
  6. eps = 10^-delta;
  7. set = 0;
  8. x0 = 0;
  9. if a > b
  10.     [a,b] = deal(b,a);
  11. end
  12.  
  13. if subs(func,a)*subs(funcd2,a) > 0
  14.     x0 = a;
  15.     set = 1;
  16. else
  17.     if subs(func,b)*subs(funcd2,b) > 0
  18.         x0 = b;
  19.         set = 1;
  20.     end
  21. end
  22.  
  23. if set == 0
  24.     for i=a:eps:b
  25.         if subs(func,i)*subs(funcd2,b) > 0
  26.             x0 = i;
  27.             set = 1;
  28.         end
  29.     end
  30. end
  31.  
  32. if set == 0
  33.     sprintf('Nie mozna znalezc punktu spelniajacego warunek f(x)*f"(x)>0!');
  34.     return
  35. else
  36.     sprintf('Znaleziono x0=%f',x0)
  37. end
  38.  
  39. xk = x0;
  40. xk1 = x0 - subs(func,x0)/subs(funcd1,x0);
  41. d = 0;
  42. while abs(subs(func,xk1)) > eps && d < maxd
  43.     d = d+1;
  44.     xk = xk1;
  45.     xk1 = xk - subs(func,xk)/subs(funcd1,xk);
  46.     if abs(xk1-xk) <= eps || abs(subs(func,xk1)) <= eps
  47.         break        
  48.     end
  49. end
  50.  
  51. if d == maxd
  52.     sprintf('Nie znaleziono miejsca zerowego w zadanym przedziale po %d przejsciach',maxd)
  53. else
  54.     sprintf('x0 = %f',xk);
  55. end
  56.  
  57. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement