ode15s XX must return a column vector

Hi,I am new to MATLAB. I need to calculate using ode15s, but I keep getting this error: XXX must return a column vector.
Here is my main script:
dp0 = 2e-7;
Np0 = 1e11;
V0 = pi/6*dp0^3;
C_bulk = 1.42e21*V0*Np0;
c0 = [0;C_bulk];
options = odeset('RelTol',1e-10);
[t,c] = ode15s(@myfun,[0:120],c0,options,V0,C_bulk);
plot(t,40.9*4.065e-14*338*c(1),'r.',
t,40.9*4.065e-14*338*c(2),'b.');
The myfun.m file that includes the ode equations are below:
function dcdt = myfun(t,c,V0,C_bulk)
V = V0*c(2)/C_bulk;
dp = (6*V/pi)^(1/3);
dcdt(1) = 1.2e17*dp-c(1);
dcdt(2) = c(1)-1.2e17*dp;
Please let me know why I get this error. Did I use the additional parameters (V0 and C_bulk) wrong? Thanks.

 Accepted Answer

James Tursa
James Tursa on 13 May 2015
Edited: James Tursa on 13 May 2015
The "column vector" error can be resolved by changing the return variable into a column vector. E.g.,
dcdt(1) = 1.2e17*dp-c(1);
dcdt(2) = c(1)-1.2e17*dp;
dcdt = dcdt(:); % <-- (:) notation results in column vector
When you build a vector piecemeal one index at a time like you do above, the default is for a row vector. Another way to solve this problem is to specify the column vector locations directly during the vector building process. E.g.,
dcdt(1,1) = 1.2e17*dp-c(1);
dcdt(2,1) = c(1)-1.2e17*dp;
And yet another way would be to pre-allocate the size of the return variable up front. E.g.,
dcdt = zeros(2,1);
dcdt(1) = 1.2e17*dp-c(1);
dcdt(2) = c(1)-1.2e17*dp;

2 Comments

Thanks James, I have one more question regarding this function. If I have a local variable in myfun.m that changes with time. What is the best way to display this variable in the base workspace? Thank you.
You can assignin('base') the variable.

Sign in to comment.

More Answers (0)

Categories

Find more on Programming in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!