📄 rksquare.mht
字号:
From: <Saved by Windows Internet Explorer 7>
Subject:
Date: Tue, 12 May 2009 09:54:49 -0700
MIME-Version: 1.0
Content-Type: text/html;
charset="Windows-1252"
Content-Transfer-Encoding: quoted-printable
Content-Location: http://www.mece.ualberta.ca/Courses/mec390/390code/rksquare.m
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.3350
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META http-equiv=3DContent-Type content=3D"text/html; =
charset=3Dwindows-1252">
<META content=3D"MSHTML 6.00.6000.16825" name=3DGENERATOR></HEAD>
<BODY><PRE>% 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 =3D 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) =20
%
% function a =3D smdrk4(x,v,f,c,k,m)
% a =3D (f - c*v - k*x)/m;
% define constants
m =3D 1; % mass
c =3D 1; % damping
k =3D 6; % spring stiffness
t0 =3D 0; % initial time
tmax =3D 40; % total time
dt =3D 0.1; % time step
t1 =3D 2; % start time of square wave
t2 =3D 14; % end time of square wave
fsquare =3D 10; % square wave amplitude
% initial conditions
x =3D 0; % position
v =3D 0; % velocity
time =3D t0;
fprintf(['Solution of Spring-Mass-Damper with Square ',...
'Wave Forcing Function\n']);
fprintf('Using manual 4th order Runge-Kutta and Matlab''s ode45\n');
% start Runge-Kutta solution
i =3D 0;
while time < tmax
time =3D time + dt;
ti =3D time - dt; % initial time for this time step
xi =3D x; % initial position for this time step
if (ti >=3D t1) & ( ti <=3D t2) % evaluate forcing =
function
f =3D fsquare; % amplitude
else
f =3D 0;
end
k1x =3D v;
k1v =3D smdrk4(x, v,f,c,k,m);
k2x =3D v + k1v * dt/2;
k2v =3D smdrk4(x + k1x * dt/2, v + k1v * dt/2,f,c,k,m);
k3x =3D v + k2v * dt/2;
k3v =3D smdrk4(x + k2x * dt/2, v + k2v * dt/2,f,c,k,m);
k4x =3D v + k3v * dt;
k4v =3D smdrk4(x + k3x * dt, v + k3v * dt,f,c,k,m);
x =3D x + (k1x + 2*k2x + 2*k3x + k4x) * dt/6;
v =3D v + (k1v + 2*k2v + 2*k3v + k4v) * dt/6;
i =3D i + 1;
position(i) =3D x;
timev(i) =3D time;
end;
% the one line Matlab solution; note NEW function: smdode45
[time45,results45] =3D ode45('smdode45',t0,tmax,[0 0]);
% view results
force =3D zeros(1,length(timev)); % create force vector for viewing
for i =3D t1/dt:t2/dt
force(i) =3D 1;=20
end
% plot the curves
plot(timev,position,time45,results45(:,1),'o',timev,force,'-');
title('Runge-Kutta solution of spring-mass-damper system');
legend('4th order Runge-Kutta','Matlab''s ode45','forcing function');
axis([t0 tmax -1 3]);
</PRE></BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -