Not sure how to fix my matrix dimension problem

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

Answers (2)

Adam Danz
Adam Danz on 29 Mar 2020
Edited: Adam Danz on 29 Mar 2020
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 Comments
Erik Sharrer
Erik Sharrer on 29 Mar 2020
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 on 29 Mar 2020
Edited: Adam Danz on 29 Mar 2020
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.

Sign in to comment.


MaryD
MaryD on 29 Mar 2020
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 Comments
Erik Sharrer
Erik Sharrer on 29 Mar 2020
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 on 29 Mar 2020
Edited: Adam Danz on 29 Mar 2020
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()

Sign in to comment.

Categories

Find more on Resizing and Reshaping Matrices 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!