In an assignment A(I) = B, the number of elements in B and I must be the same - error using ode 45

조회 수: 1 (최근 30일)
Hi,
I have written the following function:
function [dXdt] = wtfrackinetic(t,X)
%X(1)=xTMP;
%X(2)=xME;
%X(3)=xDE;
%X(4)=xTE;
%X(5)=xPOME;
%X(6)=xMethanol;
%mw1=mwTMP;
%mw2=mwME;
%mw3=mwDE;
%mw4=mwTE;
%mw5=mwPOME;
%mw6=mwMethanol;
%rho=Density of Material;
global k1 k2 k2r k3 k3r rho mw1 mw2 mw3 mw4 mw5 mw6;
dXdt=zeros(size(X));
dXdt(1)=-k1*X(1)*X(5)*rho/mw5;
dXdt(2)=k1*X(1)*X(5)*mw2*rho/mw1/mw5-k2*X(2)*X(5)*rho*mw5+k2r*X(3)*X(6)*rho*mw2/mw6/mw3;
dXdt(3)=k2*X(2)*X(5)*rho*mw3/mw2/mw5-k3*x(3)*X(5)*rho/mw5+k3r*X(4)*X(6)*rho*mw3/mw4/mw6-k2r*X(3)*X(6)*rho/mw6;
dXdt(4)=k3*X(3)*X(5)*rho*mw4/mw3/mw5-k3r*X(4)*X(6)*rho/mw6;
dXdt(5)=-k1*X(1)*X(5)*rho/mw1-k2*X(2)*X(5)*rho/mw2-k3*X(3)*X(5)*rho/mw3+k3r*X(4)*X(6)*rho*mw5/mw4/mw6+k2r*X(3)*X(6)*rho*mw5/mw3/mw6;
dXdt(6)=k1*X(1)*X(5)*rho*mw6/mw1/mw5+k2*X(2)*X(5)*rho*mw6/mw2/mw5+k3*X(3)*X(5)*rho*mw6/mw3/mw5-k3r*X(4)*X(6)*rho/mw4-k2r*X(3)*X(6)*rho/mw3;
The following are defined globally to allow easy manipulation:
X0=[0.0444 0 0 0 0.9556 0];rho=820.76;mw1=134;mw2=389;mw3=644;mw4=899;mw5=287;mw6=32;
k1=0.9;k2=0.7;k3=0.25;k2r=0.1;k3r=0.08;
Every time I attempt to run the function via the following command I get the following errors:
[t,X]=ode45('wtfrackinetic',[0 4],X0)
??? In an assignment A(I) = B, the number of
elements in B and
I must be the same.
Error in ==> wtfrackinetic at 17
dXdt(1)=-k1*X(1)*X(5)*rho/mw5;
Error in ==> odearguments at 110
f0 = feval(ode,t0,y0,args{:}); % ODE15I sets args{1}
to yp0.
Error in ==> ode45 at 173
[neq, tspan, ntspan, next, t0, tfinal, tdir, y0, f0,
odeArgs, odeFcn, ...
Any help will be appreciated and to give context this is for modelling transesterification reactions.
  댓글 수: 3
Geoff
Geoff 2012년 5월 2일
No worries. Sometimes I come off as a bit gruff, but I don't usually intend to. Welcome to Answers =)

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

답변 (2개)

Walter Roberson
Walter Roberson 2012년 5월 2일
Did you also "global" those variables in the routine that sets their values? If you did not then the variables you set would be local to the calling workspace, and when you got into your ode function, they would have [] as their value.

Richard Brown
Richard Brown 2012년 5월 2일
Hi Peter
Your code is actually fine (I just ran it). You must have something nasty hanging around to give that error (as Walter suggested, globals not declared correctly perhaps?). Incidentally there are better ways to write this kind of problem that avoid global - I can show you if you're interested. That said, you should be able to get it running by doing the following:
First, there is a small typo in your function: replace the lowercase x(3) in the dXdt(3) line with an uppercase one X(3).
Then, put the following lines into a script and run it
X0=[0.0444 0 0 0 0.9556 0];
global X0 k1 k2 k2r k3 k3r rho mw1 mw2 mw3 mw4 mw5 mw6;
rho=820.76;
mw1=134; mw2=389; mw3=644; mw4=899; mw5=287; mw6=32;
k1=0.9; k2=0.7; k3=0.25; k2r=0.1; k3r=0.08;
[t, X] = ode23s(@wtfrackinetic, [0 4], X0);
plot(t, X)
and all should be well. I used ode23s instead of ode45 because your problem appears to be stiff, and ode45 is horribly inefficient here.
If it doesn't run, try clear all before running your script.

카테고리

Help CenterFile Exchange에서 Ordinary Differential Equations에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by