plotting a graph of time vs gear...updated

조회 수: 3 (최근 30일)
Joseph
Joseph 2014년 2월 13일
댓글: Joseph 2014년 2월 13일
based on V=1.5*t^2
a car has an auto-transmission that selects gear based on speed values.
Gear1: >=0 and < 3m/s
Gear2: >=3 and < 8 m/s
Gear3:>=8 and < 15 m/s
and so on...need to graph this and not sure where to start. Thank you
here's what I have...The first graph works perfect...its the second one im stuck on...thanks
t=1:1:6;
V=1.5*t.^2;
plot(t,V)
grid on % adds grid lines
xlabel('Time(s)') % adds x label
ylabel('Speed(m/s)') % ADDS Y LABEL
title('Speed of a Car') % adds Title
t=1:.1:6;
V=(1.5)*t.^2;
if (V>= 0 & V < 3)
Gear=1;
elseif (V>=3 & V < 8)
Gear=2;
elseif (V>= 8 & V < 15)
Gear=3;
elseif (V>= 15 & V< 25)
Gear=4;
else
Gear=5;
end
figure % opens 2nd graph
plot(t,Gear)
grid on
xlabel('Time(s)')
ylabel('Gear')
title('Gear Selection')

채택된 답변

Matt Tearle
Matt Tearle 2014년 2월 13일
If I interpret this correctly, you're trying to see which gear you're in at time t? So you need to calculate v from t, then g (= 1, 2, or 3) from v. The neatest way to do that is to use histc for binning:
t = linspace(0,5);
v = 1.5*t.^2;
[~,g] = histc(v,[3 8 15 Inf]);
subplot(2,1,1)
plot(t,v)
subplot(2,1,2)
plot(t,g)
Note: if this happens to be a homework problem, do not hand this in! It is almost certainly not what the expected answer will be, and will be a pretty major flag that you didn't do it yourself :) Instead, let me know, and I'll give you pointers in the "right" direction...
  댓글 수: 3
Matt Tearle
Matt Tearle 2014년 2월 13일
Your problem is that Gear is a scalar value. Your logical conditions are doing something hairy and unexpected because V is a vector, so V < 3 is a logical array, not a scalar condition. However, when used in an if statement, MATLAB goes ahead and decides to do something anyway (something that I doubt you actually want it to do). But in the end, it's making a single logical choice, and producing a single value for Gear.
What you need is that for each value of V, to determine the corresponding value for Gear (which, at the end, should be a vector the same size as V).
There are a few different ways to do that. The neatest, IMO, is the one I showed with histc.
Joseph
Joseph 2014년 2월 13일
That worked great...I posted another question a couple hours ago about a conversion program...any help with that would be appreciated

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 2-D and 3-D Plots에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by