0

所以我需要求解 x''(t) = -x(t)^p 初始条件 x(0)= 0 和 v(0) = x'(0) = v_o = 1。参数 p 的值是 1。

这就是我所拥有的:

function [t, velocity, x] = ode_oscilation(p)

y=[0;0;0];
    % transform system to the canonical form

    function y = oscilation_equation(x,p)
        y=zeros(2,1);
        y(1)=y(2);
        y(2)=-(x)^p;
        %  to make matlab happy we need to return a column vector
        % so we transpose (note the dot in .')
        y=y.'; 
    end

    tspan=[0, 30]; % time interval of interest

    [t,velocity,x] = ode45(@oscilation_equation, tspan, 1); 

    t = y(:,1);
    xposition=y(:,3);
    velocity=y(:,2); 

end 

这是我收到的错误消息:

ode_oscillation(1) 使用 odearguments 时出错(第 91 行) ODE_OSCILLATION/OSCILATION_EQUATION 必须返回列向量。

ode45 错误(第 114 行)[neq, tspan, ntspan, next, t0, tfinal, tdir, y0, f0, odeArgs, odeFcn, ...

ode_oscillation 错误(第 17 行)[t,velocity,x] = ode45(@oscilation_equation, tspan,1);

4

1 回答 1

3

这里有一些问题。首先,来自help ode45

ode45 求解非刚性微分方程,中阶方法。

[TOUT,YOUT] = ode45(ODEFUN,TSPAN,Y0) with TSPAN = [T0 TFINAL] integrates 
the system of differential equations y' = f(t,y) from time T0 to TFINAL 
with initial conditions Y0. 

请注意,ode45需要一个函数f(t,y),其中size(t) == [1 1]用于时间和size(y) == [1 N][N 1]用于解决方案值。您oscilation_equation将输入参数的顺序反转,并且输入一个常量参数p而不是 time t

此外,初始条件Y0的大小应与y; 所以size(y0) == [N 1][1 N]。你只是有1,这显然会导致错误。

此外,您的输出参数t,xpositionvelocity将被完全忽略和错误,因为y设置为来自的输出参数ode45,最重要的是,它们的名称与 的输出参数不对应ode_oscilation。此外,它们从列中提取的顺序y不正确。

因此,总而言之,将所有内容更改为:

function [t, v, x] = ode_oscilation(p)

    % initial values
    y0 = [0 1];           

    % time interval of interest
    tspan =[0 30]; 

    % solve system 
    [t,y] = ode45(@(t,y) [y(2); -y(1)^p], tspan, y0);

    % and return values of interest
    x = y(:,1);
    v = y(:,2);

end
于 2012-11-05T05:33:27.627 回答