{"id":549,"date":"2017-09-14T13:32:16","date_gmt":"2017-09-14T16:32:16","guid":{"rendered":"http:\/\/www.professores.uff.br\/diomarcesarlobao\/?page_id=549"},"modified":"2017-09-14T13:32:16","modified_gmt":"2017-09-14T16:32:16","slug":"rk4ode2-m","status":"publish","type":"page","link":"https:\/\/www.professores.uff.br\/diomarcesarlobao\/rk4ode2-m\/","title":{"rendered":"rk4ode2.m"},"content":{"rendered":"<pre>function [t,x,v] = rk4ode2(func, a, b, x0, xd0, h);\r\n% Solution of 2nd order ODE using Runge-Kutta 4th order\r\n% with constant step size.  ODE solved is converted to\r\n% two 1st order equations.  The RHS of the system is\r\n%      dv\/dt = func(t, x, v)\r\n%      dx\/dt = v\r\n% See for example rhs_smd.m for forced spring-mass-damper\r\n%\r\n% USAGE:  [t, x, v] = rk4ode2(func,a,b,x0,xd0,h)\r\n%\r\n% input  func = name of external function to evaluate the RHS\r\n%                  of the ODE (eg 'rhs_smd')\r\n%        a, b = limits of integration\r\n%        x0   = initial condition (position)\r\n%        xd0  = initial condition (velocity)\r\n%        h    = stepsize\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   k1x = v(i);\r\n   k1v = feval(func, t(i)    , x(i)         , v(i)         );\r\n\r\n   k2x = v(i)+k1v*h\/2;\r\n   k2v = feval(func, t(i)+h\/2, x(i)+k1x*h\/2 , v(i)+k1v*h\/2 );\r\n\r\n   k3x = v(i)+k2v*h\/2;\r\n   k3v = feval(func, t(i)+h\/2, x(i)+k2x*h\/2 , v(i)+k2v*h\/2 );\r\n\r\n   k4x = v(i)+k3v*h;\r\n   k4v = feval(func, t(i)+h  , x(i)+k3x*h   , v(i)+k3v*h   );\r\n\r\n   i = i+1;\r\n   t(i) = t(i-1) + h;\r\n\r\n   x(i) = x(i-1) + (k1x + 2*k2x + 2*k3x + k4x)*h\/6;\r\n   v(i) = v(i-1) + (k1v + 2*k2v + 2*k3v + k4v)*h\/6;\r\n\r\nend\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>function [t,x,v] = rk4ode2(func, a, b, x0, xd0, h); % Solution of 2nd order ODE using Runge-Kutta 4th order % with constant step size. ODE solved is converted to % two 1st order equations. The RHS of the system is % dv\/dt = func(t, x, v) % dx\/dt = v % See for example rhs_smd.m [&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-549","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/www.professores.uff.br\/diomarcesarlobao\/wp-json\/wp\/v2\/pages\/549","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=549"}],"version-history":[{"count":1,"href":"https:\/\/www.professores.uff.br\/diomarcesarlobao\/wp-json\/wp\/v2\/pages\/549\/revisions"}],"predecessor-version":[{"id":550,"href":"https:\/\/www.professores.uff.br\/diomarcesarlobao\/wp-json\/wp\/v2\/pages\/549\/revisions\/550"}],"wp:attachment":[{"href":"https:\/\/www.professores.uff.br\/diomarcesarlobao\/wp-json\/wp\/v2\/media?parent=549"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.professores.uff.br\/diomarcesarlobao\/wp-json\/wp\/v2\/categories?post=549"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.professores.uff.br\/diomarcesarlobao\/wp-json\/wp\/v2\/tags?post=549"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}