Message Boards Message Boards

[✓] Plot a piecewise function over an interval?

GROUPS:

Consider the following code:

piecewise[{{[//function:-t^2//],[//range:-2<t<=0//]}, {[//function:t^2//],[//range:0<=t<2//]}}]

How would I plot this piecewise function over the interval (-10 < t < 10)?

POSTED BY: Stuart Symes
Answer
12 days ago

.

Show[Plot[Piecewise[{{-t^2, -2<t<=0}, {t^2, 0<=t<2}}], {t,-2,2}], PlotRange->{{-10,10},{-4,4}}]

enter image description here

POSTED BY: Bill Simpson
Answer
12 days ago

You don't need Show[] function:

Plot[Piecewise[{{-t^2, -2 < t <= 0}, {t^2, 0 <= t < 2}}], {t, -2, 2}, PlotRange -> {{-5, 5}, {-4, 4}}]

enter image description here

POSTED BY: Valeriu Ungureanu
Answer
12 days ago

If this is a periodic function defined on its period by:

Piecewise[{{-t^2, -2 < t <= 0}, {t^2, 0 <= t < 2}}]

How would I plot this waveform over the interval (-10 < t < 10)?

POSTED BY: Stuart Symes
Answer
12 days ago

@Stuart Symes, welcome to Wolfram Community! Please make sure you know the rules: https://wolfr.am/READ-1ST

The syntax you used in your top post is NOT Wolfram Language. This forum permits only subjects related to Wolfram Technologies.

Please consult Documentation and existing resources:

POSTED BY: Moderation Team
Answer
12 days ago

There are lots of ways of making this periodic:

Plot[Piecewise[{{-Mod[-t, 2]^2, -2 < -Mod[t - 2, 4] <= 0}, {Mod[t, 2]^2, 0 <= Mod[t - 2, 2] < 2}}], {t, -10, 10}, PlotRange -> {{-10, 10}, {-4, 4}}]

That one is not the nicest one... the definitions of the intervals is not clean.

This one might work, too:

Plot[Sign[#]*#^2 &[4*SawtoothWave[0.25 (x + 2)] - 2], {x, -10, 10}, PlotRange -> {All, {-4, 4}}]

Cheers,

Marco

PS: You might want to have a look at this:

http://community.wolfram.com/groups/-/m/t/156025

or this

https://mathematica.stackexchange.com/questions/105959/changing-a-piecewise-function-into-a-periodic-function

This one's nice, too:

https://mathematica.stackexchange.com/questions/32689/how-to-plot-periodic-functions-graphic

They define:

myperiodic[func_, {val_Symbol, min_?NumericQ, max_?NumericQ}] := func /. (val :> Mod[val - min, max - min] + min)

Now you can do:

Plot[myperiodic[Sign[x] x^2, {x, -2, 2}] // Evaluate, {x, -10, 10}]

which gives the desired result.

POSTED BY: Marco Thiel
Answer
12 days ago

Cheers! That's what I'm after. Why wont it plot the entire plot range tho? I'm only getting (t from -8.4 to 8.4)

POSTED BY: Stuart Symes
Answer
12 days ago

Dear Stuart,

it is much easier to answer that if you post your code and output. I suppose that the option PotRange->All might help.

Cheers,

Marco

POSTED BY: Marco Thiel
Answer
12 days ago

I tried the PlotRange -> ALL, no luck.

Code:

Plot[Piecewise[{{-Mod[-t, 2]^2, -2 < -Mod[t - 2, 4] <= 0}, {Mod[t, 2]^2, 0 <= Mod[t - 2, 2] < 2}}], {t, -10, 10}, PlotRange -> {{-10, 10}, {-4, 4}}]

enter image description here

POSTED BY: Stuart Symes
Answer
12 days ago

Hi Stuart,

given the output that you produce it looks as if you are not using Mathematica. Are you using Wolfram|Alpha?

You might either want to try Mathematica or get one of the free offers of the Wolfram Language in the Cloud. If you do that you get the full range of values.

Best wishes,

Marco

POSTED BY: Marco Thiel
Answer
12 days ago

Yeah I'm using Wolfram|Alpha. Ok, I'll look into it. Thanks for your help, appreciate it!

POSTED BY: Stuart Symes
Answer
12 days ago

Group Abstract Group Abstract