Asked to plot using vectors of unequal sizes

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

采纳的回答

Mischa Kim
Mischa Kim 2014-2-10
编辑:Mischa Kim 2014-2-10
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 个评论
B.M.
B.M. 2014-2-10
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.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Legend 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by