A list of functions in Mathematica?

Click For Summary

Discussion Overview

The discussion revolves around defining and plotting a list of functions in Mathematica, specifically focusing on how to create a list of power functions and correctly use them in a plot over a specified interval. The scope includes technical explanations and problem-solving related to programming in Mathematica.

Discussion Character

  • Technical explanation
  • Debate/contested

Main Points Raised

  • One participant seeks a method to define and plot functions like x, x^2, ..., x^10 without manually typing each function, expressing confusion over the use of a variable in the function list.
  • Another participant suggests using a simpler syntax with Table to create the list of functions, indicating that their approach works as intended.
  • A participant explains the use of the symbols # and & in defining pure functions, noting that their initial attempt did not work due to how Mathematica interprets the function list in the Plot command.
  • A later reply proposes using Evaluate with the function list to ensure proper plotting, providing examples with both power functions and sine functions.

Areas of Agreement / Disagreement

Participants express differing views on the correct syntax and methods for achieving the desired plotting functionality, with no consensus reached on a single solution.

Contextual Notes

Some limitations include the misunderstanding of how Mathematica handles function lists and the specific requirements for the Plot function to interpret those lists correctly.

Lojzek
Messages
246
Reaction score
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
what the hell is #

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

works exactly how you want it to
 
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.
 
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}]
 

Similar threads

  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 11 ·
Replies
11
Views
3K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 9 ·
Replies
9
Views
3K
  • · Replies 1 ·
Replies
1
Views
1K
  • · Replies 1 ·
Replies
1
Views
1K
  • · Replies 7 ·
Replies
7
Views
2K
  • · Replies 13 ·
Replies
13
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K