📄 eusquare.mht
字号:
From: <Saved by Windows Internet Explorer 7>
Subject:
Date: Tue, 12 May 2009 09:53:40 -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/eusquare.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>% 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 =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.02; % 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 Euler''s method\n');
% start Euler 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
xnew =3D x + v*dt;
vnew =3D v + smdrk4(x,v,f,c,k,m)*dt;
x =3D xnew;
v =3D vnew;
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('Euler and ode45 solution of spring-mass-damper system');
legend('Euler','Matlab''s ode45','forcing function');
axis([t0 tmax -1 3]);
</PRE></BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -