The routine returns the solutions in vlist order. Note that an external function strsub() is called. strsub()
must be in the same folder as solvemul(). See tip [8.2] for details.
To use solvemul() to solve our example, store two variables:
eqs1 is {a+b+c+1, 4*a+2*b+c+11, 9*a+3*b+c+29}
vars1 is {a,b,c}
then call solvemul() like this:
solvemul(eqs1,vars1)
and it returns
{-4, 2, 1}
which means that a = -4, b = 2 and c = 1, which happens to be the correct answer.
Again, there is no advantage to using this function to solve equations at the command line, or even
equations in a program, if the equations are known in advance. This type of program is necessary
when your application program generates the functions to be solved, and you don't necessarily know
what they are before you run the program. Further, solvemul() returns the solutions as a list, which
your program can use in further calculations, or display, as appropriate.
If you've read this tip list, word for word, up to this point, the first three parts of the function hold no
surprises. The two input lists are built into a string that can be used in solve(), and then expr() is used
to evaluate the string and solve for unknown variables.
However, the result of solve() is returned as a string that looks something like this:
"a=-4.0 and b = 2.0 and c=1.0"
The last parts of solvemul() convert this string to a list. First, I convert all the "and" occurrences to ":",
and delete all the spaces, for a string like this:
"a=-4.0:b = 2.0:c=1.0"
Next, I loop to search the string for the variables in vlist, and extract the values for these variables.
Using the ":" character as a delimiter makes this easier. Finally, I build the string of these result and
return it.
To use this routine, you have to be sure that the solution string consists of only a single numeric
solution for each unknown variable, so that the solution string does not have any "or"s in it. It would be
easy to add a test for this error condition, and return an error code, but my example doesn't show that.
Further, a real application should test to ensure that solve() really did return valid solutions.
[11.5] Using bounds and estimates with nsolve()
nsolve() is a function that is used to find one approximate real root of an equation. nsolve() is better
than solve() in some cases, because nsolve() returns a single number, rather than an expression such
11 - 4