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.

 채택된 답변

James Tursa
James Tursa 2015년 5월 13일
편집: James Tursa 2015년 5월 13일

1 개 추천

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

H.O.Z.
H.O.Z. 2015년 5월 13일
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 2015년 5월 13일
You can assignin('base') the variable.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Programming에 대해 자세히 알아보기

태그

질문:

2015년 5월 13일

댓글:

2015년 5월 13일

Community Treasure Hunt

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

Start Hunting!

Translated by