Re: Why are the functions different?
- To: mathgroup at smc.vnet.net
- Subject: [mg55643] Re: Why are the functions different?
- From: dh <dh at metrohm.ch>
- Date: Fri, 1 Apr 2005 05:36:14 -0500 (EST)
- References: <d2g5mg$f2m$1@smc.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
Hi Helge,
that's a very nice pitfall you stepped into. Usually you are on the save
side if you use ":=" (SetDelayed) to define a function, but here you
have a beautiful counter example.
Try to understand what
f1[x_] := Normal[Series[Tanh[x], {x, 0, 5}]]
does. At first, it simply stores the right hand side expression away
without evaluating it. Later when you say e.g. f[3], Mathematica first
replaces all occurences of x by 3. Afterwards it tries to evaluate the
resulting expression:
Normal[Series[Tanh[3], {3, 0, 5}]]
Of course this gives an error message.
On the other hand, what you intended to do is to assigne the evaluated
right hind side. You want to evaluate it at once. This is done by using
"=" (Set). Here is another pitfall, you must make sure that x does not
already have a value. Therefore, one way to do this is:
x=.;
f1[x_] = Normal[Series[Tanh[x], {x, 0, 5}]]
Sincerely, Daniel
Helge Stenstroem wrote:
> How are these two functions (f0 and f1) different? f0 can be plotted,
> but f1 cannot.
>
> f1[x_] := Normal[Series[Tanh[x], {x, 0, 5}]]
> f0[x_] := x - x^3/3 + (2*x^5)/15
>
> When evaluated like this:
> f0[x]
> f1[x]
> they look the same.
>
> The following gives error messages if f0 is replaced by f1.
> Plot[{Tanh[z], f0[z]}, {z, -1, 1}]
>
> (Mathematica 4.1 on Windows 2000)