【发布时间】:2014-02-05 16:06:16
【问题描述】:
我正在尝试使用 Runge-Kutta 方法在 matlab 中解决强制质量弹簧阻尼系统。 目前,代码使用常量值作为系统输入,但我想将向量作为输入。例如,我想用矢量值替换我的力幅值 F0。
我应该使用 for 循环还是最简单的方法?
function O = MSDSRK(m,b,k,F0,w,x0,v0)
% ----- Input argument -----
% m: mass for particle
% b: damping coefficient
% k: spring constant
% F0: amplitude of external force
% w: angular freuency of external force
% x0: initial condition for the position x(0)
% v0: initial condition for the velocity v(0)
dt=0.1;
options=odeset('InitialStep',dt,'MaxStep',dt);
td=[0:dt:50];
% Solve differential equation with Runge-Kutta solver
[t,x]=ode45(@(t,X)MSD(t,X,m,b,k,F0,w),td,[x0;v0],options);
% Extract only particle position trajectory
O=[t x(:,1)];
end
function dX=MSD(t,X,m,b,k,F0,w)
% With two 1st order diffeential equations,
% obtain the derivative of each variables
% (position and velocity of the particle)
dX(1,1)=X(2,1);
dX(2,1)=(1/m)*(F0*sin(w*t)-b*X(2,1)-k*X(1,1));
end
【问题讨论】:
-
在这种情况下我会使用循环,因为你的函数的输出已经是一个向量。如果你真的想向量化它,让你函数以矩阵的形式产生输出,其中一个维度对应于不同的 F0 值。
标签: matlab