Category Archives: Mathematica

How to plot step functions in Mathematica

Those interested in representing graphically  the Relative Supply (RS) curve of a simple 2 x 2 Ricardian model of international trade in Mathematica will find that this task requires the use of Plot[] and Piecewise[]. Below you can find an example to illustrate this:

Plot[{Piecewise[{{0,x<=0},{1.5,0<x<1}, {2,x>= 1}}]},{x,0,12},Exclusions->None, PlotStyle->{Thickness[0.01]}, Axes->True,AxesOrigin->{0,0}]

Each portion of the curve is defined in Piecewise as {function, range}. So Piecewise[{15, x<=5}, {3x, x>5}] is for a function that takes the value 15 if x is smaller than or equal to 5 and the value 3x if x is greater than 5. Note also that in the example above I utilized several options of the command Plot[]. You can find more information about these options at https://reference.wolfram.com/language/tutorial/Options.html .

How to plot curves with different domains in Mathematica

Mathematica (or the Wolfram language) is a very useful tool to plot functions. Suppose you are interested in plotting two curves in the same diagram, let’s say y = 1/x and z = 2/x. Both functions have the same domain of x belonging to the [0,10] interval. This is easily accomplished by typing:

Plot[{1/x, 2/x}, {x, 0, 10}]

 

Suppose now that you would like to plot in the same diagram both functions but the domain of z change to x belonging to the [2,12] interval. How could you do this? This is not difficult. You just need to use the command show with the option PlotRange->All. Here is a piece of code that accomplish this:

p1 = Plot[{1/x}, {x, 0, 10}]

p2 = Plot[{2/x}, {x, 2, 11}]

Show[p1, p2, PlotRange->All]