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?

2 views (last 30 days)
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 Comments
Torsten
Torsten on 25 Nov 2024
Edited: Torsten on 25 Nov 2024
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.

Sign in to comment.

Answers (1)

Walter Roberson
Walter Roberson on 25 Nov 2024
Moved: Walter Roberson on 25 Nov 2024
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

Products


Release

R2024b

Community Treasure Hunt

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

Start Hunting!