error shows vertcat error

3 visualizzazioni (ultimi 30 giorni)
ishfaq ramzan
ishfaq ramzan il 20 Giu 2020
Risposto: Walter Roberson il 20 Giu 2020
error occurs in line 48 in which
p=[x1;x2;x3;x4;x5;x6;x7;x8;x9;d1;d2];

Risposte (1)

Walter Roberson
Walter Roberson il 20 Giu 2020
for n=1:71;
x1(i)=0;
So up to x1(71) at most is initialized
i=1;
j=150;
for k=i:j;
x1(k+1)=-5*x1(k)-100*x3(k)+5*u1(k);
k can go to j and j is 150, so up to x1(151) might be assigned to
i=i+1501;
Before this you do not change i from its initial 1, so the first time through this would change i to 1+1501 = 1502
j=j+1501;
Before this you do not change j from its initial 150, so the first time through this would change j to 150+1501 = 1652
d1(i)=abs(randn)/200;
i is 1502, so you assign to d1(1502), extending d1 with zeros between position 152 and 1501.
end
You do that a bunch of times in the for n loop. Each time, up to x1(j+1) will be assigned to, and then i will be incremented to 1501 past were it was before, and d1(i) will be assigned to, guaranteeing that d1 is assigned to at locations past where you assign to x1 and so on.
P= [x1;x2;x3;x4;x5;x6;x7;x8;x9;x10;d1;d2];
The x1* variables were only assigned to up to j+1 but d1 and d2 have been assigned to roughly 1000-ish past that. d1 and d2 are going to be longer than x1 and the other x* variables.
You do not initialize a shape for the x*() and d*() variables. In MATLAB when you extend a scalar by assigning to an element past the beginning, then MATLAB makes the vector into a row vector, not a column vector. So you have a bunch of row vectors the same size (the x*) being put down below each other (forming a 2D array) and then you try to put larger row vectors d1 d2 below those.
If you had initialized everything as column vectors instead of just growing the arrays, then your P would become one long column vector and there would be no size disagreement. It would probably not make sense to have that 1000 or so zeros just before the last assigned d1 and d2 values, but it would not be a size conflict.
The way you are using your i and j variables tells me that you should stop fiddling around with linear indices and should instead be creating 2D arrays

Community Treasure Hunt

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

Start Hunting!

Translated by