A list of functions in Mathematica?

In summary, the conversation discusses the use of Mathematica to define and plot a list of functions without manually typing them all out. The use of # and & characters is explained as a way to define a function without naming the argument. The conversation also mentions a specific example and provides a solution using the Evaluate function to plot the list of functions.
  • #1
Lojzek
249
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?
 
Physics news on Phys.org
  • #2
what the hell is #

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

works exactly how you want it to
 
  • #3
ice109 said:
what the hell is #

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

works exactly how you want it to

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.
 
  • #4
Hi Lojzek,

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

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

Plot[Evaluate[funclist],(x,0,1)]

or another example:

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

Plot[Evaluate[funclist],{x,0,1}]
 

1. What is a function in Mathematica?

A function in Mathematica is a predefined set of instructions that performs a specific task or calculation. It takes in one or more input parameters and produces an output or result. Functions are essential in programming as they allow for efficient and organized code.

2. How do I define a new function in Mathematica?

To define a new function in Mathematica, you can use the syntax "functionName[parameters] := expression". This will create a function with the given name and parameters, and the expression will be evaluated whenever the function is called. You can also use the "Function" command or the "Module" command to define more complex functions.

3. Can I use built-in functions in my own functions?

Yes, you can use built-in functions in your own functions. This is one of the advantages of using Mathematica, as it has a vast library of built-in functions for various mathematical and computational tasks. You can also combine multiple built-in functions to create more complex functions.

4. How do I call a function in Mathematica?

To call a function in Mathematica, you can simply type the function name followed by the input parameters in square brackets. For example, if you have a function named "add" that takes in two numbers and returns their sum, you can call it by typing "add[2, 3]" which will return the result of 5.

5. How can I find a list of all available functions in Mathematica?

You can find a list of all available functions in Mathematica by using the "Names" command. This will return a list of all built-in and user-defined functions in Mathematica. You can also use the "Help" menu or the "Documentation Center" to search for specific functions and their usage.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
248
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
890
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
212
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
7
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
10
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
6
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
Back
Top