2012-11-05 27 views
0
ODE45

所以我需要解決X ''(T)= -x(t)的^ P與初始條件X(0)= 0和v(0)= X'(0) = V_O = 1 參數p的值是1。二階DIFF等式與在Matlab

這是我有:

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,接下來,T0,T最終,tdir,Y0,F0, odeArgs,odeFcn,...

在ode_oscillation錯誤(第17行) [t,velocity,x] = ode45(@oscilation_equation,tspan,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有反相輸入參數的順序和時間t你輸入一個恆定的參數p代替。

此外,初始條件Y0應具有尺寸y相同;所以size(y0) == [N 1][1 N]。你只有1,這顯然是造成錯誤。

而且,你的輸出參數txpositionvelocity將被完全忽略和錯誤的,因爲yode45設置爲輸出參數,最重要的是,他們的名字不符合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