{"id":541,"date":"2017-09-14T13:30:25","date_gmt":"2017-09-14T16:30:25","guid":{"rendered":"http:\/\/www.professores.uff.br\/diomarcesarlobao\/?page_id=541"},"modified":"2017-09-14T13:30:25","modified_gmt":"2017-09-14T16:30:25","slug":"rk4ada-m","status":"publish","type":"page","link":"https:\/\/www.professores.uff.br\/diomarcesarlobao\/rk4ada-m\/","title":{"rendered":"rk4ada.m"},"content":{"rendered":"<pre>function [t,y] = rk4ada(func, a, b, y0, h1,errmax);\r\n% solution of 1st order ODE using RK 4th order with adaptive step size\r\n%\r\n%      dy\/dt = func(t,y)\r\n%\r\n% USAGE:  [t,y] = rk4ada(func,a,b,y0,h1,eps)\r\n%\r\n% input  func    = name of external function to evaluate the RHS\r\n%                  of the ODE\r\n%        a, b    = limits of integration\r\n%        y0      = initial condition\r\n%        h1      = initial stepsize\r\n%        errmax  = desired absolute accuracy\r\n%\r\n% output [t, y]  = solution vectors\r\n\r\nt = [a];\r\ny = [y0];\r\ni = 1;\r\n\r\nwhile t(i) &lt; b\r\n\r\n   % first solution using h = h1\r\n   h = h1;\r\n   k1 = feval(func, t(i)    , y(i)       );\r\n   k2 = feval(func, t(i)+h\/2, y(i)+k1*h\/2);\r\n   k3 = feval(func, t(i)+h\/2, y(i)+k2*h\/2);\r\n   k4 = feval(func, t(i)+h  , y(i)+k3*h  );\r\n   y1 = y(i) + (k1 + 2*k2 + 2*k3 + k4)*h\/6;\r\n\r\n   % second solution using h = h1 \/ 2\r\n   h = h1 \/ 2;\r\n   k1 = feval(func, t(i)    , y(i)       );\r\n   k2 = feval(func, t(i)+h\/2, y(i)+k1*h\/2);\r\n   k3 = feval(func, t(i)+h\/2, y(i)+k2*h\/2);\r\n   k4 = feval(func, t(i)+h  , y(i)+k3*h  );\r\n   tm = t(i) + h;\r\n   ym = y(i) + (k1 + 2*k2 + 2*k3 + k4)*h\/6;\r\n\r\n   k1 = feval(func, tm    , ym       );\r\n   k2 = feval(func, tm+h\/2, ym+k1*h\/2);\r\n   k3 = feval(func, tm+h\/2, ym+k2*h\/2);\r\n   k4 = feval(func, tm+h  , ym+k3*h  );\r\n   y2 = ym + (k1 + 2*k2 + 2*k3 + k4)*h\/6;\r\n\r\n   % estimate error, modify solution, change next step size\r\n   delta = y2 - y1;\r\n\r\n   i = i+1;\r\n   t(i) = t(i-1) + h1;\r\n   y(i) = y2 + delta\/15;\r\n\r\n   if abs(delta) &lt; errmax   % error below tolerance\r\n      alpha = .2;\r\n   else                  % error too big\r\n      alpha = .25;\r\n   end\r\n\r\n   h1 = h1*(errmax\/abs(delta))^alpha\r\n\r\nend<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>function [t,y] = rk4ada(func, a, b, y0, h1,errmax); % solution of 1st order ODE using RK 4th order with adaptive step size % % dy\/dt = func(t,y) % % USAGE: [t,y] = rk4ada(func,a,b,y0,h1,eps) % % input func = name of external function to evaluate the RHS % of the ODE % a, b = limits [&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-541","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/www.professores.uff.br\/diomarcesarlobao\/wp-json\/wp\/v2\/pages\/541","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=541"}],"version-history":[{"count":1,"href":"https:\/\/www.professores.uff.br\/diomarcesarlobao\/wp-json\/wp\/v2\/pages\/541\/revisions"}],"predecessor-version":[{"id":542,"href":"https:\/\/www.professores.uff.br\/diomarcesarlobao\/wp-json\/wp\/v2\/pages\/541\/revisions\/542"}],"wp:attachment":[{"href":"https:\/\/www.professores.uff.br\/diomarcesarlobao\/wp-json\/wp\/v2\/media?parent=541"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.professores.uff.br\/diomarcesarlobao\/wp-json\/wp\/v2\/categories?post=541"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.professores.uff.br\/diomarcesarlobao\/wp-json\/wp\/v2\/tags?post=541"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}