Vector length must be the same
Mostra commenti meno recenti
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 Commenti
Walter Roberson
il 29 Gen 2019
MATLAB is not able to execute pictures of code and it is too much trouble to OCR the image.
Stephen23
il 29 Gen 2019
"Could someone help me find what I did wrong?"
You uploaded images of code, instead of the code iteself.
Risposta accettata
Più risposte (1)
Walter Roberson
il 29 Gen 2019
Modificato: Walter Roberson
il 29 Gen 2019
0 voti
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 .
Categorie
Scopri di più su Ordinary Differential Equations in Centro assistenza e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!