??? In an assignment A(I) = B, the number of elements in B and I must be the same.

1 回表示 (過去 30 日間)
Mariela Flores
Mariela Flores 2017 年 9 月 24 日
コメント済み: Image Analyst 2017 年 9 月 24 日
Hi, I don't get why my code is incorrect. I hope you guys can help please.
clc;
A=10;
m=1;
k=40;
c=10;
xi=c/(2*sqrt(k*m));
w=1;
wn=sqrt(k/m);
b=(1-(w/wn)^2);
d=(2*xi*w/wn)^2;
Z=A/(b^2+d)^1/2;
fi=atan((2*xi*w/wn)/(1-(w/wn)^2));
muestras=1000;
t0=0;
tf=15;
for i=1:muestras+1
t(i)=t0+(i-1)*(tf-t0)/muestras;
x(i)=Z*sin(w*t-fi);
end
plot(t,x,'g')
xlabel('t')
ylabel('x')
In the command window, it says:
??? In an assignment A(I) = B, the number of elements in B and
I must be the same.
Error in ==> AVER at 19
x(i)=Z*sin(w*t-fi);

採用された回答

Image Analyst
Image Analyst 2017 年 9 月 24 日
But in short, since t is a vector of lots of values, then so is the result of sin(), and this means that you are trying to put a bunch of values into a single element of x, and you can't do that.
Try using t(i), which is only one single value, instead of the whole t array, which has "i" elements in it:
x(i) = Z * sin(w * t(i) - fi);
  2 件のコメント
Mariela Flores
Mariela Flores 2017 年 9 月 24 日
編集済み: Mariela Flores 2017 年 9 月 24 日
I really appreciate your help, thank you so much, that was the error in my code. If now, the program says that there is an error in plot, will you think it is because I do not including the (i) with the variables?
Image Analyst
Image Analyst 2017 年 9 月 24 日
The code with my fix works perfectly fine and runs with no errors:
clc;
A=10;
m=1;
k=40;
c=10;
xi=c/(2*sqrt(k*m));
w=1;
wn=sqrt(k/m);
b=(1-(w/wn)^2);
d=(2*xi*w/wn)^2;
Z=A/(b^2+d)^1/2;
fi=atan((2*xi*w/wn)/(1-(w/wn)^2));
muestras=1000;
t0=0;
tf=15;
for i=1:muestras+1
t(i)=t0+(i-1)*(tf-t0)/muestras;
x(i)=Z*sin(w*t(i)-fi);
end
plot(t,x,'g')
xlabel('t')
ylabel('x')
Show your code so I can see what you did differently.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeEntering Commands についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by