cancel
Showing results for
Did you mean:
cancel
Showing results for
Did you mean:

Community Tip - Have a PTC product question you need answered fast? Chances are someone has asked it before. Learn about the community search. X

14-Alexandrite

## Piecewise function plot

Hello!

I am trying to plot the picewise function (attached).

Do I need to use "if" statement here or are there any other options?

I think that it should be something like this: if 0<x<0.3 then y=2, if 0.3<x<0.8 then y=-((x-0.3)/0.5)+2, if 0.8<x<1 then y=1.

Would anybody, please, help me with this issue?

Best regards,

Sergey

1 ACCEPTED SOLUTION

Accepted Solutions
24-Ruby V
(To:Sergey)

You have already been shown the variant using the if-function and also the variant using the Heaviside step function. Both look a little confusing in my opinion. The only advantage of the Heaviside variant is that you can apply symbolic operation (e.g. derivative or integral) to it.
For completeness sake here is the variant using the programmed if-statement:

BTW, there is no need to define any vectors for x and y. Just plot the function and set the axis limits directly at the axis labeling (you can change the first, second and last value). I set the lowest value on the x-axis to 0.1 to match the picture and also dragged the y-axis to the right accordingly.

P6-worksheet attached

9 REPLIES 9
21-Topaz II
(To:Sergey)

14-Alexandrite
(To:-MFra-)

Thank you.

But how do I get y=1 and y=2 in the same graph?

Sergey

23-Emerald III
(To:Sergey)

Success!
Luc

23-Emerald I
(To:Sergey)

The if statement is  one good way.  You can also use Boolean comparison multipliers.

21-Topaz II
(To:Sergey)

To draw the graph, you can use the step function as shown here:

24-Ruby V
(To:Sergey)

You have already been shown the variant using the if-function and also the variant using the Heaviside step function. Both look a little confusing in my opinion. The only advantage of the Heaviside variant is that you can apply symbolic operation (e.g. derivative or integral) to it.
For completeness sake here is the variant using the programmed if-statement:

BTW, there is no need to define any vectors for x and y. Just plot the function and set the axis limits directly at the axis labeling (you can change the first, second and last value). I set the lowest value on the x-axis to 0.1 to match the picture and also dragged the y-axis to the right accordingly.

P6-worksheet attached

14-Alexandrite
(To:Werner_E)

Thank everyone for the feedbacks, I appreciate your help.

All solutions you gave me are relevant and I will try each one.

Best regards,

Sergey

23-Emerald II
(To:Werner_E)

I find a problem with the if-elseif-else construct is that it takes up far too much vertical space for my liking.  I've suggested (several times, back when Dimetrodon ruled the land) that a case statement and/or standard mathematical piecewise notation would be a preferable implementation.

A few rough examples ...

(I'm not a fan of the double-vertical line at each indentation level, either - it's far too obtrusive and many programming languages manage just fine with indentation as a structural feature (eg, Python and Haskell))

Example 6 is in voluntary lockdown.

Examples 3 and 4 aren't necessarily more vertically compact than a series of if-elseif expressions but (IMO)  they look more consistent and require less thought about what they're actually doing.

Stuart

I also have a worksheet that used a 2-column array (first column = condition, 2nd column = associated expression), but it's in M11 and, only having Prime Express at the moment, I can't open it.

23-Emerald III
(To:Sergey)

One more alternative":

Success!
Luc

Announcements
Top Tags