Re: iterations, recursions and derivatives
- To: mathgroup at smc.vnet.net
- Subject: [mg22562] Re: [mg22544] iterations, recursions and derivatives
- From: Hartmut Wolf <hwolf at debis.com>
- Date: Sat, 11 Mar 2000 17:52:37 -0500 (EST)
- Organization: debis Systemhaus
- References: <200003090824.DAA20535@smc.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
Otto Linsuain schrieb:
>
> Hello. I find it hard to define a sequence of functions recursively and
> be able to differenciate them at the same time. For example
>
> f[x_,1]:=x^2
>
> Try to differentiate Derivative[1,0][f][2,1] will not work. Changing :=
> for = doesn't help. Can do:
>
> f[x_,1_]:=x^2 Notice the _ after the 1 (Kind of wierd, isn't it?)
>
> Now, however, can differentiate: Derivative[1,0][f][2,1] works fine.
> But cannot work recursions:
>
> f[x_,m_]:= SomeOperation[f[x,m-1]] confuses the recursion process. I
> have tried defining
>
> f[x_,m_]:=If[m==1,x^2,SomeOperation[f[x,m-1]], but the recursion again
> crashes.
>
> I have tried Which, Switch, Condition, Dt, D, etc, but to no avail.
> When I can take the derivative, I can't update m to m+1.
>
> Any suggestions? Thanks, Otto Linsuain.
Dear Otto,
indeed things can't work that way. Let's look at what you did:
In[2]:= f[x_, 1] := x^2
In[3]:= Derivative[1, 0][f][2, 1]
Out[3]= Derivative[1, 0][f][2, 1]
(I converted the output to InputForm) Nothing happed! Why?
Taking the Derivative[1, 0] of the function, meams to effectively
do:
In[4]:=
Derivative[1, 0][(Evaluate[f[x, y] /. {x -> #1, y -> #2}] &) ][2, 1]
Out[4]= Derivative[1, 0][f][2, 1]
Now we see the cause: the second slot of your function as defined cannot
be filled with #2 to take the derivative, it's only '1' that can match
there.
In contrast this works:
In[5]:=
Derivative[1, 0][(Evaluate[f[x, 1] /. {x -> #1}] &) ][2]
Out[5]= 4
Having understood this, we can devellop solutions to your problem:
In[7]:= f[1][x_] := x^2/2 (* made a small change *)
In[8]:= Derivative[1][f[1]]
Out[8]= #1 &
Works!
Now let's define our recursive engine:
In[9]:= Attributes[SomeOperation] = {HoldAll};
In[10]:= SomeOperation[f[n_][x_]] := x/(n + 2) f[n][x]
In[11]:= f[m_][x_] := SomeOperation[f[m - 1][x]]
In[12]:= ?f
"Global`f"
f[1][x_] := x^2/2
f[m_][x_] := SomeOperation[f[m - 1][x]]
So let's produce something:
In[13]:= Table[f[n][x], {n, 10}]
Out[13]=
{x^2/2, x^3/6, x^4/24, x^5/120, x^6/720, x^7/5040,
x^8/40320, x^9/362880, x^10/3628800, x^11/39916800}
In[14]:= Table[Derivative[n][f[8]][x], {n, 10}]
Out[14]=
{x^8/40320, x^7/5040, x^6/720, x^5/120, x^4/24, x^3/6,
x^2/2, x, 1, 0}
An alternative would be to take the derivative (with D),
in a better controlled way:
In[15]:= Clear[f]
In[16]:= f[x_, 1] := x^2/2
In[17]:= D[f[x, 1], x]
Out[17]= x
In[18]:= SomeOperation[f_[x_, n_]] := x/(n + 2) f[x, n]
(The attribute HoldAll is still in effect.)
In[19]:= f[x_, m_] := SomeOperation[f[x, m - 1]]
In[20]:= ?f
"Global`f"
f[x_, 1] := x^2/2
f[x_, m_] := SomeOperation[f[x, m - 1]]
In[21]:= Table[f[x, n], {n, 10}]
Out[21]=
{x^2/2, x^3/6, x^4/24, x^5/120, x^6/720, x^7/5040,
x^8/40320, x^9/362880, x^10/3628800, x^11/39916800}
In[22]:= Table[D[f[x, 8], {x, n}], {n, 10}]
Out[22]=
{x^8/40320, x^7/5040, x^6/720, x^5/120, x^4/24, x^3/6,
x^2/2, x, 1, 0}
Now let's look at the other problem you encountered:
In[23]:= Clear[f]
In[24]:= f[x_, 1_] := x^2 (* kind of weird, isn't it? *)
You missed to look at what you had got:
In[25]:= ?f
"Global`f"
f[x_, _] := x^2
So this explains why your derivative seemingly worked, and why your
recursion ran away.
Kind regards, Hartmut
- References:
- iterations, recursions and derivatives
- From: Otto Linsuain <linsuain+@andrew.cmu.edu>
- iterations, recursions and derivatives