 # MathU Pro™ 4.2

## Solver Guide

 IMPORTANT: If you do not see any images and are running Windows XP you need to extract the files from the .zip archive before viewing. Open the .zip file and select "Extract All Files".

What is in this guide?

This guide contains detailed information about how to use the MathU Pro Solver, Equation Editor and Trace debugger. It also describes in more detail how to use the FSOLVE function within your own programs. See the MathU Pro Users Guide for information on how to use MathU Pro. See the MathU Pro Function and Programming Reference for detailed information about how to create your own MathU Pro programs.

### The MathU Pro Solver (a tutorial)

The MathU Pro Solver is an environment within MathU Pro that allows you to easily solve algebraic equations or expressions for one unknown out of many. Given the values of the other variables in an equation, the Solver uses numerical search to find a value of the remaining variable that satisfies the equation (if it exists). You can also use the Solver to find a zero of an expression.

In addition, because of the way the Solver works (by automatically generating a MathU Pro program), the Solver can be used instead as a really simple way to create a program that evaluates an expression of many variables. See the advanced topics section for more about this way to use the solver.

To make this concrete, consider the equation used to compute the percent total:

%T = (part / total) * 100

In this equation, there are three variables, %T, part, and total. Given the values of any two of them, we can solve for the third. Written as an expression, the equation looks like this:

(part / total) * 100 - %T

The original equation is satisfied when this expression is zero. In this form it is probably clearer that no single variable is special.

The first step in using the Solver is to enter the equation or expression into the Solver. Open the Solver via the Solver menu from the keypad or list view: or The Solver screen allows you enter a title for the equation as well as the equation (or expression) itself. To enter the equation, use a combination of graffiti or the keyboard buttons on the screen. The Fn (function) popup list contains a list of functions supported by the Solver. The number of arguments for each function is indicated in this list (e.g., cos takes one argument while atan2 takes two). Once the equation is entered, tap the Done button to convert the equation into a Solver program. If there is a problem with your equation (or expression), an error message will be displayed and the cursor will be positioned where the problem was detected.

If the Solver found no problems, you will be returned to either the keypad screen or the list view screen with your new solver program selected. As can be seen in this screen shot, the Solver creates buttons for each variable in the expression as well as a button that can be used to evaluate the expression (EXPR) and a button that resets all the variables to zero (RESET). Each variable function acts similar to one of the financial functions (like PMT):

• Entering a value via the keyboard and then executing the variable function stores the value into that variable (store mode)
• Executing the variable function with no input solves for the variable using the currently stored values in the other variables (calculation mode).
• STO <variable> stores the current value of X stack register into the variable.
• RCL <variable> recalls the current value of the variable into the X stack register.

To exercise our program, try this:    15. Store 15.0 into PART     20. Store 20.0 into TOTAL  %T = 75. Solve for %T  0. Check result (zero indicates a valid solution)

Once created, the program the solver creates is just like any other MathU Pro program. It shows up in the popup menu on the main screen and in the list view. It can be beamed, exported, and shared with colleagues.

To re-edit the equation, select "Edit Program" from the menu or tap the Edit button in the list screen.

The MathU Pro solver environment consists of the Solver equation editor, the Trace debugger, and the FSOLVE function. The Solver equation editor is used to enter or edit your solver equations. To start the Solver, select the Solver menu item from either the main keypad screen or the list view: or Once started you will see a screen like this: The buttons on this screen enter characters or functions into the equation field on the screen. Your equation can contain any number of variables (up to 24) and any of the functions in the popup Fn list.

#### Algebraic notation

Unlike the rest of MathU Pro, the Solver works with algebraic instead of Reverse Polish expressions. It is done this way because it is often easier to enter the equation for a problem than it is to create an RPN series of keystrokes that solves it. If you think it would be easier to work in RPN, take a look at the FSOLVE function below which allows you to tap into the power of the Solver within the RPN environment.

An algebraic expression is an infix expression (in contrast to a postfix RPN program). It is called infix because the operator appears between the variables in an expression. For instance the algebraic expression

a + b

means add the value of a and b. If you have used another programming language like BASIC, C, or Java you will be familiar with this type of expression. Here are few other examples:

m * x + b
%T = (part / total) * 100
sin(x)^2 + cos(x)^2

#### Variables

The variable names within an expression must start with a letter, the underscore '_' or the percent '%' character and can include upper and lower case ascii letters (A-Z and a-z), numbers (0-9), the underscore and the percent character. Examples are:

%T
ALPHA
m50
_Time2Go

The variable names you use are case sensitive and are used to create the button labels for the solver program. Short uppercase variable names are probably best since they make the best button labels.

#### Constants

Constants within an expression are written in scientific notation. Examples are:

25
3.14159
162.23e-4

When your number format preferences are set in the Palm OS Prefs application to use the comma as your decimal separator, you must enter numbers using that format. For example:

25
3,14159
162,23e-4

In this case, you must also use the colon ':' as the argument separator in functions like if(cond:then:else) or atan2(y:x).

#### Operators

The Solver supports the following operators:

Operator Example Description
+ + a Unary plus.
- - a Unary minus.
+ a + b Add a and b.
- a - b Subtract b from a.
* a * b Multiply a and b.
/ a / b Divide b into a.
^ a ^ b Raise a to the power of b (ab).
& a & b Logical AND of a and b.
True (1) if both a and b are non-zero. False (0) otherwise.
| a | b Logical OR of a and b.
True (1) if either a or b is non-zero. False (0) otherwise.
~ ~ a Logical NOT.
True (1) if a is zero. False (0) otherwise.
< a < b Less than.
True (1) if a is less than b. False (0) otherwise.
<= a <= b Less than or equal.
True (1) if a is less than or equal to b. False (0) otherwise.
> a > b Greater than.
True (1) if a is greater than b. False (0) otherwise.
>= a >= b Greater than or equal.
True (1) if a is greater than or equal to b. False (0) otherwise.
== a == b Numerically equal.
True (1) if a is exactly equal to b. False (0) otherwise.
Be careful when using this with non-integer numbers.
Round-off error can sometimes change a and b enough
that they won't match. Use abs(a-b) < tol instead.
~= a ~= b Numerically not equal.
True (1) if a is different from b. False (0) otherwise.
See comments for == regarding safe usage.
! a! Factorial of a. a must be an integer to avoid error. Use round(a)! if necessary.
( ) (a + b) - c Specify the order of operations.
( ) cos(theta) Enclose function arguments.
, atan2(y,x) Separate arguments within a function call.
= a + 1 = b - 1 Equality. Used to separate the left- and right-hand
sides of an equation

#### The importance of precedence

The order of operations within the Solver are governed by the common operator precedent rules. To make it easy to use, the Solver uses the same set of precedent rules as common programming languages like C and Java. So, for example, parentheses have the highest priority (they are evaluated first), followed by multiplication and then addition.

Because of this, the expression

a + b * c

is interpreted as

a + (b*c)

where parentheses have been added to explicitly indicate the order of operations. If you are not sure about the order of operations, you can always add parentheses to clarify the expression.

Here's the precedence order (highest to lowest):

( ) (parentheses)
unary +/-, ~ (logical not), function evaluation
^ (power) ! (factorial)
* /
+ -
< > <= >= == ~=
& (logical and)
| (logical or)
= (equality)
Operators that appear on the same line in this table are evaluated based on their order in the expression with the leftmost one being executed first (except for ^ which evaluates the rightmost first). Hence,
a / b / c is evaluated as (a / b) / c while
a + b ^ c ^ e is evaluated as a + b ^ (c ^ e).
Hopefully all of this is familiar and intuitive to you.

#### Functions supported by the Solver

The Solver supports the following functions:

 abs(x) Absolute value. acos(x) Arccosine. acosh(x) Hyperbolic arccosine. and(y,x) Bitwise AND. asin(x) Arcsine. asinh(x) Hyperbolic arcsine. atan(x) Arctangent. atan2(y,x) Arctangent of y/x that preserves the quadrant. atanh(x) Hyperbolic arctangent. cbrt(x) Cube root. ceil(x) Smallest integer greater than or equal to x. cos(x) Cosine. cosh(x) Hyperbolic cosine. exp(x) Exponential ex. expm1(x) Exponential ex-1. frac(x) Fractional part. floor(x) Largest integer less than or equal to x. hms(x) Convert from fractional hours to H.MS value. hours(x) Convert from H.MS value to fractional hours. int(x) Integer part. inv(x) Reciprocal 1/x. ln(x) Natural (base e) logarithm. ln1p(x) Natural (base e) logarithm ln(1+x). log(x) Common (base 10) logarithm. mod(y,x) Modulo remainder of y / x or(y,x) Bitwise OR. pi The value of pi pow10(x) Common antilogarithm 10x. rand Uniformly distributed random number between 0 and 1. round(x) Closest integer to x. secs Number of seconds since January 1, 1904. sin(x) Sine. sinh(x) Hyperbolic sine. sq(x) Square x2. sqrt(x) Square root. tan(x) Tangent. tanh(x) Hyperbolic tangent. xor(x) Bitwise XOR.

Note: When the comma is chosen as the decimal separator in the Palm OS Prefs application, use the colon instead of the comma to separate arguments within these function calls.

#### Making decisions: The IF function

In addition to those functions in the table above, the Solver supports the IF function

if(cond, then, else)

The first argument is the condition to be tested. The condition is normally arrived at using one of the logical test operators (like <, &, or ~=) but any non-zero value is considered to be true. When cond is nonzero (true), the expression in the second argument (then) is evaluated and returned as the value of the IF function. When cond is zero (false) , the expression in the second argument is evaluated and returned as the value of IF function.

Note: When the comma is chosen as the decimal separator in the Palm OS Prefs application, use the colon instead of the comma to separate arguments within the IF function.

Multiple IF functions can be nested. Suppose you wish to compute the commission rate for sales people based on their gross sales. A possible nested IF expression is

commission = if(sales < 1000, 0.10, if(sales < 5000, 0.15, 0.17))

In this case, sales less than \$1000 pay a 10% commission, sales less than \$5000 pay a 15% commission, and sales greater than or equal to \$5000 pay a 17% commission.

### Entering guesses

To enter a guess for the unknown variable, key the value into the X register and then execute the function for the variable. This stores the entered value into the variable. Execute the function again to begin the solve process. Example:    (store 25 into PART)  (solve for PART)

If you know two values of the unknown that bracket the root (that is the expression EXPR evaluated at the two values are of opposite sign), then the solver is guaranteed to find a solution and that solution will be between the two initial values. This is the best way to use the Solver if you want to make sure to find the solution you desire. To enter the two bracketing values, enter them into the X and Y registers and then execute the function for the variable. Execute the function again to begin the solve process. Example:       (store 25 and 32 into PART)  (solve for PART)

If there is a solution between 25 and 32, the solver will find it. If the expression evaluated at 25 and 32 is of the same sign, the solver automatically tries to bracket the root and may find a solution outside the specified range. This process can be viewed with the Trace debugger.

To recap: To enter a single guess, store the value into the variable and then start the solver. To enter two guesses that bracket the root, key the values into the X and Y registers, store them into the variable, and start the solver.

### Controlling the precision of the answer

The precision of the numerical result is controlled via the digits setting within MathU Pro. The search will stop when the unknown variable's value is within the display's precision of the correct answer (in scientific format). For example if the number if digits is set to 2, the result (if found) will be within 1/100 of the correct answer. Set the number of digits to ALL to produce a result within the precision of the calculator (about 16 digits).

### Using FSOLVE within your own programs

The Solver environment makes use of and introduces a new function to use with MathU Pro, FSOLVE. This function is the heart of the numerical solver and you can use it within your own programs to solve more complicated problems. The FSOLVE function operates by iteratively calling the subroutine you specify in the editor with different values of x until a solution is found.

You should write your subroutine to take a single value from the x register and replace it with the value of your expression much the way the SIN function works. For example, suppose you wish to find values of x such that sin(x) - cos(2*x) = 0. Your subroutine should look like this

label mysub
enter
sin
x<>y
2
*
cos
-
return

The value of your subroutine should be zero when the desired solution is found. When possible, you should try not to leave additional values on the stack.

FSOLVE always requires two guesses, which it takes from the x and y registers: It produces the value xo which will hopefully be the value of x where the expression is zero. Always verify that this is so before assuming you have a solution. When the two inputs to FSOLVE bracket the root, FSOLVE will always find a solution between the two values. When the two inputs do not bracket the root, FSOLVE tries to find a solution anyway near your guesses. Because of this you can enter a single guess by placing it into both the x and y registers before calling FSOLVE.

Here's a small program that uses the previous subroutine to find the angle x such that sin(x) - cos(2*x) = 0 and uses the value in x as a single guess:

label findit
enter
fsolve mysub
return

### Using the Trace Debugger

The Solver environment introduces a new type of debugger to MathU Pro. It is called the Trace debugger. Solver programs write information to the trace debugger screen during the solve process. Here's what the screen might look like during a typical debug session: The first column keeps track of the number of expression evaluations, the second column is the value of the unknown variable, and the third column is the value of the expression. The thrid column will be near zero when a solution is reached.

The example shown is for a single initial guess. The solver first tries to bracket the root. If successful, it switches into search mode to converge on the root. The screen shown above is in the middle of the process and convergence hasn't been reached yet.

To enable the Trace debugger, you must have a solver program selected on the keypad or in list view. Select Debug Program from the menu: You will be presented with an empty Trace screen: Tap ON to enable the trace debugger. Tap OFF to disable the debugger. As long as the debugger is enabled, the trace screen will display whenever a solution is being computed. While the trace screen is active, you can interrupt the solver by tapping on the screen.

#### How it works

The Solver uses numerical search to find a zero of your entered expression. Unfortunately, numerical search is not guaranteed to find a solution in every case.

Problems can occur in three situations: The expression may not have a zero solution, the solution may exist but it is numerically ill-conditioned, or a solution exists and the Solver may not be able to find a solution from a given starting point. The first problem is a generic one that can only be addressed by analyzing the problem at hand. The second problem may be addressed by rearranging the operations in the expression or otherwise changing the expression to make it better conditioned. The third problem can addressed by choosing a better initial guess.

The Solver is the most robust when the initial guesses bracket the zero. In this case, the Solver is guaranteed to find a solution. For all other situations, the solver first attempts to bracket the zero by searching near the initial guess. It will only search for so long however, so you may need to rerun the solver to continue the search. If no bracket is found, the error "Bad Guess" is displayed.

Once a suitable bracket is found, the Solver switches into search mode to converge on the zero. If the search doesn't converge (probably due to a numerical issues or a singular problem), the error "No convergence" is displayed.

If the expression produces an error during evaluation, the solver will stop with the error "Error in expr".

This whole process can be tracked via the Trace debugger.

#### Error messages

Error condition Possible cause Remedies
Bad guess Your guess(es) might not be close enough to a zero. To get a feel for your expression, try using the Trace debugger or enter values into the unknown variable by hand and then execute the EXPR function. Try to find a value where EXPR is positive and another where EXPR is negative. Use these two values as guesses (see Entering Guesses).
Bad guess Your expression might be ill-conditioned numerically. In this case, you might be losing valuable information as your expression evaluated due to round-off error and the limited range of double precision numbers. Try to rearrange your expression in order to avoid subtracting two numbers that are nearly equal, dividing a large number by a small number, or overflowing the range of a double precision number. Also try to avoid doing equality comparisons of non-integers. Try computing the expression by hand on the calculator to get a feel for the numbers involved and the likely locations of the problems.
Bad guess Your problem might not have a solution. Take a look at your problem and verify that you expect a solution to exist.
No Convergence Your problem is ill-conditioned or singular near the zero. Try to arrange your expression to avoid singularities near the zero or other numerical problems.
No Convergence Your problem is singular near the zero. Try a different guess or guesses to avoid the singular point.
Error in expression Evaluating the expression resulted in an error. Try evaluating the expression by hand to determine where in the expression the error is occurring. Places to look are functions like log and factorial (!) that produce errors outside their domain.
Singular The solver has converged onto a singular point in the equation. Check the answer via EXPR. If EXPR is not near zero, choose a different guess or guesses to avoid the singularity.
Stack overflow The expression has too many nesting levels. Rearrange the order of operations in the expression so as to evaluate the deepest levels first. Avoid using nested parentheses as much as possible.

#### Limitations

The Solver has the following limitations:

• The expression must have less than 24 variables.
• The expression must be able to be evaluated using less than 16 stack positions.

#### What does the automatically generated program look like?

The automatically generated program has the following structure:

label EXPR
<code to evaluate the expression>
return
label var1F
<helper function for use with FSOLVE to solve for var1>
return
label var1
<code to store, recall, and compute var1>
return
...
label varNF
<helper function for use with FSOLVE to solve for varN>
return
label varN
<code to store, recall, and compute varN>
return

The variable functions are assigned to the keys on the keyboard beginning with the top left and continuing row-wise until all shifted function locations are assigned. The EXPR function is assigned to the next position after that. The variable order is same as the order of the variables in the expression (from left to right).

You can export the automatically generated function to the memo pad to get a full listing. You can open the automatically generated program in the editor if you open it explicitly via the "Open In Editor" menu item within the Solver's equation editor. Changing the program will convert into a normal MathU Pro program and won't be able to edit it in the equation editor.

#### Using the Solver as a front end

As mentioned in the last section, it is possible to edit the automatically generated program using the normal MathU Pro editor. This differs from the approach taken by default within MathU Pro itself where the Solver is used to edit the equation not the underlying program.

To edit the solver program using the editor, open it first within the Solver's equation editor, then select Open In Editor from the Options->menu. If you make any changes to the program, the editor will save it out in a form that can no longer be edited by the Solver. It will still run, however.

This feature facilitates the use of the Solver as a front end that can automatically generate the initial functions within a larger program. Using the editor, you can add any additional functions you need. You might for instance compute a quantity based on the original variables in the equation once the solution is found. Or you might decide that some of the variables in the original equation are really constants so they do not need to be assigned to keys on the keyboard.

If you decide to take advantage of this advanced feature, keep in mind that once you change the program in the editor you can no longer go back to the Solver equation editor.