[英]My code runs then sometimes it has error “Error using horzcat Dimensions of matrices being concatenated are not consistent.”
This is my code and I understand you have to have dimensions of matrix equal to line up which is why I transpose y to make it a column and both have 21 rows but sometimes the code runs when I publish but then after one time of running it persistently then hits this error if I try to run it again. 这是我的代码,我理解您必须具有等于矩阵的矩阵尺寸,这就是为什么我将y转置为一列并且都具有21行的原因,但是有时代码在我发布时运行,但是在运行了一次之后如果我尝试再次运行它,就会持续出现此错误。 It says error on line disp([t,y]) 它说在线disp([t,y])上的错误
%Define parameters
A=1250; %m^2
Q=450; %m^3/d
a=150;
y(1)=0;
t=[0:0.5:10]';
y=y';
disp(' t y')
disp('----------------------')
disp([t,y])
%Set loop in (Eulers method) for the amount of steps we want to do to
%find our estimated solution
for i=1:20
y(i+1)=y(i)+((3*(Q/A)*sin(t(i)).^2)-(a*(1+y(i)).^1.5/A))*0.5;
end
%generate plot for data
plot(t,y)
title('Plot of y vs t')
xlabel('time')
ylabel('y')
Your problem is that y
only has one element when you attempt to display it. 您的问题是,当您尝试显示y
时,它只有一个元素。
You're constructing [t,y]
which is a concatenation of two elements - t
and y
. 您正在构造[t,y]
,它是两个元素t
和y
的串联。 But t
is a vector of some length (21), whereas you have not initialized y
to be anything. 但是t
是某个长度的向量(21),而您尚未将y
初始化为任何东西。
If you really do want to display y
before it's been set, you can initialize it to zero using y(1:21) = 0 如果您确实想在设置之前显示y
,则可以使用y(1:21)= 0将其初始化为零。
But why do you want to display y
and t
before you've calculated your estimated solution, anyway? 但是,无论如何,为什么要在计算估计解之前显示y
和t
?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.