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 Center および File ExchangeGrid 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