Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

A list of functions in Mathematica?

  1. Jul 9, 2008 #1
    I would like to define and plot a list of functions in Mathematica without
    typing all them, but I can't find the solution.

    Let's say that we want to plot x, x^2, ..., x^10 on interval [0,1].

    This is what I tried:

    funclist=Table[#^n&,(n,1,10)]
    Plot[funclist[x],(x,0,1)]


    This does not work: n appears in the list as a variable. How can I tell the program that
    the current numerical value of n should be used for the power?
     
  2. jcsd
  3. Jul 11, 2008 #2
    what the hell is #

    funclist = Table[x^k, {k, 1, 10}]
    Plot[funclist,{x,0,1}]

    works exactly how you want it to
     
  4. Jul 11, 2008 #3
    Characters # and & are used to define a function of any argument without naming the argument (it is called a "pure function").

    Actually my first try was the same as your, but it does not work. funclist is assigned the right powers, but Plot reports "funclist is not a machine sized real number at..."

    Funny, if I copy the output of the funclist assignment sentence into the Plot function, then it works. I don't understand why, since the list intended to be plotted should be the same in both cases.
     
  5. Jul 11, 2008 #4

    alphysicist

    User Avatar
    Homework Helper

    Hi Lojzek,

    For the specific example you mention, I would do this:

    Code (Text):

    funclist=Table[x^n,(n,1,10)]

    Plot[Evaluate[funclist],(x,0,1)]
     
    or another example:

    Code (Text):

    funclist=Table[Sin[n x],{n,1,5}]

    Plot[Evaluate[funclist],{x,0,1}]
     
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?



Similar Discussions: A list of functions in Mathematica?
  1. Lists in mathematica (Replies: 4)

Loading...