{"id":531,"date":"2017-09-14T13:27:23","date_gmt":"2017-09-14T16:27:23","guid":{"rendered":"http:\/\/www.professores.uff.br\/diomarcesarlobao\/?page_id=531"},"modified":"2017-09-14T13:27:23","modified_gmt":"2017-09-14T16:27:23","slug":"eusquare-m","status":"publish","type":"page","link":"https:\/\/www.professores.uff.br\/diomarcesarlobao\/eusquare-m\/","title":{"rendered":"eusquare.m"},"content":{"rendered":"<pre>% Euler 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.02;    % 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 Euler''s method\\n');\r\n\r\n% start Euler 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  xnew = x + v*dt;\r\n  vnew = v + smdrk4(x,v,f,c,k,m)*dt;\r\n\r\n  x = xnew;\r\n  v = vnew;\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('Euler and ode45 solution of spring-mass-damper system');\r\nlegend('Euler','Matlab''s ode45','forcing function');\r\naxis([t0 tmax -1 3]);\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>% Euler 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-531","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/www.professores.uff.br\/diomarcesarlobao\/wp-json\/wp\/v2\/pages\/531","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=531"}],"version-history":[{"count":1,"href":"https:\/\/www.professores.uff.br\/diomarcesarlobao\/wp-json\/wp\/v2\/pages\/531\/revisions"}],"predecessor-version":[{"id":532,"href":"https:\/\/www.professores.uff.br\/diomarcesarlobao\/wp-json\/wp\/v2\/pages\/531\/revisions\/532"}],"wp:attachment":[{"href":"https:\/\/www.professores.uff.br\/diomarcesarlobao\/wp-json\/wp\/v2\/media?parent=531"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.professores.uff.br\/diomarcesarlobao\/wp-json\/wp\/v2\/categories?post=531"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.professores.uff.br\/diomarcesarlobao\/wp-json\/wp\/v2\/tags?post=531"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}