필터 지우기
필터 지우기

??? 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);

채택된 답변

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 CenterFile Exchange에서 MATLAB에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by