Plotting functions over certain intervals?

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

请先登录,再进行评论。

更多回答(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