0
我正試圖解決MatLab中的一些ODE,並且看到方程中的變量是他們需要限制爲正數的數量。所以我在調用方程求解器之前嘗試使用odeset(),以使它們非負,但在繪製值之後,它們實際上是負數(在下面的代碼中是洋紅線)。我究竟做錯了什麼?在Matlab中使用NonNegative設置odeset()
下面是一些代碼:
%Lots of variables
includeJ=1;
cullLIRate=1/2000;
cullDIRate=1/2000;
N = 16800;
beta = 2e-7;
delta = 0.5;
gamma = 1/50;
sigma = 1/400;
mu = 1/365;
maxTime = 30*365;
kappa = N;
gR = 0.05;
mJ = 1/3650;
initJPerAdult = 10;
numInitE = 1000;
TSpan = [0,maxTime];
initState = [N-numInitE,numInitE,0,0,0,initJPerAdult*N];
%IMPORTANT BIT HERE
options = odeset('NonNegative', 1:6)
scirSoln = ode45(@equation,TSpan,initState,[],beta,delta,gamma,sigma,mu,kappa,gR,mJ,cullLIRate,cullDIRate,includeJ);
scirVals = deval(scirSoln,timeToPlot);
plot(timeToPlot,scirVals(1,:));
hold on;
plot(timeToPlot,scirVals(3,:),'k');
plot(timeToPlot,scirVals(4,:),'g');
plot(timeToPlot,scirVals(6,:),'m');
timeToPlot = [0:max(TSpan)/1000:max(TSpan)];
對等式(...)的代碼是:
function retVal = equation(t,y,beta,delta,gamma,sigma,mu,kappa,gR,mJ,cullLIRate,cullDIRate,includeJ)
retVal = zeros(6,1);
S = y(1);
E = y(2);
LI = y(3);
DI = y(4);
R = y(5);
J = y(6);
retVal(1)= mJ * J - beta * S * (delta * LI + DI);
retVal(2) = beta * S * (delta * LI + DI) - gamma * E;
retVal(3) = gamma * E - (cullLIRate + sigma) * LI;
retVal(4) = sigma * LI - (mu + cullDIRate) * DI;
retVal(5) = mu * DI + cullLIRate* LI + cullDIRate * DI;
retVal(6) = gR * S * (1 - S/kappa) - mJ * J;
end
您沒有將您定義的odeset(選項變量)傳遞給ODE45解算器。 ODE45的語法是: [T,Y] = ODE45(ODEFUN,TSPAN,Y0,OPTIONS,P1,P2 ...) 試試這個,如果你還有問題請回復我,我們再仔細看看。 – Groot 2013-02-24 16:44:38
謝謝你的工作!調整來自其他來源的代碼,但沒有意識到...如果你把這個作爲答案,我會高興地接受並給你一些榮譽:) – 2013-02-24 17:50:01