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 个评论

MATLAB is not able to execute pictures of code and it is too much trouble to OCR the image.
"Could someone help me find what I did wrong?"
You uploaded images of code, instead of the code iteself.

请先登录,再进行评论。

 采纳的回答

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];
[t1,Y]=ode45(@first,tRange,Y0);
% ^--- name it different from another t
[t,X]=ode45(@second,tRange,X0);
concA=Y(:,1);
concB=Y(:,2);
concC=X(:,1);
concD=X(:,2);
concE=X(:,3);
plot(t1,concA,'k-')
hold on
plot(t1,concB,'b-')
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

更多回答(1 个)

Walter Roberson
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 .

类别

标签

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by