使用中点法将{Damped Harmonic Oscillator ODE'解决为一阶系统

时间:2018-04-17 20:18:11

标签: matlab

阻尼谐振子的精确解

$$ x''+ 2 \ gamma x'+ \ omega ^ 2 x = 0,\ quad x(0)= x_0,\ quad x'(0)= - \ gamma x_0 $$

$ 0< \ gamma< \ omega $是

$$ x(t)= x_0 e ^ { - \ gamma t} \ cos(\ beta t)\ quad \ text {where} \ quad \ beta:= \ sqrt {\ omega ^ 2 - \ gamma ^ 2} $$

请注意,通过进行替换,可以将此二阶ODE写为一阶系统:

$ x'= y $ and,

$ y'= -2 \ gamma y - \ omega ^ 2 x $

我想使用以下方法解决系统问题:

$$ \ dfrac {x_ {n + 1} - x_ {n-1}} {2h} = y_n \ quad \ quad \ dfrac {y_ {n + 1} - y_ {n-1}} {2h } = -2 \ gamma y_n - \ omega ^ 2 x_n。$$

这是一个明确的中点规则。这是我为问题构建的代码,但它没有给我正确的结果。我的情节没有像我预期的那样谐波行为。

function resonance

omega = 1;      % resonant frequency = sqrt(k/m)
a = 0.2;        % drag coeficient per unit mass
b = 0.1;        % driving amplitude per unit mass
omega0 = 1.2;   % driving frequency

tBegin = 0;     % time begin
tEnd = 80;      % time end

x0 = 0.2;       % initial position
v0 = 0.8;       % initial velocity

a = omega^2;    % calculate a coeficient from resonant frequency

% Use Runge-Kutta 45 integrator to solve the ODE
[t,w] = ode45(@derivatives, [tBegin tEnd], [x0 v0]);
x = w(:,1);     % extract positions from first column of w matrix
v = w(:,2);     % extract velocities from second column of w matrix

plot(t,x);

title('Damped, Driven Harmonic Oscillator');
ylabel('position (m)');
xlabel('time (s)');


% Function defining derivatives dx/dt and dv/dt
% uses the parameters a, b, A, omega0 in main program but changeth them not
function derivs = derivatives(tf,wf)
    xf = wf(1);            % wf(1) stores x
    vf = wf(2);            % wf(2) stores v
    dxdt = vf;                                     % set dx/dt = velocity
    dvdt = xf + 2 * b * vf + a * tf;  % set dv/dt = acceleration
    derivs = [dxdt; dvdt];  % return the derivatives
end

end

另外,我对格式化表示歉意。我用于数学堆栈交换,并且LaTeX样式格式似乎不适用于此,我不知道如何将我的数学运用于数学环境。

1 个答案:

答案 0 :(得分:1)

你错过了一个标志,它应该是

    dvdt = - ( xf + 2 * b * vf + a * tf );  % set dv/dt = acceleration

然而,整个表达式与前面提到的等式不一致,

x'' + 2*b*x' * a*x = 0

应该导致

    dvdt = - ( 2*b*vf + a*xf );  % set dv/dt = acceleration

但是,您再次定义了a两次,因此请将w2=omega^2更改为

    dvdt = - ( 2*b*vf + w2*xf + a );  % set dv/dt = acceleration