# How could we define a function recursively?

Let us first consider the constructon of the following simple iteration

Clear["Global*"]
p[z_] := z^3 - 1;
beta = -Sign[p'[z]];
theIterationFunction = z - p[z]/(p'[z] + beta p[z]) // FullSimplify


which results in the following correct iteration function

$$theIterationFunction=z+\frac{-1+z^3}{-3 z^2+\left(-1+z^3\right) \text{Sign}[z]^2}.$$

Now by choosing any value for $z$, the output would be simply given in Mathematica 8, since the computation of $\text{Sign}[z]$ has been done automatically. My problem is in a similar but more complicated iteration. Please consider the following

Clear["Global*"]
p[z_] := z^3 - 1;
w = z + beta  p[z]; FD = (p[w] - p[z])/(w - z);
theIterationFunction2 = z - p[z]/FD // FullSimplify


which yeilds in

$$theIterationFunction2=z-\frac{\text{beta} \left(-1+z^3\right)^2}{-z^3+\left(z+\text{beta} \left(-1+z^3\right)\right)^3}.$$

In fact, here $beta$, must be calculate by the formula $beta=\frac{-p[z]+p[z+\text{beta} p[z]]}{\text{beta} p[z]}$. That is, there is a self-defined formula for $beta$, which itself depends on $beta$. Thus, we must choose a starting value for $beta$, such as $beta=0.1$ at the beggining, but after that, each time theIterationFunction2 must calculate a new value based on its formula. For example, at the very beggining, we set $beta=0.1$ and $z=2.$, and the output will be a new value for $beta$ (NEEDLESS TO BE SHOWN) and my aimed value $y$. Now, if I give $y$, the last value of $beta$ which is saved in the memory must be used to produce a new value for $beta$ and my aimed new value $yy$. The process must be go on in this way. Hence, altogether I must define a function recursively with a pre-assumed starting value for $beta$, while I am un-able to do so! I will be thankful if anyone could help me to define such a function recursively.

-

In general that formula for $beta$ doesn't have a unique solution, notice that Solve[beta == -p[z] + p[z + beta p[z]]/(beta p[z]), beta] has two solutions.

But let's just pick the first one and always use that:

Clear[p, w, beta, fd, it];
p[z_] := z^3 - 1
w[z_] = z + beta  p[z];
fd[z_] = (p[w[z]] - p[z])/(w[z] - z);
sol = First@Solve[beta == (-p[z] + p[z + beta p[z]])/(beta p[z]), beta];
it[z_] = FullSimplify[(z - p[z]/fd[z])/.sol]


$\frac{\sqrt{1-3 z \left(z^7-2 z^4+2 z^3+z-2\right)}+z \left(\left(3 z^4-\sqrt{1-3 z \left(z^7-2 z^4+2 z^3+z-2\right)}-6 z+5\right) z^2+3\right)+1}{6 z^2}$

Using Last solution instead you'd get:

$\frac{-\sqrt{1-3 z \left(z^7-2 z^4+2 z^3+z-2\right)}+z \left(\left(3 z^4+\sqrt{1-3 z \left(z^7-2 z^4+2 z^3+z-2\right)}-6 z+5\right) z^2+3\right)+1}{6 z^2}$

Or did I misunderstand the question and the new beta on the left side and the previous one on the right side?

If so you can use Nest to simultaneusly calculate the new beta and pass that back to the function you want to iterate:

Clear@it;
it[z_, beta_] = FullSimplify[z - p[z]/fd[z]];
nextbeta[z_, beta_] = (-p[z] + p[z + beta p[z]])/(beta p[z]);
z0 = 2.; beta0 = 0.1;
First@Nest[{it@@##, nextbeta@@##} &, {z0, beta0}, 4]


You can see here what is happening:

Clear[it, nextbeta, z0, beta0]
NestList[{it @@ ##, nextbeta @@ ##} &, {z0, beta0}, 2]
(* {{z0, beta0},
{it[z0, beta0], nextbeta[z0, beta0]},
{it[it[z0, beta0], nextbeta[z0, beta0]], nextbeta[it[z0, beta0], nextbeta[z0, beta0]]}} *)

-
Thank you ssch. The second way that you describe is my question. That is, the old beta is in the right side and the new beta must be obtained as the left beta. Therefore, in your second part of the answer, there are still some drawbacks in my idea. Since, I need the final formula as an iteration function (similar to the case of Sign[z]), not to be defined numerically as you have done for four nested values. Is there any way to define this recursive function by two parts I mean the first part calculate a new value (a new value) and the second part update beta? –  Fazlollah Soleymani Feb 26 '13 at 21:05
@FazlollahSoleymani You can do something like: beta = 0.1; f[z_] := First@{it[z, beta], beta = nextbeta[z, beta]} or wrap it in a module to localize the beta: newit[beta0_] := Module[{beta = beta0}, First@{it[#, beta], beta = nextbeta[#, beta]} & ]; f = newit[0.1] –  ssch Feb 26 '13 at 21:59