MathGroup Archive 2003

[Date Index] [Thread Index] [Author Index]

Search the Archive

RE: variable definition help

  • To: mathgroup at smc.vnet.net
  • Subject: [mg42391] RE: [mg42379] variable definition help
  • From: "David Park" <djmp at earthlink.net>
  • Date: Fri, 4 Jul 2003 01:33:10 -0400 (EDT)
  • Sender: owner-wri-mathgroup at wolfram.com

Jay,

You might want to look at Section 2.3.1 on patterns in The Mathematica Book.

Generally you will want to use f[x_] as in

f[x_]:= x + Sin[x]

x_ is a pattern that can stand for anything. After you make the definition
you could write f[x], f[y], f[t], f[x+y] or f[anything] and obtain the
expected result. You could also use f'[t] to obtain the derivative.

If instead you used

f[x]:= x + Sin[x]

then the definition would only work for f[x]. f[y], f[t], f[x+y] would be
left unevaluated.

Using

f = x + Sin[x]

loses the variable information and does not really define a function.

You could use a pure function definition:

f = Function[x, x + Sin[x]]

or

f = # + Sin[#]&

but this would just be the long method for the first definition.

If you have a function with parameters you could use the following form

f[a_, b_][x_]:= a Sin[x] + b Cos[x]

Then you could obtain the derivative for a specific case by writing f[3,
2]'[x] This form is generally more convenient than

f[x_]:= a Sin[x] + b Cos[x]

and then using

a = 3;
b = 2;
f[x] or f'[x]

because it avoids having to set values for a and b.

David Park
djmp at earthlink.net
http://home.earthlink.net/~djmp/

David Park
djmp at earthlink.net
http://home.earthlink.net/~djmp/

From: seferiad [mailto:seferiad at pacbell.net]
To: mathgroup at smc.vnet.net


When defining functions, any of the following seem to be commonly used
(assuming the function is of one variable, x):

f   or   f[x]   or   f[x_].

Can someone point me to an explanation that defines the pros/cons of using
one approach over the other. In particular, when I should use  f[x]   vs.
f[x_].

 Thanks, Jay


  • Prev by Date: programming in mathematica
  • Next by Date: Re: New version, new bugs
  • Previous by thread: Re: variable definition help
  • Next by thread: upper and lower bound for the unknowns