Asked to plot using vectors of unequal sizes

1 view (last 30 days)
I am working on the following problem:
========================================
We wish to examine the motion of a damped harmonic oscillator. The small amplitude oscillation of a unit mass attached to a spring is given by the formula y = e−(R/2)t sin(ω1t ), where ω2 1 = ω2 o − R2/4 is the square of the natural frequency of the oscillation with damping (i.e., with resistance to motion); ω2 o = k is the square of the natural frequency of undamped oscillation; k is the spring constant; and R is the damping coefficient. Consider k = 1 and vary R from 0 to 2 in increments of 0.5. Plot y versus t for t from 0 to 10 in increments of 0.1.
========================================
My main problem with this problem is that I am told to evaluate y for R=0:0.5:2, but plot over the range of t=0:0.1:10. How can this work, if y is 1x5 and t is 1x101?
Here is my code, minus the line for t, which I make the same as R to get y, but then try to change when going to plot (doesn't work):
========================================
k= 1; %spring constant
R=[0:0.5:2]; %damping coeff
w0=sqrt(k);
w1=sqrt((w0^2)-R.^2/4);
y=exp(-(R./2).*t).*(sin(w1.*t));
plot(t,y)

Accepted Answer

Mischa Kim
Mischa Kim on 10 Feb 2014
Edited: Mischa Kim on 10 Feb 2014
B.M., simply compute y in a for-loop and plot the results in the same figure using hold all. You also might want to include markers and a legend for readability. Use colors and markers to specify the color and marker type for each of the plots.
k = 1; %spring constant
R = 0:0.5:2; %damping coeff
t = 0:0.1:10;
w0 = sqrt(k);
w1 = sqrt((w0^2)-R.^2/4);
figure
hold all; grid; box
colors = 'krbcy';
markers = 'x+*.o';
for ii = 1:length(R)
y(ii,:) = exp(-(R(ii)/2).*t).*(sin(w1(ii)*t));
plot(t,y(ii,:),[markers(ii) colors(ii)]);
end
xlabel('t in s')
ylabel('y in TBD')
legend('R0','R1','R2','R3','R4')
  1 Comment
B.M.
B.M. on 10 Feb 2014
Thanks Mischa. The book I'm using hasn't gone into much detail about plotting yet so I figured there was some more complicated way to do it...just wish I could figure out what they were expecting given the simple discussion so far.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!