Plotting a function with different conditions

조회 수: 3 (최근 30일)
wiig krist
wiig krist 2014년 4월 2일
댓글: wiig krist 2014년 4월 2일
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)
wiig krist
wiig krist 2014년 4월 2일
Ok I got it , thanks

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

카테고리

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