简体   繁体   English

在MatLab中使用vertcat时出错

[英]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. 我在for循环中收到一个vertcat错误,我一直在寻找解决方案,但我似乎找不到在循环中具有矩阵的解决方案。 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. 这是我的代码:错误在矩阵A上,任何帮助将不胜感激,谢谢。

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*r)引起的(m*r) matrixB l会引起另一个问题)。
m and l grow inside the loop: ml在循环内增长:

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. 由您决定在第一次迭代后要使用ml哪一部分。
As Dan pointed out, the route to debug this is to set a breakpoint in the line that gives the error. 正如Dan所指出的,调试此错误的方法是在产生错误的行中设置一个breakpoint 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. 正如Divakar所建议的,当需要i作为虚数单位时,请勿将其用作循环计数器。 Therefore, replace all instances of the loop counter with ii (take your time doing this, you do not want to overlook one instance). 因此,将循环计数器的所有实例替换为ii (花点时间这样做,您不想忽略一个实例)。
  2. Next, you have two choices: 接下来,您有两个选择:

    • either , replace m and l with this: 要么 ,将ml替换为:

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

      that way, both m and l will remain scalars 这样, ml都将保持标量

    • or , replace every instance of m and l with m(ii) and l(ii) respectively ,分别用m(ii)l(ii)替换ml每个实例

    whichever you do, be consistent. 无论您做什么,都要保持一致。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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