Showing posts with label PlotLabel. Show all posts
Showing posts with label PlotLabel. Show all posts

Wednesday, June 24, 2015

Using Recursion to Define a Periodic Function

I'm working my way through Heikki Ruskeepaa's book, Mathematica Navigator and found this elegant one-liner that shows how to define a periodic function with recursion. Note that the base case required to stop the recursion is not a single value but the condition 0 <= t <= 2.

Clear@sawtooth;sawtooth[time_,scale_:1]:=If[0<=time<=2,scale time,sawtooth[scale (time-2)]]
Plot[sawtooth@t,{t,0,10}]



How does the recursion work? If t => 2, sawtooth keeps subtracting 2 from t recursively until t is back in the range 0 <= t <= 2 and computes that value for y. We can see sample values of a function plotted with DiscretePlot.

DiscretePlot[sawtooth@t,{t,0,6,0.2},PlotTheme->"Web"]



Of course a faster way of defining the domain is to use Mod, but there is a lesson here. We don't need the speed of Mod, so either implementation is fine - using Mod for the most compact and fast function, or using an elegant new recursive technique to learn it. This function omits the values at multiples of 2, but is effectively the same function:

Clear@sawtooth2;sawtooth2[time_,scale_:1]:=scale Mod[time,2]

Mathematica has built-in functions to produce various canonical waveforms  such as SawtoothWave. Here are square and triangular wave examples.

Plot[SquareWave[{-50,50},t],{t,0,10},ExclusionsStyle->Dotted,AxesLabel->{"time (mS)","mV"},PlotLabel->"Electric potential at the electrode"]




Plot[TriangleWave[{-40,10},t],{t,0,10},AxesLabel->{"time (mS)","mV"},PlotLabel->"Electric potential seen by the axon"]




Thursday, October 24, 2013

ListLinePlot with Controls to Improve Plot Readability

For good readability, as required by a journal, we need to change the look of a ListLinePlot and it can take a bit of fiddling. Here is a template I created that you can use. For publication, the best resolution seems to be given by saving as PDF, but PNG is also good. Although accepted by journals, TIFF doesn't seem to give sufficient resolution (e.g. 600 DPI). To give more separation between the PlotLabel and the y-axis label, add \n at the end of the PlotLabel to insert a newline.

I have shown the code in formal block indented style by inserting line returns after each command, and then Mathematica creates the indents at their proper level. This is a coding best practice. but I typically just leave mine as one continuous stream of commands.

Please note: If you copy and paste this code into your Notebook, the plot will look messed up until you click and drag a corner to expand it to the size you'd see in a journal. You can adjust the controls to work for a smaller size.

This is a plot of axon thresholds by the electrical stimulus strength on their surface that is required to fire them for various diameter axons. The stimulation is in 60 microsecond pulses, 5000 per second. Axons have little repeater stations to keep the signal going and electrical stimulation can trigger these repeaters. These are large- to medium-sized fibers that sense proprioception (muscle position) and fine-touch with a conduction velocity of 30 - 70 m/sec. The overall study is on high frequency stimulation of the spinal cord for pain relief.

ListLinePlot[
  {{5.7,.87},{7.5,.53},{8.7,.40},{10.0,.33},{11.5,.25},{12.8,.25},{14.0,.24}},
  Mesh->Full,
  MeshStyle->PointSize@Large,
  PlotStyle->Thick,
  TicksStyle->Large,
  PlotLabel->Style["Fiber Thresholds at Frequency = 5 KHz and 60 \[Mu]s Pulse Width",Large],
  AxesLabel->{Style["Fiber Diameter (\[Mu]m)",FontSize->18],   Style["Stimulus (nA)",FontSize->18]}
]