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
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
Stephen23 2019년 1월 29일
"Could someone help me find what I did wrong?"
You uploaded images of code, instead of the code iteself.

댓글을 달려면 로그인하십시오.

 채택된 답변

madhan ravi
madhan ravi 2019년 1월 29일

1 개 추천

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

Jingyu Kan
Jingyu Kan 2019년 1월 29일
Thanks so much! I guessed this was the problem!

댓글을 달려면 로그인하십시오.

추가 답변 (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 .

카테고리

태그

질문:

2019년 1월 29일

편집:

2019년 1월 29일

Community Treasure Hunt

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

Start Hunting!

Translated by