【问题标题】:Second Order Diff Eq with ode45 in MatlabMatlab中带有ode45的二阶差分方程
【发布时间】:2015-07-17 12:16:13
【问题描述】:

所以我需要用初始条件 x(0)= 0 和 v(0) = x'(0) = v_o = 1 求解 x''(t) = -x(t)^p。 参数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) 使用 oearguments 时出错(第 91 行) ODE_OSCILLATION/OSCILATION_EQUATION 必须返回一个 列向量。

ode45 中的错误(第 114 行) [neq, tspan, ntspan, 下一个, t0, tfinal, tdir, y0, f0, odeArgs, odeFcn, ...

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

【问题讨论】:

    标签: matlab ode


    【解决方案1】:

    这里有一些问题。一、来自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 而不是时间t

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

    此外,您的输出参数txpositionvelocity 将被完全忽略和错误,因为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
    

    【讨论】:

      猜你喜欢
      • 2013-11-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多