Plotting a function with different conditions
Show older comments
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);
Answers (1)
Joseph Cheng
on 2 Apr 2014
1 vote
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 Comments
wiig krist
on 2 Apr 2014
Joseph Cheng
on 2 Apr 2014
Edited: Joseph Cheng
on 2 Apr 2014
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
on 2 Apr 2014
Categories
Find more on Creating and Concatenating Matrices in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!