Message Boards Message Boards

0
|
4214 Views
|
5 Replies
|
3 Total Likes
View groups...
Share
Share this post:

Best way to define composite function of arbitrary function?

I'm still learning Mathematica. Where do I find documentation on how to use a generalized real-value function as an argument in a function?

The attached notebook is the simplest example I could think of, though it would be a handy tool in itself if it worked. Specific questions are in the comments.

As I said, I'm still learning Mathematica. So where there's a choice, please use explicit forms of the commands.

Thanks.

POSTED BY: Jay Gourley
5 Replies

Thanks, Gianluca Gorni. Works great. I'm glad you experts take the time to help newbies.

Attachments:
POSTED BY: Jay Gourley

Just avoid the construct f[var] in the definition of myPlot:

myPlot[f_, var_, a_, b_] :=
 Manipulate[Plot[f, {var, a, b},
   Epilog -> {PointSize[.02], Red,
     Point[{pnt, f /. var -> pnt}]}],
  {pnt, a, b}]

This way you can use myPlot with all kinds of functions:

myPlot[x^3, x, 1, 2]
myf[x_] := x^2;
myPlot[myf[x], x, 1, 2]
ff[x_, t_, z_] := x^3/t + z;
myPlot[ff[x, 3, 7], x, 1, 2]
POSTED BY: Gianluca Gorni

Jay,

here one can use the concept of "pure functions":

ff[x_, t_, z_] := x^3/t + z;
myPlot[ff[#, 3, 7] &, x, -2, 2]

or directly

myPlot[#^3 &, x, -2, 2]

As a remark: In your function myPlot the definition of var is not really necessary as it is simply a dummy variable. One way would be using a local variable, another way is to use "formal parameters", e.g.:

myPlot[f_, a_, b_] := Manipulate[Show[Plot[f[\[FormalX]], {\[FormalX], a, b}], 
   Graphics[{PointSize[.02], Point[{pnt, f[pnt]}, VertexColors -> Red]}]], {pnt, a, b}]

Edit: ... or no specific variable definition at all as this variable is local to Plot.

Does that help? Regards -- Henrik

POSTED BY: Henrik Schachner
Posted 3 years ago

For ff we can also use SubValues which is a WL concept similar to Currying.

ClearAll@ff
ff[t_, z_][x_] := x^3/t + z

myPlot[ff[3, 7], x, -2, 2]

Information@ff
POSTED BY: Rohit Namjoshi

Thanks, Henrik. Sorry for the delayed gratitude. I had hoped to study up on pure functions before now. It may be the weekend before I do. In the meantime, I want you to know, I appreciate the help. I'll add to this thread when I get a chance top try out your suggestion.

POSTED BY: Jay Gourley
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract