{"id":453,"date":"2017-09-14T12:27:22","date_gmt":"2017-09-14T15:27:22","guid":{"rendered":"http:\/\/www.professores.uff.br\/diomarcesarlobao\/?page_id=453"},"modified":"2017-09-14T12:27:22","modified_gmt":"2017-09-14T15:27:22","slug":"newtraph-m","status":"publish","type":"page","link":"https:\/\/www.professores.uff.br\/diomarcesarlobao\/newtraph-m\/","title":{"rendered":"newtraph.m"},"content":{"rendered":"<pre>% Newton-Raphson root finding - single function\r\n%\r\n% The function and its derivative are defined in another script\r\n% file as follows: (Note that this won't accept vector inputs)\r\n%\r\n%   function [func, deriv] = fcn_nr(x)\r\n%       func = x^4 - 5*x^3 - 124*x^2 + 380*x + 1200;\r\n%       deriv = 4*x^3 - 15*x^2 - 248*x + 380;\r\n\r\n% First, let's plot the function.\r\nclear; clf; xp = -12:1:15; n = length(xp);\r\nfor i=1:n\r\n  yp(i) = fcn_nr(xp(i));\r\nend\r\nquickplt(xp,yp,'x','f(x)','Plot of function','grid')\r\nhold on\r\n\r\n% now proceed with finding a root:\r\n\r\nx = input('Enter initial guess of root location: ');\r\n\r\nitermax = 100;      % max # of iterations\r\niter = 0;\r\nerrmax = 0.001;     % convergence tolerance\r\nerror = 1;\r\nfprintf('\\n   #      root       rel error\\n\\n');\r\n\r\nwhile error &gt; errmax &amp; iter &lt; itermax\r\n\r\n  iter = iter + 1;\r\n  [f fprime] = fcn_nr(x);\r\n  if fprime == 0\r\n    fprintf('ERROR: deriv(x) = 0; can''t divide by zero\\n')\r\n    break;\r\n  end;\r\n\r\n  xnew = x - f \/ fprime;    % here is new root estimate\r\n\r\n  error = abs((xnew - x)\/xnew) * 100;  % find change from previous\r\n  fprintf('%3i   %10.6f   %10.5f\\n',iter,xnew,error);\r\n  x = xnew;      % set up for next iteration\r\n\r\nend\r\nplot(x, fcn_nr(x),'ro')\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>% Newton-Raphson root finding &#8211; single function % % The function and its derivative are defined in another script % file as follows: (Note that this won&#8217;t accept vector inputs) % % function [func, deriv] = fcn_nr(x) % func = x^4 &#8211; 5*x^3 &#8211; 124*x^2 + 380*x + 1200; % deriv = 4*x^3 &#8211; 15*x^2 [&hellip;]<\/p>\n","protected":false},"author":22,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"_exactmetrics_skip_tracking":false,"_exactmetrics_sitenote_active":false,"_exactmetrics_sitenote_note":"","_exactmetrics_sitenote_category":0,"footnotes":""},"categories":[],"tags":[],"class_list":["post-453","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/www.professores.uff.br\/diomarcesarlobao\/wp-json\/wp\/v2\/pages\/453","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.professores.uff.br\/diomarcesarlobao\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.professores.uff.br\/diomarcesarlobao\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.professores.uff.br\/diomarcesarlobao\/wp-json\/wp\/v2\/users\/22"}],"replies":[{"embeddable":true,"href":"https:\/\/www.professores.uff.br\/diomarcesarlobao\/wp-json\/wp\/v2\/comments?post=453"}],"version-history":[{"count":1,"href":"https:\/\/www.professores.uff.br\/diomarcesarlobao\/wp-json\/wp\/v2\/pages\/453\/revisions"}],"predecessor-version":[{"id":454,"href":"https:\/\/www.professores.uff.br\/diomarcesarlobao\/wp-json\/wp\/v2\/pages\/453\/revisions\/454"}],"wp:attachment":[{"href":"https:\/\/www.professores.uff.br\/diomarcesarlobao\/wp-json\/wp\/v2\/media?parent=453"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.professores.uff.br\/diomarcesarlobao\/wp-json\/wp\/v2\/categories?post=453"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.professores.uff.br\/diomarcesarlobao\/wp-json\/wp\/v2\/tags?post=453"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}