I need help graphing a piece wise function in Matlab, the function is
y(t) = y1=-3t^2+5 when t>=0 and y2=3t^2+5 when t<0
with t in [-9,9]
I have tried many different ways to graph this, and I honestly can't figure it out. Any help would be greatly appreciated!

 채택된 답변

Adam
Adam 2017년 2월 2일
편집: Adam 2017년 2월 2일

0 개 추천

It is very simple when you think about it in components. You can create y piece-wise and then plot a graph as normal.
e.g.
t = -9:9; % Or whatever granularity you want for t
y( t >= 0 ) = -3*t( t >= 0 ).^2 + 5;
y( t < 0 ) = 3*t( t < 0 ).^2 + 5;

댓글 수: 5

Leesy
Leesy 2017년 2월 6일
I'm confused on the "% Or whatever granularity you want for t" part... Like i'm not sure what that means. I am very new to Matlab. Also shouldn't there be a "plot" code in there somewhere? Is there anyway you could show me a complete code? Thank you so much!
Do you want to graph at only every 1 second, or every 1/2 second, or every 1/10 second? The "granularity" is the step size
step_size = 0.25; %1/4 second
t = -9 : step_size : 9;
plotting:
plot(t, y)
Leesy
Leesy 2017년 2월 6일
Ah, i get it. If the first line were "t=-9:0.5:9" would that work? Or does it have to be in that other format?
-9:0.5:9 would be fine. The two-line version was just to make it easier to how the parts went together.
t = linspace( -9, 9, 100 )
works fine too if you want to insist on 100 values rather than defining a step. Basically you can create your t vector any way you like. It doesn't even have to have regular steps if you don't want.

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

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Startup and Shutdown에 대해 자세히 알아보기

질문:

2017년 2월 2일

댓글:

2017년 2월 6일

Community Treasure Hunt

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

Start Hunting!

Translated by