Advertisement
Guest User

Untitled

a guest
Dec 11th, 2017
210
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MatLab 0.70 KB | None | 0 0
  1. function x=newtonTest(f,x)
  2.     xprev = x-1;
  3.     df=@(x,h)(f(x+h)-f(x-h))./(2*h); % df(x,h) ~ f'(x)
  4.     h = 0.0001; %bardzo mala liczba
  5.     epsilon0 = 0.001; % wartosc od ktorej dana wartosc zaczyna byc postrzegana jako 0
  6.     epsilonDif = 0.001; %roznica od ktorej dwie wartosci zaczynaja byc postrzegane jako rowne, musi byc <1
  7.     maxIterations = 100;
  8.     while abs(f(x))> epsilon0 && abs(x-xprev)>epsilonDif && maxIterations>0
  9. %         disp(maxIterations);
  10.         d = df(x,h);
  11.         if abs(d) < epsilon0 % f'(x) = 0
  12.             error('Niespelniono warunku: f''(x) != w calym przedziale');
  13.         end
  14.         xprev = x;
  15.         x = x-f(x)/d;
  16.         maxIterations = maxIterations-1;
  17.     end
  18. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement