Why does Matlab tell me that my ODE function returns a vector of length 12, while the length of my initial conditions vector is 6?

4 次查看(过去 30 天)
Hi there!
I'm getting an error message from Matlab, telling me that my ODE model returns a vector of length 12, while the length of my initial conditions vector is 6. The state vector I've defined in my ODE function file, at the beginning of my code, is:
xG = z(1);
yG = z(2);
theta = z(3);
vGx = z(4);
vGy = z(5);
omega = z(6);
and, therefore, my first three, simple ODEs is [z(4);z(5);z(6)], and the last three ODEs are the derivatives of z(4), z(5), and z(6). So, it seems to me that I wrote a function that returns a vector of length 6, not the 12 that Matlab is suggesting.
What have I done wrong?
Thanks in advance,
  2 个评论
Torsten
Torsten 2024-11-25
编辑:Torsten 2024-11-25
MATLAB won't tell you that you return a vector of length 12 if you return a vector of length 6. But we must see your code to tell you what's going wrong.
You can easily determine the length of the vector returned to the ODE integrator by using
size(dydt)
at the end of your ODE function if "dydt" is the name of the output from your function.

请先登录,再进行评论。

采纳的回答

Walter Roberson
Walter Roberson 2024-11-25
移动:Walter Roberson 2024-11-25
It is common for this to happen if you accidentally forget to index the variable, For example,
dydt = [z(1).^2;
z(2)-z(4);
z+z(5)^3;
z(1)-5*z(6);
z(2)+sin(z(4));
z(3).*z(4).*z(5)+z(6)];
There might only be six rows in dydt, but the third one accidentally used unindexed z as part of the expression

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Ordinary Differential Equations 的更多信息

产品


版本

R2024b

Community Treasure Hunt

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

Start Hunting!

Translated by