Plotting a function with different conditions

3 次查看(过去 30 天)
am trying to build and plot a Triangular function with an amplitude of 1 , starting in second 1, reaching the max in second 3 and going back to zero in second 4,
x(A,t1,t2,t3,t) =
  • A/t2-t1 *(t-t1), t1<=t<=t2
  • A/t2-t3 *(t-t3),t2<=t< t3
  • 0, elsewhere
the problem is that I can't find a way to add the two conditions to the function ,even with one condition I get only a straight line
here is what I got so far, please give me an idea to how to add them both
the script :
fs = 20; %freq
t = 0:1/fs:5;
t1=1; t2=3; t3=4;
A=1; %amplitude
x2 = mytri(A,t1,t2,t3,t);
plot (t,x2,'.-')
axis([ -2 5 -2 5])
the function
function x2 = mytri(A,t1,t2,t3,t)
x2=A/t2-t1*t-t1*(t1<=t<=t2);

回答(1 个)

Joseph Cheng
Joseph Cheng 2014-4-2
Look at each portion of your mytri() function. (side note your x2 equation doesn't match the first condition for x (you're missing a t1^2. as t1*(t-t1)=t1*t-t1^2))
you can use the find() function to find when t1<=t and t<=t2. or use the x(t1<=t and t<=t2) to satisfy your conditions.
  3 个评论
Joseph Cheng
Joseph Cheng 2014-4-2
编辑:Joseph Cheng 2014-4-2
yes what i suggested will get you to do your conditions for the sawtooth. next the condition
A/t2-t1 *(t-t1), t1<=t<=t2
means the equation when expanded for when t is between t1 and t2 is:
A/t2 - t1*t-t1*t1
what you have written in your function is:
A/t2 - t1*t-t1*(values of t between t1 and t2)
you need to follow the orders of operations as A*(B-C) is A*B-A*C and NOT A*B-C*(some subset of B or B itself)

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Grid Lines, Tick Values, and Labels 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by