{"id":533,"date":"2017-09-14T13:27:41","date_gmt":"2017-09-14T16:27:41","guid":{"rendered":"http:\/\/www.professores.uff.br\/diomarcesarlobao\/?page_id=533"},"modified":"2017-09-14T13:27:41","modified_gmt":"2017-09-14T16:27:41","slug":"rk4demo-m","status":"publish","type":"page","link":"https:\/\/www.professores.uff.br\/diomarcesarlobao\/rk4demo-m\/","title":{"rendered":"rk4demo.m"},"content":{"rendered":"<pre>% 4th order Runge-Kutta Demo\r\n%\r\n%  diff equation:  dy\/dx = y - SIN(x) + COS(x)        subject to y(0) = 1\r\n%\r\n%      file fcn1.m        function dyxy = fcn1(x,y)\r\n%                         dydx = y - sin(x) + cos(x);\r\n%\r\n%  exact solution is, y(x) = SIN(x) + EXP(x)\r\n\r\nh        = input('Enter step size:      ');\r\nprintint = input('Enter print interval: ');\r\n\r\nfprintf('\\n\\n  Step size: %g   print interval: %g\\n\\n', h, printint);\r\n\r\nx      = 0;    % initial conditions\r\nxmax   = 5;\r\nyRK    = 1;\r\nyeuler = yRK;\r\n\r\nfprintf('   x       yexact      y Euler   EulerError      y RK4    RK4Error\\n');\r\nfprintf('%6.2f %10.4f\\n',x,yRK);\r\n\r\ni = 0;\r\nwhile x &lt; xmax\r\n  yeuler = yeuler + fcn1(x, yeuler) * h;    % Euler solution\r\n \r\n  k1 = fcn1(x,         yRK);                % 4th order Runge-Kutta solution\r\n  k2 = fcn1(x + h \/ 2, yRK + h * k1 \/ 2);\r\n  k3 = fcn1(x + h \/ 2, yRK + h * k2 \/ 2);\r\n  k4 = fcn1(x + h,     yRK + h * k3);\r\n\r\n  yRK = yRK + (k1 + 2*k2 + 2*k3 + k4)*h \/ 6;\r\n  x   = x + h;\r\n\r\n  yexact     = sin(x) + exp(x);\r\n  Eulererror = (yeuler - yexact) \/ yexact * 100;\r\n  RKerror    = (yRK - yexact) \/ yexact * 100;\r\n\r\n  i = i + 1;\r\n  if (rem(i,printint) == 0)   % print every printint step\r\n    fprintf('%8.3f %10.4f %12.4f %10.3f %12.4f %12.3f\\n',...\r\n             x,yexact,yeuler,Eulererror,yRK,RKerror);\r\n    i = 0;\r\n  end;\r\nend;\r\n\r\n% Or try the one step Matlab solution...\r\n\r\n[xODE45,yODE45] = ode45('fcn1',0,5,1);\r\n\r\nfprintf('\\n        x       ode45 solution    error\\n');\r\nfor i = 1:length(xODE45)\r\n  x = xODE45(i);\r\n  yexact = sin(x) + exp(x);\r\n  ODE45error = (yODE45(i) - yexact) \/ yexact * 100;\r\n  fprintf('%12.4f  %12.4f  %12.5f\\n',x,yODE45(i),ODE45error);\r\nend;<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>% 4th order Runge-Kutta Demo % % diff equation: dy\/dx = y &#8211; SIN(x) + COS(x) subject to y(0) = 1 % % file fcn1.m function dyxy = fcn1(x,y) % dydx = y &#8211; sin(x) + cos(x); % % exact solution is, y(x) = SIN(x) + EXP(x) h = input(&#8216;Enter step size: &#8216;); printint = [&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-533","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/www.professores.uff.br\/diomarcesarlobao\/wp-json\/wp\/v2\/pages\/533","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=533"}],"version-history":[{"count":1,"href":"https:\/\/www.professores.uff.br\/diomarcesarlobao\/wp-json\/wp\/v2\/pages\/533\/revisions"}],"predecessor-version":[{"id":534,"href":"https:\/\/www.professores.uff.br\/diomarcesarlobao\/wp-json\/wp\/v2\/pages\/533\/revisions\/534"}],"wp:attachment":[{"href":"https:\/\/www.professores.uff.br\/diomarcesarlobao\/wp-json\/wp\/v2\/media?parent=533"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.professores.uff.br\/diomarcesarlobao\/wp-json\/wp\/v2\/categories?post=533"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.professores.uff.br\/diomarcesarlobao\/wp-json\/wp\/v2\/tags?post=533"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}