Vector length must be the same
显示 更早的评论
Could someone help me find what I did wrong?
I am trying to plot two system ODE in matlab, but the system shows error in vector length? I do not get what is wrong~
Here is my code:
tRange = [0,300];
concA0=1.2;
concB0=0.2;
concC0 = 1.2; concD0 = 1.2; concE0 = 1.2;
Y0=[concA0;concB0];
X0=[concC0;concD0;concE0];
[t,Y]=ode45(@first,tRange,Y0);
[t,X]=ode45(@second,tRange,X0);
concA=Y(:,1);
concB=Y(:,2);
concC=X(:,1);
concD=X(:,2);
concE=X(:,3);
hold on
plot(t,concA,'k-')
plot(t,concB,'b-')
hold off
plot(t,concC,'r-')
plot(t,concD,'m-')
plot(t,concE,'g-')
function dYdt = first(t,Y)
concA = Y(1);
concB = Y(2);
dAdt=-2.3*power(10,-3).*power(concA,4);
dBdt=2.3*power(10,-3).*power(concA,4);
dYdt=[dAdt;dBdt];
end
function dXdt = second(t,X)
concC = X(1);
concD = X(2);
concE = X(3);
dCdt=-8.5*power(10,-4).*concC.*power(concD,2);
dDdt=-2*8.5*power(10,-4).*concC.*power(concD,2);
dEdt=8.5*power(10,-4).*concC.*power(concD,2);
dXdt=[dCdt;dDdt;dEdt];
end
2 个评论
Walter Roberson
2019-1-29
MATLAB is not able to execute pictures of code and it is too much trouble to OCR the image.
Stephen23
2019-1-29
"Could someone help me find what I did wrong?"
You uploaded images of code, instead of the code iteself.
采纳的回答
更多回答(1 个)
Walter Roberson
2019-1-29
编辑:Walter Roberson
2019-1-29
0 个投票
When you pass a vector of two elements to ode45 as the time range then the number of entries it uses in the output is whatever it feels like using in order to get a result with the required error tolerance . That happens to end up being a different number for first than for second so the t returned for first is not the same length as for second but your code treats the two as if they were the same length and same values .
类别
在 帮助中心 和 File Exchange 中查找有关 Ordinary Differential Equations 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!