Not sure how to fix my matrix dimension problem

3 次查看(过去 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

回答(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