Error using vertcat Dimensions of arrays being concatenated are not consistent.

15 views (last 30 days)
High everyone, I am unsuccessful in getting consistent dimensions for t and y in this code:
https://www.dropbox.com/s/7sz6gli891k0u4l/simultaneousEquations1.m?dl=0
What can I do?
  5 Comments
Dursman Mchabe
Dursman Mchabe on 15 Aug 2018
Thanks a lot for the comments, I will improve my spacing, and try to run the code again.

Sign in to comment.

Accepted Answer

Jan
Jan on 15 Aug 2018
I prefer spaces around each operator and keeping the parentheses only, where they improve the readability. Compare:
1.7e-3 -(y(12))*(y(14))/(y(10))
6.55e-8 -(y(12))*(y(15))/(y(14))
6.24-(y(12))*(y(13))/(y(8))
5.68e-5-(y(12))*(y(11))/(y(13))
5.3e-8 - (y(12))*(y(16))
(y(3))-(y(8))-(y(13))-(y(11))
(y(4))-(y(10))-(y(14))-(y(15))
(y(5))-(y(10))-(y(14))-(y(15))];
with
1.7e-3 - y(12) * y(14)) / y(10); ...
6.55e-8 - y(12) * y(15)) / y(14); ...
6.24 - y(12) * y(13) / y(8); ...
5.68e-5 - y(12) * y(11) / y(13); ...
5.3e-8 - y(12) * y(16); ...
y(3) - y(8) - y(13) - y(11); ...
y(4) - y(10) - y(14) - y(15); ...
y(5) - y(10) - y(14) - y(15)];
Consider Stephen's advice to avoid ambiguities in the code. The less readable the code is, the harder is it to debug. Splitting the calculations into separate lines is a good idea.

More Answers (1)

Stephen23
Stephen23 on 15 Aug 2018
Edited: Stephen23 on 15 Aug 2018
The problem is how you have written your vector with random space characters. For example this:
1.7e-3 -(y(12))*(y(14))/(y(10))...
^^ ouch!!!
MATLAB will interpret X -Y as equivalent to X,-Y. Why is this a problem? Because you have lots of lines with no spaces (giving only one column) and then some lines with spaces (giving multiple columns). Clearly these cannot be vertically concatenated together in any sensible way:
yp = [...
A-B-C % no spaces -> one column
A -B-C % space -> two columns
...]
This is clearly documented:
I recommend that you use consistent spacing, either one of these will work:
A - B
A-B
but you should NOT try to mix the two! I recommend that when concatenating like that, you should use explicit commas and semi-colons to delimit all elements of the matrix, e.g.:
yp = [...
A+B;
C-D-E;
F+G;
...]
This makes it clear what your intent was, whereas A -B-C is totally ambiguous: is it a mistake, or is that gap intentional? Using commas and semi-colons makes the code intent much clearer. Clearer code is easier to understand, and is much less buggy.
But in this case, because your calculations are quite verbose, I think it would be clearer to use indexing, like this:
yp = y;
yp(1) = A+B;
yp(2) = C-D-E;
yp(3) = F+G;
...
This has the major advantage that if there is any syntax error it will indicate the particular line on which the error occurs, making your debugging much simpler.
  2 Comments
Dursman Mchabe
Dursman Mchabe on 15 Aug 2018
Thanks a lot for the comments, I will improve my spacing, and try to run the code again.
Dursman Mchabe
Dursman Mchabe on 15 Aug 2018
I have sorted out the spacing. It did the trick.See the attached file/dropbox link:
https://www.dropbox.com/s/7sz6gli891k0u4l/simultaneousEquations1.m?dl=0
My current error is:
"Error using daeic12 (line 166) Need a better guess y0 for consistent initial conditions.
Error in ode15s (line 310) [y,yp,f0,dfdy,nFE,nPD,Jfac] = daeic12(odeFcn,odeArgs,t,ICtype,Mt,y,yp0,f0,...
Error in simultaneousEquations1 (line 79) [t,y] = ode15s(@(ti,yi)revisedModelode(ti,yi),tspan,y0,options);
>> "
Is there any method of determining a better guess?

Sign in to comment.

Categories

Find more on Creating and Concatenating Matrices in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!