Not sure how to fix my matrix dimension problem
조회 수: 1 (최근 30일)
이전 댓글 표시
t=0:0.1:4
m=5;
k=1000;
zeta=[0, 0.1, 0.25, 0.5, 0.75, 0.9, 1];
xo=0.05;
vo=0;
w=sqrt(k/m)
wd=w*sqrt(1-zeta.^(2))
A=(sqrt((vo+zeta.*w.*xo).^(2)+(wd.*xo).^(2))).*(1./wd)
o=atan((xo*wd).*(1./(vo+zeta*w*xo)))
figure(1)
for i=1:length(zeta)
x=A.*exp(-w*zeta.*t).*sin(wd.*t+o);
plot(t,x)
hold on
end
댓글 수: 0
답변 (2개)
Adam Danz
2020년 3월 29일
편집: Adam Danz
2020년 3월 29일
t is a 1x41 vector; wd and zeta are both 1x7 vectors. You can't do pairwise multiplication with two arrays of different size.
Base on the line plot(t,x) I assume you are expecting t to have the same number of elements as x in which case t will also need to be a 1x7 vector.
Perhaps you're looking for
t = linspace(0,4,numel(zeta));
or
t = cumsum([0, 0.1 * ones(1,numel(zeta)-1)]);
But there's another problem/mystery: you're not using the i variable within your loop and the values for x will never change within the loop so you'll end up plotting the same line over and over again.
댓글 수: 2
MaryD
2020년 3월 29일
You are using for loop with variable i but you are not using i inside the loop. I'm not sure what you want to achive but maybe this will work
for i=1:length(zeta)
x=A(i).*exp(-w*zeta(i).*t).*sin(wd(i).*t+o(i));
figure(i)
plot(t,x)
hold on
end
댓글 수: 3
Adam Danz
2020년 3월 29일
편집: Adam Danz
2020년 3월 29일
If you're plotting all of that on the same figure, move figure(1) and hold on outside of the loop (see my previous comment). Also, move title(), xlabel(), and ylabel() outside of the loop (before or after).
I also recommend you label the lines so you know which one is which.
for i = _____
x = ______
plot(t,x, 'DisplayName', num2str(i))
end
legend()
참고 항목
카테고리
Help Center 및 File Exchange에서 Line Plots에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!