??? In an assignment A(I) = B, the number of elements in B and I must be the same.
1 view (last 30 days)
Show older comments
Mariela Flores
on 24 Sep 2017
Commented: Image Analyst
on 24 Sep 2017
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 Comments
Accepted Answer
Image Analyst
on 24 Sep 2017
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 Comments
Image Analyst
on 24 Sep 2017
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.
More Answers (0)
See Also
Categories
Find more on Entering Commands in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!