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

Community Tip - You can Bookmark boards, posts or articles that you'd like to access again easily! X

Piecewise function plot

Sergey
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
Werner_E
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:

Werner_E_0-1588722526213.png

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

View solution in original post

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

answer to Sergei.jpg

Sergey
14-Alexandrite
(To:-MFra-)

Thank you.

 

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

 

Sergey

 

LucMeekes
23-Emerald III
(To:Sergey)

Just add them.

LucMeekes_0-1588694510993.png

Success!
Luc

Fred_Kohlhepp
23-Emerald I
(To:Sergey)

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

FredKohlhepp_0-1588697482679.png

 

-MFra-
21-Topaz II
(To:Sergey)

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

answer to Sergei 1.jpg

Werner_E
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:

Werner_E_0-1588722526213.png

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

Sergey
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

StuartBruff
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 ...

 

2020 05 09 B.png

 

(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.

 

LucMeekes
23-Emerald III
(To:Sergey)

One more alternative":

LucMeekes_0-1589181472144.png

Success!
Luc

Top Tags