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.

 采纳的回答

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 个评论

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.

请先登录,再进行评论。

更多回答(0 个)

类别

帮助中心File Exchange 中查找有关 Programming 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by