{"id":545,"date":"2017-09-14T13:31:45","date_gmt":"2017-09-14T16:31:45","guid":{"rendered":"http:\/\/www.professores.uff.br\/diomarcesarlobao\/?page_id=545"},"modified":"2017-09-14T13:31:45","modified_gmt":"2017-09-14T16:31:45","slug":"rk4ada2-m","status":"publish","type":"page","link":"https:\/\/www.professores.uff.br\/diomarcesarlobao\/rk4ada2-m\/","title":{"rendered":"rk4ada2.m"},"content":{"rendered":"<pre>function [t,x,v] = rk4ada2(func, a, b, x0, xd0, h, errmax);\r\n% solution of 2nd order ODE for spring-mass-damper\r\n% using RK 4th order with adaptive step size\r\n%\r\n% USAGE:  [t,x,v] = rk4ada2(func,a,b,x0,xd0,h1,errmax)\r\n%\r\n% input  func    = name of external function to evaluate the RHS\r\n%                  of the ODE for dv\/dt (see 'rhs_smd.m')\r\n%        a, b    = limits of integration\r\n%        x0      = initial condition - position\r\n%        xd0     = initial condition - velocity\r\n%        h       = initial stepsize\r\n%        errmax  = desired absolute accuracy\r\n%\r\n% output [t,x,v]  = solution vectors\r\n\r\nt = [a];\r\nx = [x0];\r\nv = [xd0];\r\ni = 1;\r\n\r\nwhile t(i) &lt; b\r\n\r\n  delta = 2*errmax;\r\n  while abs(delta) &gt; errmax\r\n\r\n   % first solution using h1 = h\r\n   h1 = h;\r\n   k1x = v(i);\r\n   k1v = feval(func, t(i)     , x(i)          , v(i)         );\r\n   k2x = v(i) + k1v*h1\/2;\r\n   k2v = feval(func, t(i)+h1\/2, x(i)+k1x*h1\/2 , v(i)+k1v*h1\/2);\r\n   k3x = v(i) + k2v*h1\/2;\r\n   k3v = feval(func, t(i)+h1\/2, x(i)+k2x*h1\/2 , v(i)+k2v*h1\/2);\r\n   k4x = v(i) + k3v*h1;\r\n   k4v = feval(func, t(i)+h1  , x(i)+k3x*h1   , v(i)+k3v*h1  );\r\n   x1 = x(i) + (k1x + 2*k2x + 2*k3x + k4x)*h1\/6;\r\n   v1 = v(i) + (k1v + 2*k2v + 2*k3v + k4v)*h1\/6;\r\n\r\n   % second solution using h2 = h \/ 2\r\n   h2 = h \/ 2;\r\n   k1x = v(i);\r\n   k1v = feval(func, t(i)     , x(i)          , v(i)         );\r\n   k2x = v(i) + k1v*h2\/2;\r\n   k2v = feval(func, t(i)+h2\/2, x(i)+k1x*h2\/2 , v(i)+k1v*h2\/2);\r\n   k3x = v(i) + k2v*h2\/2;\r\n   k3v = feval(func, t(i)+h2\/2, x(i)+k2x*h2\/2 , v(i)+k2v*h2\/2);\r\n   k4x = v(i) + k3v*h2;\r\n   k4v = feval(func, t(i)+h2  , x(i)+k3x*h2   , v(i)+k3v*h2  );\r\n\r\n   % mid-interval values\r\n   tm = t(i) + h2;\r\n   xm = x(i) + (k1x + 2*k2x + 2*k3x + k4x)*h2\/6;\r\n   vm = v(i) + (k1v + 2*k2v + 2*k3v + k4v)*h2\/6;\r\n\r\n   k1x = vm;\r\n   k1v = feval(func, tm     , xm          , vm         );\r\n   k2x = vm + k1v*h2\/2;\r\n   k2v = feval(func, tm+h2\/2, xm+k1x*h2\/2 , vm+k1v*h2\/2);\r\n   k3x = vm + k2v*h2\/2;\r\n   k3v = feval(func, tm+h2\/2, xm+k2x*h2\/2 , vm+k2v*h2\/2);\r\n   k4x = vm + k3v*h2;\r\n   k4v = feval(func, tm+h2  , xm+k3x*h2   , vm+k3v*h2  );\r\n\r\n   x2 = xm + (k1x + 2*k2x + 2*k3x + k4x)*h2\/6;\r\n   v2 = vm + (k1v + 2*k2v + 2*k3v + k4v)*h2\/6;\r\n\r\n   % estimate position error, modify solution, change next step size\r\n   delta = x2 - x1;\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   if delta ~= 0\r\n      h = h*(errmax\/abs(delta))^alpha;\r\n   end\r\n\r\n  end  % while delta &gt; errmax\r\n\r\n  i = i+1;\r\n  t(i) = t(i-1) + h1;\r\n  x(i) = x2 + delta\/15;\r\n  v(i) = v2 + (v2-v1)\/15;\r\n\r\nend  % while t&lt;b<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>function [t,x,v] = rk4ada2(func, a, b, x0, xd0, h, errmax); % solution of 2nd order ODE for spring-mass-damper % using RK 4th order with adaptive step size % % USAGE: [t,x,v] = rk4ada2(func,a,b,x0,xd0,h1,errmax) % % input func = name of external function to evaluate the RHS % of the ODE for dv\/dt (see &#8216;rhs_smd.m&#8217;) % [&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-545","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/www.professores.uff.br\/diomarcesarlobao\/wp-json\/wp\/v2\/pages\/545","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=545"}],"version-history":[{"count":1,"href":"https:\/\/www.professores.uff.br\/diomarcesarlobao\/wp-json\/wp\/v2\/pages\/545\/revisions"}],"predecessor-version":[{"id":546,"href":"https:\/\/www.professores.uff.br\/diomarcesarlobao\/wp-json\/wp\/v2\/pages\/545\/revisions\/546"}],"wp:attachment":[{"href":"https:\/\/www.professores.uff.br\/diomarcesarlobao\/wp-json\/wp\/v2\/media?parent=545"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.professores.uff.br\/diomarcesarlobao\/wp-json\/wp\/v2\/categories?post=545"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.professores.uff.br\/diomarcesarlobao\/wp-json\/wp\/v2\/tags?post=545"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}