Plot/Function

From J Wiki
Jump to navigation Jump to search
Plot | Verbs | Class Commands Data Options Outputs Types | Colors Fonts Keys Text YAxes | Function Multi Shape

Function Plots

The simplest way to plot a function is to give Plot the description of the function and the domain, and let Plot decide where to evaluate the function. You do this by giving the function instead of the data as the last variable in the right argument to plot or pd. The plot may be a 2-D plot or a 3-D plot. An example is

plot 0 10 ; 'sin'

The character string instead of data tells Plot to evaluate the sin function over the interval [0,10]. One advantage of a function plot, besides its simplicity, is that it has the X-axis information so can label that axis correctly. Compare the above example to the equivalent numeric "plot sin 5+i:5j99". In this case, "plot" does not have the X values available so it labels that axis with integers from 0 to 99 as seen here, whereas the function plot labels the X-axis with values 0-10, as given to the function.

FunctionPlotSine0-10.png NumericPlotSine0-10.png

The independent variable(s) are given either as intervals or as lists of points. An interval is indicated by 2 or 3 numbers, specifying start value,end value,number of steps. A list of points is indicated by a boxed argument containing the points, or by an unboxed list of more than 3 points. Multiple intervals or point-lists are allowed.

If the number of steps is omitted or 0, Plot will pick an appropriate number of points to use. It does so by repeatedly subdividing the interval until the curve is smooth or it decides that the curve is discontinous, in which case it plots continuous sections separately.

The subdivision is controlled by the plot options Cfuncres and singtoler. Cfuncres (C is x or y) gives the subdivision resolution: an interval smaller than 1/Cfuncres of the screen will not be split. Cfuncres defaults to twice the pixel resolution of the plot. singtoler is used when the display has singularities, and controls how much of the heading-off-to-infinity tail of the curve will be shown at the singularity. You can experiment to find a good value for singtoler for your application; the default is 10 and higher numbers cause more of the tail to be displayed.

The function(s) to be displayed can be given as a list of gerunds, one for each verb to be drawn, or as a string where the verb-specifiers are separated by the ` character (use doubled ` as an escape if your verb contains a ` character). Each verb-specifier can be in either tacit or explicit form: if it contains the words y or y. it is assumed to describe an explicit verb, otherwise a tacit one.

The verbs are invoked with lists as arguments and should be able to return a list of results. If you use pd , note that the verbs are not executed until pd 'show' is processed, so the values of any public variables that are referred to by an explicit verb will use the values in effect when the pd 'show' is executed. Public variables referred to in a tacit verb are frozen (using f.) when the pd for the function is issued.

Examples of function plots:

plot _10 10 ; '%'           NB. reciprocal: has a discontinuity
plot _10 10 ; 'sin`cos'     NB. two curves
plot 0.001 0.1 ; 'sin % y'  NB. sin(1/x), a busy function

3D plot example:

f=: 4 : '(cos r) % 1 + r =. x +&:*: y'
plot _4 4 100 ; _4 4 100 ; 'f'

Re-working Function Definition from Monadic to Dyadic

Sometimes it may be necessary to change the form of a function's definition to accommodate a function plot.

For instance, say we have the standard sombrero function defined to take an argument specifying the points along one side of the base of the sombrero (a single vector argument):

   sombrero0=: [: (1&o. % ]) [: %: [: +/~ *:

So, a straightforward plot might look like this:

   'surface' plot sombrero0 i:20j99
SombreroMonadicallyGenerated-sm.png

However, since this function takes only a single argument, it generates the grid of points by orthogonally adding the squares of the vector argument with +/~ so it isn't in a dyadic form. Note that a dyadic form is more general since we specify the grid by two sets of points to be combined orthogonally instead of using the single set twice.

A dyadic form might look like this:

dyasombrero=: (4 : '(1&o. % ]) %:+/*:x,y')"0/

where we put "0/ in the definition to work on the scalar elements of one vector versus each scalar element of the other vector.

So, we can make a non-square sombrero like this:

   plot _25 25 100; _15 15 100; 'dyasombrero'
DyadicSombreroByFunctionPlot-sm.png

Here's another way to make a sombrero and save it as a .PNG file. Note that we also replace the default palette with our own by re-assigning RGCLR_jzplot:

   load '~User/code/bmpPal.ijs'
   RGCLR_jzplot_=: ADJPAL
   sombrero=: 4 : '(cos % >:)x +&:*: y'
   'mesh 0' plot _4 4 100; _4 4 100; 'sombrero'
   pd 'save png C:\amisc\pix\sombreroCos.png'
94535
SombreroCos-sm.png