Results 1 to 6 of 6

Math Help - Definin recursive functions in Mathematica

  1. #1
    Senior Member bkarpuz's Avatar
    Joined
    Sep 2008
    From
    R
    Posts
    481
    Thanks
    2

    Exclamation [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
    f_{0}(t)=t for t\in[-1,0],
    I need to compute
    f_{k}(t):=\big(f_{k-1}(t-1)+1\big)^{2} for t\in[n-1,n] and k=1,2,\cdots,n,
    where n\in\mathbb{N} is a fixed number.

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

    Many thanks!

    bkarpuz


    Solution is here.
    Last edited by bkarpuz; January 19th 2010 at 11:13 AM.
    Follow Math Help Forum on Facebook and Google+

  2. #2
    Senior Member bkarpuz's Avatar
    Joined
    Sep 2008
    From
    R
    Posts
    481
    Thanks
    2

    Exclamation

    Quote Originally Posted by bkarpuz View Post
    Dear friends,

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

    For instance, if I let
    f_{0}(t)=t for t\in[-1,0],
    I need to compute
    f_{k}(t):=\big(f_{k-1}(t-1)+1\big)^{2} for t\in[n-1,n] and k=1,2,\cdots,n,
    where 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 n and \varphi to the function and get the output figure?
    Follow Math Help Forum on Facebook and Google+

  3. #3
    Super Member
    Joined
    Aug 2008
    Posts
    903
    Quote Originally Posted by bkarpuz View Post
    Dear friends,

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

    For instance, if I let
    f_{0}(t)=t for t\in[-1,0],
    I need to compute
    f_{k}(t):=\big(f_{k-1}(t-1)+1\big)^{2} for t\in[n-1,n] and k=1,2,\cdots,n,
    where 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:

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

    and I could say:

    Code:
    In[17]:= myFunctions[[2]] /. t -> 4
    
    Out[17]= 100
    Follow Math Help Forum on Facebook and Google+

  4. #4
    Senior Member bkarpuz's Avatar
    Joined
    Sep 2008
    From
    R
    Posts
    481
    Thanks
    2

    Question

    Quote Originally Posted by shawsend View Post
    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:

    \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 t I could not make it work when I modified.
    Is it possible to do something like this one?
    Follow Math Help Forum on Facebook and Google+

  5. #5
    Super Member
    Joined
    Aug 2008
    Posts
    903
    Quote Originally Posted by bkarpuz View Post
    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 n and \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]]
    Follow Math Help Forum on Facebook and Google+

  6. #6
    Senior Member bkarpuz's Avatar
    Joined
    Sep 2008
    From
    R
    Posts
    481
    Thanks
    2

    Lightbulb

    Quote Originally Posted by shawsend View Post
    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.
    Last edited by bkarpuz; January 19th 2010 at 11:11 AM.
    Follow Math Help Forum on Facebook and Google+

Similar Math Help Forum Discussions

  1. Recursive to Sum if possible
    Posted in the Pre-Calculus Forum
    Replies: 1
    Last Post: March 26th 2011, 03:59 PM
  2. recursive
    Posted in the Discrete Math Forum
    Replies: 1
    Last Post: May 26th 2010, 05:27 PM
  3. A Recursive Function Problem in Mathematica
    Posted in the Math Software Forum
    Replies: 2
    Last Post: July 1st 2009, 02:20 PM
  4. Primitive Recursive vs Recursive Functions
    Posted in the Discrete Math Forum
    Replies: 2
    Last Post: January 29th 2009, 07:32 AM
  5. recursive def
    Posted in the Discrete Math Forum
    Replies: 4
    Last Post: July 31st 2007, 08:01 AM

Search Tags


/mathhelpforum @mathhelpforum