"Conc2 must return a Column Vector" error message. Identical Code Works with a different function.

1 view (last 30 days)
Hey guys, sorry if this isn't the ocrrect place to post this problem, first time poster. But I have a matlab code that isn't running at the moment. I've attached my main script with 2 functions that are seemingly identical, but one runs and the other does not. Any help will be greatly appreciated!
==============================================================
MAIN SCRIPT
global Cao Cbo k V t Ca1 Cb1 Ca2 Cb2 tspan
Cao = 2; %mol/L
Cbo = 2; %mol/L
C = [Cao, Cbo];
V = 200; %L
k = 0.025; %L/(mol/min)
t = 1000; %min
tspan = [0 t];
[t,y1] = ode45(@Conc1,tspan,C);
Ca1 = y1(69,1);
Cb1 = y1(69,2);
C2 = [Ca1, Cb1];
[t,y2] = ode45(@Conc2,tspan,C2);
=================================================
THIS IS THE FUNCTION THAT WORKS CORRECTLY
function dConc_dt=Conc1(t,C)
global Cao Cbo k V t
V = 200; %L
k = 0.025; %L/(mol/min)
dCa1_dt = -k*C(1)*C(2)+(Cao-C(1))/t;
dCb1_dt= -k*C(1)*C(2)+(Cbo-C(2))/t;
dConc_dt=[dCa1_dt;dCb1_dt];
end
======================================================
THIS IS THE FUNCTION THAT RETURNS THAT COLUMN VECTOR ERROR MESSAGE
function dConc2_dt = Conc2(t,C2)
global k V t Ca1 Cb1
k = 0.025; %L/(mol/min)
dCa2_dt = -k*C2(1)*C2(2)+(Ca1-C2(1))/t;
dCb2_dt = -k*C2(1)*C2(2)+(Cb1-C2(2))/t;
dConc2_dt = [dCa2_dt;dCb2_dt];
end
===========================================================
THIS IS THE EXACT ERROR MESSAGE
??? Error using ==> odearguments at 113
CONC2 must return a column vector.
Error in ==> ode45 at 173
[neq, tspan, ntspan, next, t0, tfinal, tdir, y0, f0, odeArgs, odeFcn, ...
Error in ==> CME550_HW8 at 34
[t,y2] = ode45(@Conc2,tspan,C2);

Answers (1)

Star Strider
Star Strider on 17 Oct 2014
I’m having problems running your code because of the globals and the test function files I use for MATLAB Answers problems.
As a troubleshooting step, take the semicolon off of the end of this line and see what displays to the Command Window:
dConc2_dt = [dCa2_dt;dCb2_dt];
  2 Comments
John
John on 18 Oct 2014
Edited: John on 18 Oct 2014
Here is the code without the globals and with the values are input directly into the functions. I'm now running the code, but returning all values that are NaN and NaN in both columns. I believe that my functions should produce real numbers when integrated and should definitely not be returning NaN...
MAIN SCRIPT
Cao = 2; %mol/L
Cbo = 2; %mol/L
C = [Cao, Cbo];
V = 200; %L
k = 0.025; %L/(mol/min)
t = 1000; %min
tspan = [0 t];
[t,y1] = ode45(@Conc1,tspan,C);
Ca1 = y1(69,1);
Cb1 = y1(69,2);
C2 = [Ca1, Cb1];
[t,y2] = ode45(@Conc2,tspan,C2);
=================================================
THIS IS THE FUNCTION THAT WORKS CORRECTLY
function dConc_dt=Conc1(t,C)
k = 0.025;
Cao = 2;
Cbo = 2;
V = 200; %L
k = 0.025; %L/(mol/min)
dCa1_dt = -k*C(1)*C(2)+(Cao-C(1))/t;
dCb1_dt= -k*C(1)*C(2)+(Cbo-C(2))/t;
dConc_dt=[dCa1_dt;dCb1_dt];
end
======================================================
THIS IS THE FUNCTION THAT RETURNS THAT COLUMN VECTOR ERROR MESSAGE
function dConc2_dt = Conc2(t,C2)
global
Ca1 = 0.2635;
Cb1 = 0.2635;
k = 0.025; %L/(mol/min)
dCa2_dt = -k*C2(1)*C2(2)+(Ca1-C2(1))/t;
dCb2_dt = -k*C2(1)*C2(2)+(Cb1-C2(2))/t;
dConc2_dt = [dCa2_dt;dCb2_dt];
end
===========================================================
Star Strider
Star Strider on 18 Oct 2014
In that code, both functions produce NaN values for all but the first time point (with the initial conditions), and that only in ‘Conc1’. The NaN values are due to your dividing by ‘t’ with an initial t=0, and initial conditions that also cause the numerator of that fraction to be 0. They then propagate through both equations since your initial conditions for your second equation are the last values of your first.
Change ‘tspan’ to not start with zero and the problem disappears and both functions integrate correctly:
tspan = 0.1:25:1000;
Change these lines as well so they are defined by the final results regardless of ‘tspan’:
Ca1 = y1(end,1);
Cb1 = y1(end,2);

Sign in to comment.

Categories

Find more on Simulink 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!