Plotting functions over certain intervals?

조회 수: 10 (최근 30일)
Nicholas Ross
Nicholas Ross 2021년 2월 17일
댓글: Nicholas Ross 2021년 2월 17일
Hey all,
I'm attempting to write a program where I plot 3 different functions (x1, x2, x3) for three different time intervals.
For -4 < t < -2 : x1 = (t.^2) - (2*t) + 3
For -2 < t < 2 : x2 = 4cos((2*pi*t) - (pi/8)) + 3sin(2*pi*t)
For 2 < t < 4 : x3 = sinc(t)
When I try to implement my code I get straight lines in my plot which I know aren't correct. I'm struggling with how to set 't' equal to a range of values between two numbers not including those two numbers (ie -4 < t < -2). I know this is fairly simple if 't' was => -4 and =< -2 (ie t = -4:0.1:-2) but I need the starting and ending values to be excluded I'm attempting to do this without any if statements as well. Below is my code:
t = -4:0.1:4;
t1 = (t>-4) & (t<-2);
t2 = (t>-2) & (t<2);
t3 = (t>2) & (t<4);
x1 = (t1.^2) - (2*t1) + 3
x2 = 4*cos((2*pi*t2) - (pi/8)) + 3*sin(2*pi*t2)
x3 = sinc(t3);
plot(t1,x1,t2,x2,t3,x3)

채택된 답변

Walter Roberson
Walter Roberson 2021년 2월 17일
t = -4:0.1:4;
t1 = (t>-4) & (t<-2);
That code is not storing the times in the given range into t1: it is creating a logical vector (0 and 1) the same length as t that tells you if the corresponding t is in range.
t1=t(t>-4&t<-2);
  댓글 수: 3
Walter Roberson
Walter Roberson 2021년 2월 17일
No, your original code had t1 be 0 (false) and 1 (true). It would not be the locations (indices) or values of the times that matched. t1(K) would have been true if t(K) was in that range.
The code I suggested instead selects the content that are in the right range. After t1 would list only the times in that range and would not need to be tested more.
Nicholas Ross
Nicholas Ross 2021년 2월 17일
okay that makes sense. Thank you

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 MATLAB에 대해 자세히 알아보기

제품


릴리스

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by