15
$\begingroup$

Given a pure function, e.g. f=Sin[#]&, it is possible to plot it with introducing an arbitrary local variable, e.g.,

Plot[f[x],{x,0,1}]

However, the introduction of a variable x seems unnecessary. Is it possible to plot this without specifying a name for the variable? If not, is there a good reason why this functionality doesn't exist?

$\endgroup$
  • $\begingroup$ AFAIK it only exists for InterpolatingFunction. $\endgroup$ – Michael E2 Apr 13 '16 at 0:32
  • 3
    $\begingroup$ One could similarly ask why Integrate[] needs a dummy variable... $\endgroup$ – J. M. will be back soon Apr 13 '16 at 0:44
  • 2
    $\begingroup$ myPlot[f_, r_] := Plot[f[First@r], r]; myPlot[f, {x, 0, 1}] $\endgroup$ – ciao Apr 13 '16 at 9:21
13
$\begingroup$

Yes, you can plot it, but not using Plot. For example, you could map the function over a range of values and then use ListLinePlot:

With[{xmin = 0, xmax = 4π},
 ListLinePlot[f/@Subdivide[##,100],DataRange->{##}]&[xmin,xmax]
]

This uses the new function Subdivide with 100 plot points.

The reason why Plot requires you to specify a dummy variable is that it takes expressions and not functions as its argument. Therefore, the plot variable is not identifiable by a slot, and you need to specify it by naming the plot variable.

$\endgroup$
  • 2
    $\begingroup$ One of course loses out on the adaptive sampling of Plot[] in the process. $\endgroup$ – J. M. will be back soon Apr 13 '16 at 1:41
  • 3
    $\begingroup$ @J.M. Yes - but one person's loss is another person's gain - sometimes the sampling slows plotting down and you end up turning it off anyway... $\endgroup$ – Jens Apr 13 '16 at 2:16
10
$\begingroup$

There is the function Unique[], which is often used to avoid about symbol name collisions.

You could use it like this:

Plot[f[#], {#, 0, 10}] &[Unique[]]

Of course, in another sense, it's the most arbitrary that a variable can get (it's just a variable with a name $12345 where 12345 represents some arbitrary number).

$\endgroup$
  • 3
    $\begingroup$ I can't help but feel that this is a cheat of sorts; you still specified a variable that just happened to be randomly generated. $\endgroup$ – J. M. will be back soon Apr 13 '16 at 4:19
  • 1
    $\begingroup$ @J.M. Sure, but I think it's often what people would want. For example, you might have a the function f[a_] := D[a x^2, x] /. x -> 1 and not have access/be aware to the fact it is dependent on x internally, as it just looks like a simple mapping from a to f[a], but if you plot it with x you get a very different result to any other symbol. $\endgroup$ – Lucas Apr 13 '16 at 14:07
8
$\begingroup$

The short answer is no, not with Plot, which like many other Mathematica built-in functions that range over a set of values, is designed to follow a standard syntax pattern for which Table may be considered the prototype. Consider Do or even Manipulate$\text{*}$, which was added long after Plot. Both follow this pattern although in the case of Manipulate the semantics are wildly different.

$\text{*}$ See this conference article for a discussion of how Manipulate was deliberately designed to emulate Table. It also has many additional interesting things to say about Manipulate.

$\endgroup$
2
$\begingroup$

As an additional small note to ciao's comment, it's possible to define your own function based on Plot to work with pure functions, e.g.:

plotPure[fun_, d_, args___] := 
    Plot[Evaluate@If[ListQ[fun], Through[fun[x]], fun[x]], {x, d[[1]], d[[2]]}, args];

This way it provides all Plot functionality:

plotPure[{Sin, Sin[#]^3 &}, {-Pi, Pi}, PlotStyle->"Thick"]
$\endgroup$

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.