- 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 ビュー (過去 30 日間)
古いコメントを表示
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?
採用された回答
Jan
2018 年 8 月 15 日
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.
その他の回答 (1 件)
Stephen23
2018 年 8 月 15 日
編集済み: Stephen23
2018 年 8 月 15 日
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.
参考
カテゴリ
Help Center および File Exchange で Function Creation についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!