dynfunc ------- from http://www.ti.com/calc/docs/act/92dynfunc.htm ------------ TI-92 "The expr function gives you the ability to construct a Define statement from the user's input." Dynamic Function Definition Some applications need to be able to ask the user for a function at run time. The ability to define a new function from user input is called dynamic definition. The TI-92's expr function compiles a string, then executes it. This function gives you the ability to construct a Define statement from the user’s input. Installing the Program If you have the TI GRAPH LINKTM program, you can download the dynfunc() program described below and install it on your TI-92 calculator. Download dynfunc.92p program now Analyzing the Program The dynfunc() program uses dynamic definition to define a new function specified by the user. First, it searches for the first empty function in the Y= Editor. Next, it asks the user for an expression, and, finally, defines a function with the expression. Each time you run this program, the next empty yn(x)= function is defined. :dynfunc() :Prgm :Local prvmode,n,def,xpr :setMode("Exact/Approx","AUTO")prvmode : :© Search for first free Y= function :For n,1,99 : If getType(#("y"&string(n)))="NONE":Exit :EndFor : :© Create Define statement :"Define y"&string(n)&"(x)="def : © def contains "Define y1(x)=" : :falsedone :Loop : InputStr xpr © get expression from user : Try : expr(def&xpr) : © compile and execute Define statement : truedone : Else © come here if expr fails : Disp "Error: try again" : EndTry : If done:Exit :EndLoop : :setMode("Exact/Approx",prvmode) :EndPrgm The program begins by saving the current EXACT/APPROX mode setting in variable prvmode. (This mode setting is restored at the end of the program using setMode.) This program needs AUTO mode so that variable n will assume integer values in the For loop. In the For loop, the program tests successive variables y1 through y99 until it finds one that is undefined. The expression #("y"&string(n)) converts n into a string, concatenates it to "y", then uses string indirection (the # operator) to convert the string into a symbol name. Function getType looks up the type of the variable. In this case, it is searching for type "NONE", which means the variable is undefined. After finding an empty graph function variable, the program creates a Define statement and stores it as a string in variable def. The next section of the program gets the body of the function definition from the user. This is set up in a loop so that it keeps asking the user for an expression until it gets one that compiles without a syntax error. The program asks for the function body as a string from the user with the InputStr command. Next, it tacks the function header on the front of the expression string and tries to execute it. If expr successfully compiles the constructed Define statement, then a done flag is set and the program exits the loop. If expr fails because of a syntax error (or any error, for that matter), control passes to the Else branch of the Try block, an error message prompts the user to try again, and the program loops back to the InputStr statement.