简体   繁体   中英

Error using vertcat in MatLab

im getting a vertcat error in my for loop, i have looked about for a solution but i cant seem to find one that has a matrix in the loop. It also appears to solve the loop once then get stuck the second time round. this is my code: the error is at matrix A, any help would be greatly appreciated, thanks.

phi=1:1:89;
for i=1:length(phi)

m=cosd(phi(1:i));
l=sind(phi(1:i));
%the following calculates k tilde ratio for use in solving of equations
r= U1/U2; %velocity ratio
Kratio = ((r*(mach2/(1-mach2^2)))*(-m*mach2 + l*sqrt((m.^2/l.^2)-(((1/r)^2)*
((1/mach2^2)-1)))));
alpha = (A2^2/(gamma*U1^2))*(Kratio/(m-(Kratio*(1/r))));
beta= (A2^2/(gamma*U1^2))*(l/(m-(Kratio*(1/r))));

%from boundary conditions 
B1= (((gamma-1)*(mach1^2))-2)/((gamma+1)*(mach1^2));
B2= 2/((gamma+1)*mach1^2);
C1=4/(((gamma-1)*mach1^2)+2);
C2= -(((gamma-1)*mach1^2)+4)/(((gamma-1)*mach1^2)+2);
D1= (4*gamma*mach1^2)/((2*gamma*mach1^2)-(gamma-1));
D2=-((2*gamma*mach1^2)/((2*gamma*mach1^2)-(gamma-1)));
E1= (2*(mach1^2-1))/((gamma+1)*mach1^2);

%matrix to be solved for unknown coefficients

matrixA= [1 0 0 -alpha 0 0 0; 0 ((m*r)) 0 0 l 0 0; 0 0 1 -beta 0 0 0; 1 1 0 0 0     
(B1-1) 0; 0 0 0 1/gamma 0 C1 1; 0 0 0 1 0 D1 0; 0 0 1 0 1 ((E1*l)/m) 0];-->error here

matrixB= [0 ; 0 ; 0 ; -B2*Ae+B1*l*Av ; C1*l*Av-C2*Ae ; D1*l*Av-D2*Ae; -m*Av];

format long
coefficients= matrixA\matrixB;

i=sqrt(-1);
t=0;

F=coefficients(1,1);
G=coefficients(2,1);
H=coefficients(3,1);
K=coefficients(4,1);
I=coefficients(5,1);
L=coefficients(6,1);
Q=coefficients(7,1);

% assumes k=1 for equation Kratio,t=0 for this case (test)
%N=201;
%x=zeros(N);
%for j=1:N
x=20;
x1=-20;
%end
%for j=1:N
y=0;
y2=0;



%upstream and downstream conditions
%plots the graph for fixed t
% k has been set to 2
 %entropy modes




%vorticity modes

%plot for upstream KE 
u1prime= l*Av*exp(k*i*(m*x1+l*y2-U1*m*t));
v1prime= -m*Av*exp(k*i*(m*x1+l*y2-U1*m*t));
KE1= u1prime.*conj(u1prime)+v1prime.*conj(v1prime);


%plot for downstream KE (kinetic engery)
u2prime2=F*exp(k*i*Kratio*x+k*i*l*y-k*i*U1*m*t)+G*exp((k*i*(m*r*x+l*y-U1*m*t)));
v2prime2=H*exp(k*i*Kratio*x+k*i*l*y-k*i*U1*m*t)+I*exp((k*i*(m*r*x+l*y-U1*m*t)));
KE=u2prime2.*conj(u2prime2)+v2prime2.*conj(v2prime2);

KEnorm=(KE)/KE1


end

Your problem arises from (m*r) (and another one will arise with l in matrixB ).
m and l grow inside the loop:

m=cosd(phi(1:i));  
l=sind(phi(1:i));

It's up to you to figure out which portion of m and l you want to use after the first iteration.
As Dan pointed out, the route to debug this is to set a breakpoint in the line that gives the error. Once you get to it, check all the variables in your matrix for their size.


One last comment: I had to assume the following variables:

U1 = 1.0;
U2 = 0.5;
mach2 = 0.3;
mach1 = 0.5;
A2 = 0.6;
gamma = 1.4;
Ae = 1;
Av = 1;
k = 1;

Next time, please consider providing a minimal working example, that upon copy/paste will regenerate your problem.


EDIT
If you attempt to fix your code, consider the following steps:

  1. As Divakar suggested, do not use i as the loop counter, when you need it as the imaginary unit. Therefore, replace all instances of the loop counter with ii (take your time doing this, you do not want to overlook one instance).
  2. Next, you have two choices:

    • either , replace m and l with this:

       m=cosd(phi(ii)); l=sind(phi(ii)); 

      that way, both m and l will remain scalars

    • or , replace every instance of m and l with m(ii) and l(ii) respectively

    whichever you do, be consistent.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM