Advertisement
Guest User

Untitled

a guest
Oct 23rd, 2019
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.40 KB | None | 0 0
  1. function result = mvnewton(f,g,aprox,tol)
  2.  
  3. %Argumenty fce:
  4. % f,g - matlab function_handle
  5. % aprox - pocatecni iterace (bod o dvou souradnicich)
  6. % tol - volitelny argument, tolerance pri jejiz dosazeni iterovani konci
  7. %Return fce:
  8. % result - priblizna hodnota hledaneho reseni (bod o dvou souradnicich)
  9.  
  10. if nargin < 3
  11. error('Nedostatecny pocet zadanych argumentu')
  12. end
  13.  
  14. if(~isa(f,'function_handle') || ~isa(g,'function_handle'))
  15. error('f nebo g nejsou matlabovsky function_handle')
  16. end
  17.  
  18. if (~exist('tol', 'var'))
  19. tol = 0.01;
  20. end
  21.  
  22. iter = 1;
  23. maxiter = 1000; % maxiter - maximalni pocet iteraci pri kterem iterace konci
  24. syms x;
  25. syms y;
  26. f = sym(f);
  27. g = sym(g);
  28. fx = diff(f,x);
  29. fy = diff(f,y);
  30. gx = diff(g,x);
  31. gy = diff(g,y);
  32.  
  33. F = [f;g];
  34. J = [fx, fy ; gx, gy];
  35.  
  36. x = aprox(1); y = aprox(2);
  37.  
  38. xy = [x;y] - inv(eval(J))*eval(F);
  39. x = xy(1); y = xy(2);
  40. if isnan(xy)
  41. error('x nebo y nabylo v prubehu iterace hodnoty NaN')
  42. end
  43. result = xy;
  44.  
  45.  
  46. while (abs(eval(f))+ abs(eval(g))) > tol
  47. if(iter > maxiter)
  48. error('1 000 iteraci nedostacovalo k dosahnuti pozadovane presnosti')
  49. end
  50.  
  51. if(~isreal(aprox) || isnan(x) || isnan(y) || isinf(x) || isinf(y))
  52. error('V prubehu iterace souradnice x,y nebyly konecne realne c.')
  53. end
  54.  
  55. result = xy;
  56.  
  57. xy = [x;y] - inv(eval(J))*eval(F);
  58. x = xy(1); y = xy(2);
  59. iter = iter+1;
  60. end
  61. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement