??? In an assignment A(I) = B, the number of elements in B and I must be the same.
1 回表示 (過去 30 日間)
古いコメントを表示
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);
0 件のコメント
採用された回答
Image Analyst
2017 年 9 月 24 日
The FAQ explains it completely: http://matlab.wikia.com/wiki/FAQ#How_do_I_fix_the_error_.22Subscript_indices_must_either_be_real_positive_integers_or_logicals..22.3F
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 件のコメント
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 Exchange で Entering Commands についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!