Root-Finders in TK Solver
Here is a summary of the root-finders available in TK Solver, including their strengths and weaknesses. My goal is to help you choose the right tool for the task at hand. If you're just solving a problem once and need a quick solution, the built-in Iterative Solver is the best tool but if you're creating an application for repeated use, some other tools from the TK Library will be valuable.
Iterative Solver
The Iterative Solver uses a modified Newton-Raphson approach. It requires an initial guess. It cannot be bounded or controlled once engaged. It runs the entire model repeatedly until the solution is found or the processes diverges. It cannot be used on a subset of a model, such as within a procedure function loop. The initial guesses are programmable within TK. It can be run repeatedly at each element of a list solve.
Optimizer
The Optimizer uses a variety of methods. It is a close cousin to the Excel Solver add-in. It requires a Premium license. It uses the initial value as its guess. This initial value is not programmable within TK. It accepts bounds which may be functions of other variables. It runs the entire model repeatedly until the solution is found or the processes diverges. It cannot be used on a subset of a model, such as within a procedure function loop. It cannot be run repeatedly at each element of a list solve.
TK Library Functions
TK Library functions can be merged into models to solve subsets of equations such as within procedure loops. These complement the Iterative Solver and Optimizer.
Here is a listing of the root-finders in the TK Library.
The first batch process polynomial coefficients and return the roots.
quadr1 - solves quadratic equation for real roots
quadr2 - solves quadratic equation for real and complex roots
cubicg - solves cubic equations in general form
cubicnr - solves cubic equations in normal or reduced form
quartic - solves quartic equations for real and complex roots
Bairstow - solves polynomial equations for real and complex roots
The next group require that an error term be generated and returned by a function. For example, to solve the equation
Y = X*LN(X) + 20*X
for Y = 5, iteration would be required to find the value of X. Both the Iterative Solver and Optimizer could be used but what if Y will vary within a procedure function loop and solutions are required for each Y? Create a rule function with the following equation
Y = X*LN(X) + 20*X + ERROR
Make X the "argument" variable and ERROR the "result" variable. The value of Y will be passed in as a list element as it changes during the procedure loop. For example, the first element of a list called Ytemp could be used to store and retrieve the temporary value.
Y = 'Ytemp[1]
Now TK Library functions such as NewtonN can be used to determine the value of X that makes ERROR close to 0.
bisect - finds the point at which the sign of the error changes; Note that this may not be a root of the equation but could also be a point where the equation becomes undefined.
mbisect - finds a series of solutions within a range of values
refal - regula falsi (false position) method; Similar to bisect in that it searches for a sign change in the error and may return a root or a point where the equation becomes undefined
NewtonS - Newton's method, assuming that an equation for the derivative is known and available
NewtonN - Newton's method, with numeric differentiation
Newton2 - Newton's method with numeric differentation; functions of 2 unknowns
Newton3 - Newton's method with numeric differentation; functions of 3 unknowns
Newton4 - Newton's method with numeric differentation; functions of 4 unknowns
Newton5 - Newton's method with numeric differentation; functions of 5 unknowns
Newtonm - Newton's method with numeric differentation; functions of m unknowns; uses lists instead of variables to allow for an arbitrary number of unknowns.
Labels: TK models, TK Solver
Debugging a Complex TK Model
A customer sent me a TK model with 200+ lines on the variable sheet and 100+ lines on the rule sheet, plus several procedure functions called from the rules. The request was for help in getting the model to solve. When the iterative solver was launched, TK responded with the dreaded "Dependency Error" message.
That error indicates that when the iterative solver reached a certain point, it discovered that changes to one or more of the guess values no longer resulted in significant changes to the error terms it was trying to make zeros.
There are many different causes for this condition. The iterative solver may have started from poor guesses and diverged so far from the solution that the values have become extremely large. The error function may not be a smooth, continuous surface over a particular domain. There may not be a solution for the given set of inputs.
The key to diagnosing the problem is to overdefine the problem, changing the status of all the guess variables to inputs. (For this article, I will continue to refer to them as guess variables.) TK will detect an inconsistency and point to the offending rule. Edit in an error term. Repeat this process until you have new error variables associated with each of the guess variables. Make some changes to the guess variables and watch to see that the error terms update. It is sometimes useful to list solve using lists for each of the guess variables, one by one, as inputs and the error terms as outputs. In effect, this plots a slice of the solution hyperspace. What you're looking for is the sensitivity of each of the error terms to each of the guess variables. Also be on the lookout for ranges over which it appears an error doesn't seem to change. That would be a tar pit for the iterative solver if it stumbled into that range. In models with just a single guess variable, this plot will show you exactly where the solution lies.
Next, for models with two or more guess variables, make all but one of the error terms inputs of 0 and change the status of the associated guess variables to GUESS. The remaining error term should be an output and its associated guess variable should be an input. It may not be possible to get a solution under this scenario but if this works, it's a major breakthrough because now you can repeat the process over a range of values of the remaining guess variable to see where its associated error term becomes zero. In other words, get the model to where you can list solve over a single guess variable and you'll be able to get a plot of where that error term goes to zero.
In the case of the recent tech call, this process worked and unfortunately, it became clear that there was no feasible solution to the given problem. That is the error plot for the remaining variable never approached zero over the feasible range of inputs.
In some cases, the iterative solver requires extremely good guesses to find a solution and you might want to use the Optimizer as an alternative. The Optimizer is less reliant on the initial guesses and allows for adding bounds on the guess variables and constraints on related expressions. Assuming you have the error terms identified as described above, add a rule to the rule sheet to find the square root of the sum of the squared error terms. Then set up the Optimizer to minimize that value by changing the guess variable values within your desired bounds. If the Optimizer can't get the result down near 0, it's likely that there isn't a valid solution to the simultaneous equations.
In the case of the tech call model, the square root of the sum of squares of the three error terms was around 1000, so it wasn't even close.
Labels: iterative solver, TK models, TK Solver, variable sheet
Excerpts From a Recent TK Demo
I was over an hour into a demo for a group of six engineers and had covered most of the basics when I got the usual question from a Mathcad fan who was growing frustrated by all the enthusiasm the audience was showing for TK. “What if I want to type everything into a single document, including text and formulas, and have the solutions appear as I progress down the document?”
I gave my standard response. “It’s easy to create documents in Word, copying and pasting TK objects such as equations, tables, plots, and sections of the Variable Sheet into them as needed. You can dynamically link the TK objects so that when you change the values in the model, the document updates as well. TK also provides a built-in report writer that’s an efficient way to automatically dump most of the model into a report.”
The Mathcad fan just grew more agitated by this. “But what if I want to type the values directly into the document? Can I type the values into Word and have the TK model update?”
Again, I gave my standard response. “No. The Word document includes your text but the variables, tables, and plots come from the linked TK model.” And then the shocker. “You might try using Mathcad if you want to type values into the document…”
The Mathcad fan quickly chirped, “Exactly. That’s the way I need to work.”
I couldn’t resist. “Really? So what happens when you’ve typed up your Mathcad document and then you find out the problem changed slightly (in TK terms, anyway) and what had been a solution variable suddenly becomes a critical input?”
There’s a moment of silence at this point. I waited but eventually answered the question myself. “Time to start redoing your document. Mathcad documents flow from right to left and top to bottom. You need to completely restructure your document to handle the new scenario. Meanwhile, with TK Solver and Word, the report automatically updates when the TK model is backsolved. The equations haven’t changed. It’s just the sequence in solving them that changes and TK handles that automatically.”
Another long pause. It’s like he just learned that the Earth isn’t flat. I press on. “You probably already know how to use Word so there’s nothing to learn. And since most office PCs have Word installed, there’s usually no added cost.”
The Mathcad fan mustered a half-hearted response: “But I really like being able to type and see the formulas directly.”
“You can type and see the formulas directly in TK Solver and they transfer nicely over to Word. Best of all, you can type REAL EQUATIONS into TK without any additional programming. For example, what if you’re making a point that two ratios must be equal? In TK Solver, you just type an equation showing ratios on both sides of an equal sign. Mathcad works with sequences of assignment statements. You have to decide which variable you are going to solve for and isolate it to the left of an assignment statement. That’s just a lot of extra work.”
The rest of the audience encouraged the Mathcad fan to give it up.
I’ve often heard it said that 85% of the technical engineering work is done by 15% of the engineers and that the rest devolve into paper pushers. TK Solver encourages innovation and creativity while efficiently producing concise, effective, adaptable reports. In the end, Mathcad users could accomplish much more with TK.
Join the innovators. Go with TK.
Labels: mathcad, TK models, TK Solver
TK Solver Laws
The late Dr. Milos Konopasek, inventor of TK Solver, wrote a paper several years ago entitled TK Solver Laws. That paper and a related TK model file are available via the TK Model Share section of the UTS web site, in the Miscellaneous group. The paper has been modified a few times over the years to reflect some of the new features that were added to the program. Here is the introduction and a quick summary of the laws from that paper.
The purpose of the following material is to contribute to the collective understanding of the essence of TK Solver. It does not answer the question, “What is TK?”, and it does not attempt to catalog or explain TK techniques and tricks to any extent. It is a set of problem solving laws which are good to know, respect and live by.
The “laws” are supplemented with a sample model FRUSTUM in the Appendix. It deals with the frustum of a cone and it has 18 variables and 15+ rules. It is an extension of the cone model frequently used in TK Solver demos and training materials.
1. As many independent equations are needed as there are unknown variables to be solved for.
2. A set of TK rules equivalent to m independent equations linking n variables provides a framework for solving all feasible combinations of input and output variables.
3. A TK model can be solved for a certain set of unknowns by the Direct Solver if the reduced variable-equation-coincidence (VEC) matrix can be triangulized by reshuffling of rows and columns. Every diagonal element points to a variable and an equation resolvable in that variable.
4. Simultaneous equations break down the propagation of solution by the Direct Solver.
5. The simultaneous equations for TK’s Iterative Solver to be concerned with are those left after resolving the triangulized head and removing the reverse-triangulized tail of the model.
6. A set of simultaneous equations can always be solved by the Direct Solver, after assigning input values to some of the unknowns and dealing with ensuing inconsistencies, by editing error terms into affected rules.
7. There is always a minimum number of unknown variables to be assigned input (guess) values in order to make the set of simultaneous equations resolvable by the Direct Solver.
8. The number of guess variables may be reduced, or the use of the Iterative Solver avoided altogether for some output variables, by presolving the subsystems of simultaneous equations symbolically and adding redundant equations to the set of rules, or by using the techniques of local root finding or iteration.
9. The invertibility of the operations and functions is the name of the game with TK.
10. The lexical analysis (with VEC matrices and other tools) helps us to use TK efficiently. It may assist with, but it never substitutesfor, the necessary mathematical and subject-related analysis.
11. List solving or block solving is a way of performing a series of simple solutions automatically for a series of values of input variable(s); it should not be launched before simple solving works satisfactorily.
12. Respect and take advantage of the difference between values of variables (that are initialized at the beginning and cannot be overwritten during the solution process) and values of list elements (no automatic initialization, free to overwrite any time).
13. Beware of multiple solutions.
Labels: TK models, TK Solver