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.

 Akzeptierte Antwort

James Tursa
James Tursa am 13 Mai 2015
Bearbeitet: James Tursa am 13 Mai 2015

1 Stimme

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 Kommentare

H.O.Z.
H.O.Z. am 13 Mai 2015
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.
Walter Roberson
Walter Roberson am 13 Mai 2015
You can assignin('base') the variable.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Programming finden Sie in Hilfe-Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by