[no subject]
- To: mathgroup at yoda.physics.unc.edu
- From: rvk at blink.att.com
- Date: Tue, 27 Oct 92 14:58:38 EST
thanks to the more than two dozen people who responded to my
inquiry about converting a series to a function. a few people
asked me to pass on any responses. I'm attaching a few represetative
ones.
-r. kline
rvk at blink.att.com
==========================================================================
In[10]:= f = (Normal[Series[Sin[x],{x,0,5}]] /. x->#)&
Out[10]= Normal[Series[Sin[x], {x, 0, 5}]] /. x -> #1 &
In[11]:= f[x]
3 5
x x
Out[11]= x - -- + ---
6 120
In[12]:= f[r]
3 5
r r
Out[12]= r - -- + ---
6 120
==========================================================================
The transforming of general expressions into functions is something
that I regularly accomplish with the following inelegant but quick
solution. Say I have an exression called bob:
In[1]:= bob = Normal[ Series[ Sin[x], {x,0,5} ] ]
3 5
x x
Out[1]= x - -- + ---
6 120
Now turn that into a function:
In[2]:= fun[z_]:=bob/.{x->z}
It now has all of the properties of ordinary functions of arbitrary arguments:
In[3]:= fun[alpha]
3 5
alpha alpha
Out[3]= alpha - ------ + ------
6 120
In[4]:= D[fun[alpha],alpha]
2 4
alpha alpha
Out[4]= 1 - ------ + ------
2 24
==========================================================================
In[1]:=
a=Normal[Series[Sin[x],{x,0,5}]]
Out[1]=
3 5
x x
x - -- + ---
6 120
In[5]:=
f[x_]:=a
The definition of the function f is not exactly what you want.
In[6]:=
??f
Global`f
f[x_] := a
The reason is that ":=", or in FullForm SetDelayed[], has the attribute HoldAll
, which means it does not evaluate it's arguments. In this example it means tha
t "a" does not evaluate to the series with "x" in it.
In[7]:=
Attributes[SetDelayed]
Out[7]=
{HoldAll, Protected}
You can force evaluation of the right hand side by the following definition:
In[9]:=
f1[x_] := Evaluate[ a ]
Note that you now get the desired result.
In[10]:=
??f1
Global`f1
f1[x_] := x - x^3/6 + x^5/120
Wanting to assign a function to c computed result occurs often.
==========================================================================
You asked about a=Normal[Series[Sin[x],{x,0,5}]] and f[x_]:=a ...
What you want instead of f[x_]:=a is f[x_]:=Release[a]. That should do it.
==========================================================================
Suppose I create a series, using
a=Normal[Series[Sin[x],{x,0,5}]]
giving a = x - x^3/3! + x^5/5!.
I now want to use a to create a function of x, which, functionally,
is
f[x_]:=a.
Define a=Normal[Series[Sin[x],{x,0,5}]] // InputForm
and then F[x_] := Evaluate[a] // N.
==========================================================================
You need to understand the distinction between Set[ ] ("=") and SetDelayed[ ]
(":="). With SetDelayed[ ] you form a rule with the *unevaluated* right
hand side. So if you type f[x_] := a you get exactly what you typed in
and since there is no "x" in "a" there is nothing for the pattern matcher
to match. It doesn't work. If you type f[x_] = a the "a" first gets
evaluated to x - x^3/3! + x^5/5! and the rule that you get is then
f[x_] = x - x^3/6 + x^5/120. This rule will then work.
==========================================================================
You need to do an immediate assignment:
f[x_] = a (* NOT f[x_] := a *)