If I am plotting a square wave s.t.:

$Assumptions = {k, p} ∈ Reals && {k, p} > 0
f[x_] := Piecewise[{{2 k, 2 p < x < 4 p}, {0, 0 < x < 2 p}, {0, 4 p < x}}]

Is there a way that I can plot this without setting k and p equal to something (for example 1)?

An additional question, is there a way to evaluate f[x] without giving k and p values? E.g.:

In[65]:= f[3 p]
Out[66]= Piecewise[{{2 k, 2 p < 3 p < 4 p}, {0, True}}]

would instead evaluate as

In[65]:= f[3 p]
Out[66]= 2 k

Thank you all for you time,

Edited to add that I am trying to make a plot similar to squarewell

but with a square wave. Notice that the units for the range are multiples of E.

  • $\begingroup$ 1) There is no good way to plot f without assigning values for $p$ and $k$; if the latter are not specified, then f is really a function of 3 variables; your best bet would then be some kind of ContourPlot3D. 2) Most functions don't take assumptions into consideration automatically; try perhaps Simplify[f[3 p]], or more directly Refine[f[3 p]] (which should be invoked automatically by Simplify anyway). $\endgroup$ – MarcoB Dec 6 '16 at 5:54
  • $\begingroup$ ContourPlot3D is the wrong way to go about it. Looking to plot something with units as shown in this graphic: i.stack.imgur.com/iIQEL.gif $\endgroup$ – FoxForceFive Dec 6 '16 at 6:01

Your assumption {k, p} > 0 is not equivalent to k > 0 && p > 0, likewise {k, p} ∈ Reals is not what you intend, but it's not necessary anyway, as it is implied by k > 0 && p > 0. So with

$Assumptions = k > 0 && p > 0

you can do (as noted by @MarcoB)

Simplify[f[3 p]] 
(* 2 k *)

A typical way to plot this type of thing is by rescaling to use dimensionless variables:

Plot[Simplify[f[x p]/k], {x, 0, 6}, AxesLabel -> {p, k}]

Mathematica graphics

If you want to explicitly label each tick with the units you can do

Plot[Simplify[f[x p]/k], {x, 0, 6}, 
  Ticks -> {{1, p} # & /@ Range[6], {1, k} # & /@ Range[0, 2, .5]}

Mathematica graphics

  • $\begingroup$ Thank you, this is exactly what I was looking for. $\endgroup$ – FoxForceFive Dec 7 '16 at 16:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.