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 comentarios

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

Iniciar sesión para comentar.

 Respuesta aceptada

madhan ravi
madhan ravi el 29 de En. de 2019
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 comentario

Jingyu Kan
Jingyu Kan el 29 de En. de 2019
Thanks so much! I guessed this was the problem!

Iniciar sesión para comentar.

Más respuestas (1)

Walter Roberson
Walter Roberson el 29 de En. de 2019
Editada: Walter Roberson el 29 de En. de 2019

0 votos

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 .

Etiquetas

Preguntada:

el 29 de En. de 2019

Editada:

el 29 de En. de 2019

Community Treasure Hunt

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

Start Hunting!

Translated by