[英]Please Help: In an assignment A(I) = B, the number of elements in B and I must be the same. MATLAB
I was asked in an assignment to use Euler's method to determine the values of t and y from t=0:1000. 要求我在一次作业中使用欧拉方法从t = 0:1000确定t和y的值。 I have all the basic code and parameters down but when i put my Euler's equation in I get the error code 我记下了所有基本代码和参数,但是当我将欧拉方程放进去时,我得到了错误代码
In an assignment A(I) = B, the number of elements in B and I must be the same. 在分配A(I)= B中,B和I中的元素数必须相同。
Error in Project1 (line 24) Ay(i+1) = Ay(i) + (dAy)*x; Project1(第24行)中的错误Ay(i + 1)= Ay(i)+(dAy)* x;
How could I change these variables between vectors and scalars to allow the equation to run? 如何在矢量和标量之间更改这些变量以允许方程运行? My full code can be found below: 我的完整代码可以在下面找到:
dt=x;
Ay=zeros(1,1001);
Ay0=1250;
Ay(1) = Ay0;
t=0;
y=0;
t=0:dt:1000;
for i=1:1000
if y > 10
Qout=3*(y-10).^1.5;
else
Qout=0;
end
Qin=1350*sin(t).^2;
dAy=Qin-Qout;
Ay(i+1) = Ay(i) + dAy*dt;
end
plot(t,y);
The problem lies in the line of your code: 问题出在代码行中:
Ay(i+1) = Ay(i) + dAy*dt;
dAy*dt returns a vector. dAy * dt返回一个向量。
When you add it to Ay(i) you still end up with a vector. 当您将其添加到Ay(i)时,仍然会得到一个向量。
Ay(i+1) is a SINGLE element within a vector. Ay(i + 1)是向量中的单个元素。
You Cannot assign a vector quantity to an element within a vector. 您不能将向量数量分配给向量中的元素。
The issue is that your variable "Qin" is not a number it is a vector containing sin values of the whole vector t. 问题是您的变量“ Qin”不是数字,它是一个包含整个向量t的sin值的向量。 Similarly your "dAy" is also a vector. 同样,您的“ dAy”也是一个向量。 Hence it cannot be stored in a variable Ay. 因此,它不能存储在变量Ay中。
if your dt =x = 1, just replace sin(t) with sin(i) ie replace 如果您的dt = x = 1,只需将sin(t)替换为sin(i),即替换
Qin=1350*sin(t).^2; Qin = 1350 * sin(t)。^ 2; by Qin=1350*sin(i).^2; 由Qin = 1350 * sin(i)。^ 2;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.