MATLAB: Ode15s XX must return a column vector

ode

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.

Best Answer

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;