2017-01-09 38 views
1

我想學習如何使用MATLAB解微分方程(Lorenz系統)的系統,並繪製每個解決方案作爲t的函數常微分方程的系統解決在使用ODE45

X’ = −σx + σy 
Y’ = ρx − y − xz 
Z’ = −βz + xy 

其中σ = 10β = 8/3ρ = 28以及x(0) = −8,y(0) = 8z(0) = 27

這裏是我使用的代碼:

function xprime = example(t,x) 

sig = 10; 
beta = 8/3; 
rho = 28; 
xprime = [-sig*x(1) + sig*x(2); 
      rho*x(1) - x(2) - x(1)*x(3); 
      -beta*x(3) + x(1)*x(2)]; 

x0 = [-8 8 27];  
tspan = [0 20];  
[t,x] = ode45(@example, tspan, x0); 

figure  
plot(t,x(:,1)), hold on 
plot(t,x(:,2)), hold on 
plot(t,x(:,3)), hold off 

然而,這會產生一個錯誤,我該如何解決這個問題?我不確定哪些輸入參數丟失或哪裏出錯。我感謝任何幫助,謝謝。

輸入參數不足。

示例中的錯誤(第9行) xprime = [ - sig x(1)+ sig x(2); (1) - x(2) - x(1)x(3); -βeta x(3)+
x(1)* x(2)];

+0

你遞歸 - 是故意的,還是...? –

+0

也許,我只是因爲我不知道寫一個更好的方法。 – Tina

回答

3

這實際上是一個不錯的第一次嘗試!

問題是,當您按運行按鈕(或按F5)時,您調用功能example沒有參數;這是MATLAB抱怨的。

的第二個問題是,即使你要能夠運行這樣的功能,ode45會調用該函數example,這將調用ode45,它會叫example,這將調用ode45等,直到達到了遞歸限制。

的解決這兩個是它在兩個功能(這些可以被寫入同一個M文件)拆分:

% top-level function; no arguments 
function caller() 

    x0 = [-8 8 27]; 
    tspan = [0 20]; 
    [t,x] = ode45(@example, tspan, x0); 

    figure 
    plot(t,x(:,1)), hold on 
    plot(t,x(:,2)), hold on 
    plot(t,x(:,3)), hold off 

end 

% The derivative 
function xprime = example(t,x) 

    sig = 10; 
    beta = 8/3; 
    rho = 28; 
    xprime = [-sig*x(1) + sig*x(2); 
       rho*x(1) - x(2) - x(1)*x(3); 
       -beta*x(3) + x(1)*x(2)];    
end