In an assignment A(I) = B, the number of elements in B and I must be the same - error using ode 45
Show older comments
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 Comments
Geoff
on 1 May 2012
That's unreadable. Please edit, select the code, press the 'code' button in the editor toolbar. You will see the results in the preview window below it. And make sure you have a blank line before and after each code section. See, that's much better. Thanks! =)
Peter Smith
on 2 May 2012
Geoff
on 2 May 2012
No worries. Sometimes I come off as a bit gruff, but I don't usually intend to. Welcome to Answers =)
Answers (2)
Walter Roberson
on 2 May 2012
0 votes
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
on 2 May 2012
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.
Categories
Find more on Ordinary Differential Equations in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!