Error using vertcat in using ode45

1 view (last 30 days)
Shuo Zhang
Shuo Zhang on 19 Jul 2017
Edited: Jan on 19 Jul 2017
I'm trying to solve a system of 126 PDEs with ode45. In the function "nsode.m" I have typed all the equations and in another file "mode1.m" I have listed the initial conditions for each variable. However I always get the following error messages(shown in the image). How can I solve it?

Answers (1)

Jan
Jan on 19 Jul 2017
Edited: Jan on 19 Jul 2017
Wow, this code is unreadable. Did you create it manually? This would be such prone to typos, that I'd never trust the results. It is much easier to create such huge regulare matrices by using the vector notation. E.g. replace
TT=P(1)^2+P(2)^2+P(3)^2+P(4)^2+P(5)^2+P(6)^2+P(7)^2+P(8)^2+P(9)^2+P(10)^2 ...
+P(11)^2+P(12)^2+P(13)^2+P(14)^2+P(15)^2+P(16)^2+P(17)^2+P(18)^2+P(19)^2+P(20)^2 ...
+P(21)^2+P(22)^2+P(23)^2+P(24)^2+P(25)^2+P(26)^2+P(27)^2+P(28)^2+P(29)^2+P(30)^2 ...
+P(31)^2+P(32)^2+P(33)^2+P(34)^2+P(35)^2+P(36)^2+P(37)^2+P(38)^2+P(39)^2+P(40)^2 ...
+P(41)^2+P(42)^2+P(43)^2+P(44)^2+P(45)^2+P(46)^2+P(47)^2+P(48)^2+P(49)^2+P(50)^2 ...
+P(51)^2+P(52)^2+P(53)^2+P(54)^2+P(55)^2+P(56)^2+P(57)^2+P(58)^2+P(59)^2+P(60)^2;
by
TT = sum(P.^2);
% Or even faster:
TT = P * P.';
Instead of the slow slow without preallocation:
for i=1:1:60
P(i)=sin(pi*i*ls/L);
end
You can define:
P = sin((1:60) * (pi * ls / L));
The rest can be abbreviated massively also, e.g.
y0 = [0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0];
by
y0 = zeros(126, 1);
Do you see the idea? The reader should see on first view, what the purpose of the code is. This is impossible in the [0;0;0;0;0; ...] formulation.
It is not efficient to search the problem of the line 62, because the code is not readable. Use the vector notation instead or at least loops. It would be a waste of time to search for the typo here, because you could never be sure if there is no other typo also.

Community Treasure Hunt

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

Start Hunting!