Not sure how to fix my matrix dimension problem

조회 수: 2 (최근 30일)
Erik Sharrer
Erik Sharrer 2020년 3월 29일
편집: Adam Danz 2020년 3월 29일
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

답변 (2개)

Adam Danz
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
Erik Sharrer
Erik Sharrer 2020년 3월 29일
If I use linspace or cumsum then the step size is to large. What would be the best apporach in fixing that and the i variable?
Adam Danz
Adam Danz 2020년 3월 29일
편집: Adam Danz 2020년 3월 29일
The cumsum() results in the same step size as your original t vector but extends to a 0.6 instead of 4.
The linspace() results in values between 0 and 4 just like your original t vector but with different step sizes.

댓글을 달려면 로그인하십시오.


MaryD
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
Erik Sharrer
Erik Sharrer 2020년 3월 29일
Helped a lot thanks! Ended up doing this and I got what I needed.
for i=1:length(zeta)
x=A(i).*exp(-w*zeta(i).*t).*sin(wd(i).*t+o(i));
figure(1)
plot(t,x);
hold on
title('Displacement vs time')
xlabel('time (s)')
ylabel('displacement (m)')
end
Adam Danz
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 CenterFile Exchange에서 Line Plots에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by