Triangular pulse with function handle t(@)

7 views (last 30 days)
Hi! I have a rectangular pulse thas is given by this function handle: @(t)( (t>=0).*(t<pulseDuration) )
And I need to change this function handle to create a triangular pulse. Any ideas?
  14 Comments
Bjorn Gustavsson
Bjorn Gustavsson on 3 Nov 2020
This is a task that is so much easier to handle in a .m-file where you can write a function using all of matlab's programming capabilities: conditionals (if-elseif-else-end), loops (for and while-loops) and on and on. The definition of dynamical/anonymous functions is way trickier, and doing this with multiple cases easily becomes a mess of parenthesises and operator precedences bungled up. If we take a look at your first function
ft1 = @(t)((t>=0).*(t<(pulseDuration)).*(t/(pulseDuration)));
You first define the lower and upper time-limits of the up-slope segment and then the shape of the function in that range.
If we modify that to just be half the length we get something like below:
ft2a = @(t)(t>0).*(t<pulseDuration/2).*(t/(pulseDuration/2));
Just a modified upper limit and a modified linear segment.
You are allowed to add multiple such segments together, if you just define their different ranges in time to give you what you want. So if we do that and add another segment that doesn't overlap you get something like this:
ft2b = @(t)(t>0).*(t<pulseDuration/2).*(t/(pulseDuration/2)) + (t>=pulseDuration/2).*(t<pulseDuration).*(some-line-equation);
I'll let you doodle-out what your linear equation in the second term should be, and since your pulseDuration is already defined you're allowed to use it as much as you need in the function. If you want you can also use pulseDuration as a second input argument to the function which make the function more adaptable to triangular pulses of different widths.
Laura Ferrer Pascual
Laura Ferrer Pascual on 3 Nov 2020
Thank you very much! I finally did it !!

Sign in to comment.

Accepted Answer

James Tursa
James Tursa on 3 Nov 2020
Edited: James Tursa on 3 Nov 2020
If you must use a one-liner function handle, the general form could be this:
f = @(t) (t >= 0).*(t < pulseDuration/2).*(expression1) + (t >= pulseDuration/2).*(t < pulseDuration).*(expression2)
You would need to fill in the two places I have marked as expression1 and expression2. The general idea is to have this done in pieces, with each piece having the form ( ).*( ).*(expression). The first two ( ).*( ) simply generates a logical array that has 1's where t is valid for the associated expression. You can do this for as many different segments as needed ... in your case two.
The first expression1 would be a line segment that starts at (0,0) and goes to (pulseDuration/2,1).
The second expression2 would be a line segment that starts at (pulseDuration/2,1) and goes to (pulseDuration,0).
See if you can figure out what these two different expressions should be. Both should be expressions involving t and pulseDuration. You might simply draw these line segments out on a piece of paper and then use standard techniques to figure out the expressions. Or you could Google how to generate the expression for a line given two points on the line and do this once for each segment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!