# Thread: Definin recursive functions in Mathematica

1. ## [SOLVED] Defining recursive functions in Mathematica

Dear friends,

I need to define a number of functions recursively by defining its initial function.

For instance, if I let
$\displaystyle f_{0}(t)=t$ for $\displaystyle t\in[-1,0]$,
I need to compute
$\displaystyle f_{k}(t):=\big(f_{k-1}(t-1)+1\big)^{2}$ for $\displaystyle t\in[n-1,n]$ and $\displaystyle k=1,2,\cdots,n$,
where $\displaystyle n\in\mathbb{N}$ is a fixed number.

How can I do this (should I use tables)?

Many thanks!

bkarpuz

Solution is here.

2. Originally Posted by bkarpuz
Dear friends,

I need to define a number of functions recursively by defining its initial function.

For instance, if I let
$\displaystyle f_{0}(t)=t$ for $\displaystyle t\in[-1,0]$,
I need to compute
$\displaystyle f_{k}(t):=\big(f_{k-1}(t-1)+1\big)^{2}$ for $\displaystyle t\in[n-1,n]$ and $\displaystyle k=1,2,\cdots,n$,
where $\displaystyle n\in\mathbb{N}$ is a fixed number.

How can I do this (should I use tables)?

Many thanks!

bkarpuz
One may wish to learn the solution.
I have coded the following and it works.

Code:
n = 2;
\[Phi][t_] = t;
xmax = MaxValue[{\[Phi][t], t >= -1, t <= 0}, t];
xmin = MinValue[{\[Phi][t], t >= -1, t <= 0}, t];
x = Table[ 0, {i, 1, n + 1}];
x[[1]] = \[Phi][t];
For[i = 2, i <= n + 1, i++,
x[[i]] = Simplify[(ReplaceAll[x[[i - 1]], t -> (t - 1)]
+ 1)^2];
xmax = Max[{xmax, MaxValue[{x[[i]], t >= i - 2,
t <= i - 1}, t]}];
xmin = Min[{xmin, MinValue[{x[[i]], t >= i - 2,
t <= i - 1}, t]}];
]
Show[Table[
Plot[x[[i]], {t, i - 2, i - 1},
PlotRange -> {{-1, n}, {xmin, xmax}}], {i, 1, n + 1}]]
I have another question.
How can I build a function for this?
I mean I will only send $\displaystyle n$ and $\displaystyle \varphi$ to the function and get the output figure?

3. Originally Posted by bkarpuz
Dear friends,

I need to define a number of functions recursively by defining its initial function.

For instance, if I let
$\displaystyle f_{0}(t)=t$ for $\displaystyle t\in[-1,0]$,
I need to compute
$\displaystyle f_{k}(t):=\big(f_{k-1}(t-1)+1\big)^{2}$ for $\displaystyle t\in[n-1,n]$ and $\displaystyle k=1,2,\cdots,n$,
where $\displaystyle n\in\mathbb{N}$ is a fixed number.

How can I do this (should I use tables)?

Many thanks!

bkarpuz
I think this is what you want:

Code:
In[12]:=
Subscript[f, 0][t] = t;
myFunctions = Table[
Subscript[f, n_][t_] =
(Subscript[f, n - 1][t - 1] + 1)^2,
{n, 1, 4}]

Out[13]=
{t^2, (1 + (-1 + t)^2)^2,
(1 + (1 + (-2 + t)^2)^2)^2,
(1 + (1 + (1 + (-3 + t)^2)^2)^2)^2}
So then:

$\displaystyle \texttt{myFunction[[2]]}=((t-1)^2+1)^2$

and I could say:

Code:
In[17]:= myFunctions[[2]] /. t -> 4

Out[17]= 100

4. Originally Posted by shawsend
I think this is what you want:

Code:
In[12]:=
Subscript[f, 0][t] = t;
myFunctions = Table[
Subscript[f, n_][t_] =
(Subscript[f, n - 1][t - 1] + 1)^2,
{n, 1, 4}]

Out[13]=
{t^2, (1 + (-1 + t)^2)^2,
(1 + (1 + (-2 + t)^2)^2)^2,
(1 + (1 + (1 + (-3 + t)^2)^2)^2)^2}
So then:

$\displaystyle \texttt{myFunction[[2]]}=((t-1)^2+1)^2$

and I could say:

Code:
In[17]:= myFunctions[[2]] /. t -> 4

Out[17]= 100
I have found something like that in the internet.

Code:
PicardIteration[x0_, y0_, max_] :=
Module[{i, k},
Y = Table[0, {i, max + 1}];
Y[[1]] =
y0;
k = 1;
Print[Subscript["Y", 1], "(x) = ", Y[[1]] ];
While[k < max + 1,
Module[{},
Y[[k + 1]] =
y0 + Integrate[ f[t, ReplaceAll[Y[[k]], x -> t] ], {t, x0, x};
Y[[k + 1]] = Normal[Series[ Y[[k + 1]], {x, x0, 100}]];
k = k + 1;
Print[Subscript["Y", k], "(x) = ", Y[[k]] ];  ]; ];
Print[" "];
Print["The I.V.P. is"];
Print["  y' = ", f[x, y], "   with   y(", x0, ") = ", y0];
Print["After  ", k - 1, "  iterations, we have"];
Print["y(x) \[TildeTilde] ", Y[[k]]];
];
Then I modified this due to my purpose.
However since one of my entries is a function of $\displaystyle t$ I could not make it work when I modified.
Is it possible to do something like this one?

5. Originally Posted by bkarpuz
One may wish to learn the solution.
I have coded the following and it works.

Code:
n = 2;
\[Phi][t_] = t;
xmax = MaxValue[{\[Phi][t], t >= -1, t <= 0}, t];
xmin = MinValue[{\[Phi][t], t >= -1, t <= 0}, t];
x = Table[ 0, {i, 1, n + 1}];
x[[1]] = \[Phi][t];
For[i = 2, i <= n + 1, i++,
x[[i]] = Simplify[(ReplaceAll[x[[i - 1]], t -> (t - 1)]
+ 1)^2];
xmax = Max[{xmax, MaxValue[{x[[i]], t >= i - 2,
t <= i - 1}, t]}];
xmin = Min[{xmin, MinValue[{x[[i]], t >= i - 2,
t <= i - 1}, t]}];
]
Show[Table[
Plot[x[[i]], {t, i - 2, i - 1},
PlotRange -> {{-1, n}, {xmin, xmax}}], {i, 1, n + 1}]]
I have another question.
How can I build a function for this?
I mean I will only send $\displaystyle n$ and $\displaystyle \varphi$ to the function and get the output figure?
Let me answer this one first. Not sure about the other one you asked. In the code below, I convertet your code to a function called myFunction and passed to it a value of n as well as a function f[t_]:=t and then used Show[myFunction[2,f]]:

Code:
f[t_] := t;
myFunction[n_, myf_] := Module[{xmax, xmin, x, i},
xmax = MaxValue[{myf[t], t >= -1, t <= 0}, t];
xmin = MinValue[{myf[t], t >= -1, t <= 0}, t];
x = Table[0, {i, 1, n + 1}];
x[[1]] = myf[t];
For[i = 2, i <= n + 1, i++,
x[[i]] = Simplify[(ReplaceAll[x[[i - 1]], t -> (t - 1)] + 1)^2];
xmax = Max[{xmax, MaxValue[{x[[i]], t >= i - 2, t <= i - 1}, t]}];
xmin = Min[{xmin, MinValue[{x[[i]], t >= i - 2, t <= i - 1}, t]}];
];
Table[Plot[x[[i]], {t, i - 2, i - 1},
PlotRange -> {{-1, n}, {xmin, xmax}}], {i, 1, n + 1}]
];
Show[myFunction[2, f]]

6. Originally Posted by shawsend
Let me answer this one first. Not sure about the other one you asked. In the code below, I convertet your code to a function called myFunction and passed to it a value of n as well as a function f[t_]:=t and then used Show[myFunction[2,f]]:

Code:
f[t_] := t;
myFunction[n_, myf_] := Module[{xmax, xmin, x, i},
xmax = MaxValue[{myf[t], t >= -1, t <= 0}, t];
xmin = MinValue[{myf[t], t >= -1, t <= 0}, t];
x = Table[0, {i, 1, n + 1}];
x[[1]] = myf[t];
For[i = 2, i <= n + 1, i++,
x[[i]] = Simplify[(ReplaceAll[x[[i - 1]], t -> (t - 1)] + 1)^2];
xmax = Max[{xmax, MaxValue[{x[[i]], t >= i - 2, t <= i - 1}, t]}];
xmin = Min[{xmin, MinValue[{x[[i]], t >= i - 2, t <= i - 1}, t]}];
];
Table[Plot[x[[i]], {t, i - 2, i - 1},
PlotRange -> {{-1, n}, {xmin, xmax}}], {i, 1, n + 1}]
];
Show[myFunction[2, f]]
Thanks a lot!.
In my code, I guess it was working too but I called the function as
Code:
Show[myFunction[2, t]]
, i.e., without defining
Code:
f[t_]:=t;
This solves my problem.
Tyvm shawsend.