- Please give the complete error message. This means all of the red text.
- Please upload any files here by clicking the paperclip button.
Error using vertcat Dimensions of arrays being concatenated are not consistent.
14 views (last 30 days)
Show older comments
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?
Accepted Answer
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
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.
See Also
Categories
Find more on Function Creation 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!