# Using an InputField to define a function to compose with a sequence

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"}]


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}]
}],
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}]
}],
None,
{
"n",
"\!$$\*SubscriptBox[\(a$$, $$n$$]\)",
" f(\!$$\*SubscriptBox[\(a$$, $$n$$]\))"
}
}
]
]
] (* Dynamic *)
}],
{{t, 3, "k"}, 1, 10, 1, Appearance -> "Labeled"},
Initialization :> fn[code]]]]

• 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. Commented Sep 10, 2012 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)? Commented Sep 10, 2012 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. Commented Sep 10, 2012 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. Commented Sep 10, 2012 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. Commented Sep 10, 2012 at 18:03