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

14 ビュー (過去 30 日間)
Dursman Mchabe
Dursman Mchabe 2018 年 8 月 15 日
コメント済み: Dursman Mchabe 2018 年 8 月 15 日
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 件のコメント
Stephen23
Stephen23 2018 年 8 月 15 日
編集済み: Stephen23 2018 年 8 月 15 日
"however i have no clue why"
No Voodoo required, just reading the MATLAB documentation.
Dursman Mchabe
Dursman Mchabe 2018 年 8 月 15 日
Thanks a lot for the comments, I will improve my spacing, and try to run the code again.

サインインしてコメントする。

採用された回答

Jan
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
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.
  2 件のコメント
Dursman Mchabe
Dursman Mchabe 2018 年 8 月 15 日
Thanks a lot for the comments, I will improve my spacing, and try to run the code again.
Dursman Mchabe
Dursman Mchabe 2018 年 8 月 15 日
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?

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeFunction Creation についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by