Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

I am still trying to figure out Mathematica (started early August). I am trying to write a program that will accept an arbitrary function and an arbitrary sequence that can start at an arbitrary natural number in order to compose the function with the sequence so that my students can investigate the sequential criterion for the limit of a function. I cannot figure out how to get Mathematica to accept a function...below is what I have worked out so far. Any help would be greatly appreciated!

Manipulate[DynamicModule[{ a = 1 /n, b = 1}, 
  Column[{Labeled[InputField[ Dynamic[  f[x_] :> ( x^2 - 4)/( x - 2 ) ]], "function", Left], 
          Labeled[InputField[Dynamic[a]],"sequence", Left],
          Labeled[InputField[Dynamic[b]], "i=" , Left ], 
          Dynamic[If[ ( t - b ) < 11, 
             TableForm[ Transpose[ {Range[b, t], Table[ N[ a, 4 ], {n, b, t} ], 
               Table[ N[ f[ a ], 4], {n, b, t}]}], TableHeadings -> { 
     None, { "n", "\!\(\*SubscriptBox[\(a\), \(n\)]\)", 
      " f(\!\(\*SubscriptBox[\(a\), \(n\)]\))"}}], 
            TableForm[Transpose[ {Range[t - 10, t], 
               Table[ N[ a, 4 ], {n, t - 10, t} ], 
               Table[N[ f[ a ], 4], {n, t - 10, t}]}], TableHeadings -> { 
     None, { "n", "\!\(\*SubscriptBox[\(a\), \(n\)]\)", 
      " f(\!\(\*SubscriptBox[\(a\), \(n\)]\))"}}]]]}]], {{t, 1, "k"}, 1, 1000, 1, Appearance -> "Labeled"}]
share|improve this question

1 Answer 1

Here I will try to stay as close to your code and your stated goal as possible. There may be other ways to solve your problem, but for that you'd need to provide more context. I will use a somewhat simpler UI to make the code shorter. Here is one way to do what you need:

DynamicModule[{code = Hold[f[x_] :> (x^2 + 4)/(x + 2)]},
   With[{fn = (code = #; ReleaseHold[# /. RuleDelayed -> SetDelayed]) &}, 
     Manipulate[
        Column[{
         Labeled[InputField[Dynamic[code, fn], Hold[Expression]],"function", Left],
         f /@ Range[t]
        }], 
        {{t, 3, "k"}, 1, 10, 1, Appearance -> "Labeled"}, 
        Initialization :> fn[code]
     ]
   ]
]

What happens here is that we store the rule as a held expression in the input field. When the code for the function changes, we re-evaluate the function's definition. Storing the rule in Hold is needed so that the l.h.s. of the rule is not evaluated according to the definition of f, since a general pattern f[x_] is used.

EDIT

Per request of OP, here is the code for the exact case in question:

DynamicModule[{code = Hold[f[x_] :> (x^2 + 4)/(x + 2)], a = 1/n, b = 1},
  With[{fn = (code = #;ReleaseHold[# /. RuleDelayed -> SetDelayed]) &}, 
    Manipulate[
      Column[{
        Labeled[InputField[Dynamic[code, fn], Hold[Expression]],"function", Left],
        Labeled[InputField[Dynamic[a]], "sequence", Left], 
        Labeled[InputField[Dynamic[b]], "i=", Left], 
        Dynamic[
           If[(t - b) < 11, 
              TableForm[
                Transpose[{
                    Range[b, t], 
                    Table[N[a, 4], {n, b, t}], 
                    Table[N[f[a], 4], {n, b, t}]
                }], 
                TableHeadings -> {
                  None, 
                  {
                     "n", 
                     "\!\(\*SubscriptBox[\(a\), \(n\)]\)", 
                     " f(\!\(\*SubscriptBox[\(a\), \(n\)]\))"
                  }
                }
              ], 
              (* else *)
              TableForm[
                 Transpose[{
                     Range[t - 10, t], 
                     Table[N[a, 4], {n, t - 10, t}], 
                     Table[N[f[a], 4], {n, t - 10, t}]
                 }], 
                 TableHeadings -> {
                   None, 
                  {
                     "n", 
                     "\!\(\*SubscriptBox[\(a\), \(n\)]\)", 
                     " f(\!\(\*SubscriptBox[\(a\), \(n\)]\))"
                  }
                 }
              ]
           ]
        ] (* Dynamic *)
      }], 
      {{t, 3, "k"}, 1, 10, 1, Appearance -> "Labeled"}, 
      Initialization :> fn[code]]]]
share|improve this answer
    
I'm sorry, I was not clear before. I have considered your code and I am not sure how to incorporate it. This is an example of what I hope to achieve. A function input field appears with a default function, say (x^2 - 4)/(x - 2), below an input field to hold a default sequence say (2 + 1/n), and finally an input field that will determine at what natural number the sequence will start. Then I want the sequence of ((2+1/n)^2 -4)/((2+1/n)-2) to be computed and displayed. The goal is that a student can input some function and investigate its behavior for an some limit point with some sequence. –  RKennedy Sep 10 '12 at 16:37
    
@RKennedy Are you going to make a CDF out of it (or, in other words, is it imperative that Manipulate is the outermost function)? –  Leonid Shifrin Sep 10 '12 at 16:50
    
My students all have access to Mathematica so it is not essential that Manipulate be the outermost, but ultimately I want the choice to incorporate it into a cdf if the program seems to have real pedagogical value. –  RKennedy Sep 10 '12 at 17:38
    
@RKennedy I added the full code, but did not attempt to make Manipulate the outermost. This is an artificial security requirement imposed by CDF, and it makes life harder. If this becomes a problem, you can post a follow-up question, there are ways out. –  Leonid Shifrin Sep 10 '12 at 17:56
    
Leonid, Thanks for your help. I will study your solution. Also, I found your book and have begun reading through it. Thanks for leaving it open to all. I scheduled to attend a Mathematica intro at the end of the month. I've caught the bug. –  RKennedy Sep 10 '12 at 18:03

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.