{"id":551,"date":"2017-09-14T13:32:33","date_gmt":"2017-09-14T16:32:33","guid":{"rendered":"http:\/\/www.professores.uff.br\/diomarcesarlobao\/?page_id=551"},"modified":"2017-09-14T13:32:33","modified_gmt":"2017-09-14T16:32:33","slug":"rksquare-m","status":"publish","type":"page","link":"https:\/\/www.professores.uff.br\/diomarcesarlobao\/rksquare-m\/","title":{"rendered":"rksquare.m"},"content":{"rendered":"<pre>% Runge-Kutta solution of second order O.D.E.\r\n% damped spring-mass system with applied square wave forcing function\r\n%\r\n%  ODE is:   m*a + c*v + k*x = f\r\n%\r\n% where f is a square wave of specified amplitude from time t1 to t2\r\n\r\nclear;\r\n\r\n% define function file smdrk4.m (spring-mass-damper Runge-Kutta 4th order)     \r\n%\r\n%   function a = smdrk4(x,v,f,c,k,m)\r\n%   a = (f - c*v - k*x)\/m;\r\n\r\n% define constants\r\n\r\nm = 1;         % mass\r\nc = 1;         % damping\r\nk = 6;         % spring stiffness\r\n\r\nt0 = 0;        % initial time\r\ntmax = 40;     % total time\r\ndt = 0.1;      % time step\r\n\r\nt1 = 2;        % start time of square wave\r\nt2 = 14;       % end time of square wave\r\nfsquare = 10;  % square wave amplitude\r\n\r\n% initial conditions\r\n\r\nx = 0;         % position\r\nv = 0;         % velocity\r\ntime = t0;\r\n\r\nfprintf(['Solution of Spring-Mass-Damper with Square ',...\r\n         'Wave Forcing Function\\n']);\r\nfprintf('Using manual 4th order Runge-Kutta and Matlab''s ode45\\n');\r\n\r\n% start Runge-Kutta solution\r\n\r\ni = 0;\r\nwhile time &lt; tmax\r\n  time = time + dt;\r\n  ti = time - dt;     % initial time for this time step\r\n  xi = x;             % initial position for this time step\r\n\r\n  if (ti &gt;= t1) &amp; ( ti &lt;= t2)       % evaluate forcing function\r\n    f = fsquare;                    % amplitude\r\n  else\r\n    f = 0;\r\n  end\r\n\r\n  k1x = v;\r\n  k1v = smdrk4(x, v,f,c,k,m);\r\n\r\n  k2x = v + k1v * dt\/2;\r\n  k2v = smdrk4(x + k1x * dt\/2, v + k1v * dt\/2,f,c,k,m);\r\n\r\n  k3x = v + k2v * dt\/2;\r\n  k3v = smdrk4(x + k2x * dt\/2, v + k2v * dt\/2,f,c,k,m);\r\n\r\n  k4x = v + k3v * dt;\r\n  k4v = smdrk4(x + k3x * dt, v + k3v * dt,f,c,k,m);\r\n\r\n  x = x + (k1x + 2*k2x + 2*k3x + k4x) * dt\/6;\r\n  v = v + (k1v + 2*k2v + 2*k3v + k4v) * dt\/6;\r\n\r\n  i = i + 1;\r\n  position(i) = x;\r\n  timev(i) = time;\r\nend;\r\n\r\n% the one line Matlab solution; note NEW function: smdode45\r\n\r\n[time45,results45] = ode45('smdode45',t0,tmax,[0 0]);\r\n\r\n% view results\r\n\r\nforce = zeros(1,length(timev));  % create force vector for viewing\r\nfor i = t1\/dt:t2\/dt\r\n  force(i) = 1; \r\nend\r\n\r\n% plot the curves\r\n\r\nplot(timev,position,time45,results45(:,1),'o',timev,force,'-');\r\ntitle('Runge-Kutta solution of spring-mass-damper system');\r\nlegend('4th order Runge-Kutta','Matlab''s ode45','forcing function');\r\naxis([t0 tmax -1 3]);\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>% Runge-Kutta solution of second order O.D.E. % damped spring-mass system with applied square wave forcing function % % ODE is: m*a + c*v + k*x = f % % where f is a square wave of specified amplitude from time t1 to t2 clear; % define function file smdrk4.m (spring-mass-damper Runge-Kutta 4th order) % [&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-551","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/www.professores.uff.br\/diomarcesarlobao\/wp-json\/wp\/v2\/pages\/551","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=551"}],"version-history":[{"count":1,"href":"https:\/\/www.professores.uff.br\/diomarcesarlobao\/wp-json\/wp\/v2\/pages\/551\/revisions"}],"predecessor-version":[{"id":552,"href":"https:\/\/www.professores.uff.br\/diomarcesarlobao\/wp-json\/wp\/v2\/pages\/551\/revisions\/552"}],"wp:attachment":[{"href":"https:\/\/www.professores.uff.br\/diomarcesarlobao\/wp-json\/wp\/v2\/media?parent=551"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.professores.uff.br\/diomarcesarlobao\/wp-json\/wp\/v2\/categories?post=551"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.professores.uff.br\/diomarcesarlobao\/wp-json\/wp\/v2\/tags?post=551"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}