Search All of the Math Forum:

Views expressed in these public forums are not endorsed by NCTM or The Math Forum.

Notice: We are no longer accepting new posts, but the forums will continue to be readable.

Topic: How to plot functions
Replies: 5   Last Post: Mar 11, 2013 1:41 AM

 Messages: [ Previous | Next ]
 Bill Rowe Posts: 1,647 Registered: 3/14/08
Re: How to plot functions
Posted: Mar 11, 2013 1:41 AM

>Let's say I've defined a function f[c]=c*x^2

>when I try to plot this directly,

>Plot[ f[3], {x, -10, 10} ]

>it doesn't plot anything.

Right. Define f as follows:

f[c_]:= c*x^2

There are two key points. First, I've used SetDelayed (:=)
rather than Set (=). This results in the function f being
evaluated when it is used by Plot, not before. Using Set causes
f to be evaluated when it is defined. That is usually not the
behavior you want for a function.

Second, and more important, I've used c_ not c as the argument
for f. The pattern c_ matches anything and gives it a local name
of c. So, call f with a numeric value, Mathematica replaces the
c on the right hand side with the values supplied.

Without the blank(_) portion and with Set, you have defined f
for the symbol c, not anything else. That is f[3] is undefined
resulting in no plot being displayed. Notice, the difference between

In[7]:= f[c] = c*x^2; f[3]

Out[7]= f[3]

and

In[8]:= g[c_] = c*x^2; g[3]

Out[8]= 3*x^2

Date Subject Author
3/10/13 Bob Hanlon
3/10/13 Christoph Lhotka
3/11/13 Dr. Peter Klamser