Clear Filters
Clear Filters

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

1 view (last 30 days)
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);

Accepted Answer

Image Analyst
Image Analyst on 24 Sep 2017
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
Mariela Flores
Mariela Flores on 24 Sep 2017
Edited: Mariela Flores on 24 Sep 2017
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 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.

Sign in to comment.

More Answers (0)

Categories

Find more on MATLAB 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!