Creating an equation library (Updated 03-FEB-2017)
|
01-09-2015, 09:51 PM
(This post was last modified: 03-09-2017 04:25 PM by Han.)
Post: #1
|
|||
|
|||
Creating an equation library (Updated 03-FEB-2017)
(This post has been edited; the original content of the first post is at the bottom of this post)
MARCH 2017: This project now exists as an actual app. Please see http://hpmuseum.org/forum/thread-7725.html ------------ Version 0.063 now makes use of the SVD package I wrote and posted here: http://www.hpmuseum.org/forum/thread-497...67743.html This version falls back on the SVD package I wrote for certain classes of matrices -- mostly those with singular values of 0. The Prime's implementation computes the left orthonormal vectors based on the reciprocal of the singular values. So when a singular value is zero, this results in undefined orthonormal vectors. There are still many errors that need to be trapped, but if you stall the SVD program in addition to the code below, you should be able to solve any system, now -- provided your guesses does not result in a point \( \mathbf{x} \) such that \( \mathbf{F}(\mathbf{x} )\) or its Jacobian is undefined. I will eventually write some documentation, but for now the solve is a simple Newton solver. Briefly, \[ \mathbf{F}(\mathbf{x}) = \left[ \begin{array}{c} f_1 (x_1, x_2, \dotsm, x_n)\\ f_2 (x_1, x_2, \dotsm, x_n) \\ \vdots \\ f_m (x_1, x_2, \dotsm, x_n) \end{array} \right] \] where the \( f_i \)'s are the equations set to 0. For example, \( x^2 + xy = 9 \) becomes \( f(x,y) = x^2+xy - 9 \). The linearization of \( \mathbf{F} \) is \[ \mathbf{F} (\mathbf{x}_{n+1} ) = \mathbf{F} (\mathbf{x}_n ) + J_\mathbf{F} \cdot \underbrace{(\mathbf{x}_{n+1} - \mathbf{x}_{n} ) }_{\Delta \mathbf{x}}\] where \( J_{\mathbf{F}} \) is the Jacobian (evaluated at \( \mathbf{x}_{n} \) ). The solver solves for \( \Delta \mathbf{x} \) by computing the pseudo inverse of \( J_\mathbf{F} \) -- which is exactly the inverse if the Jacobian if it is non-singular. I won't get too much SVD and pseudo-inverse until I write up the documentation. Code: ssVersion:="Equation Library 0.063 by Han Duong"; Original first post below -- info is out of date due to revisions ------------------------------------------------------------------------------------------- Here's a brief outline of my ideas. 1) Each category of equations will be a list of lists of the following form: Code: { Perhaps later on we can include graphics by means of lists of point definitions, line definitions, and polygon definitions. Ideally, the user would not have to specify what the variable list is, and the app will be smart enough to parse through each equation string to identify what the variables are. 2) A choose screen will allow users to select which category of equations to work with 3) Once a category is selected, all the equations will be shown in an INPUT() window with check boxes next to them. The user then checks the equations to use. 4) The app then creates another INPUT() window for variables and their initial values. Each variable may also be checked to specify whether the quantity should be treated as a variable that may change during the solve process, or if it is a constant (much like in the SOLVESYS library for the HP48 and HP49). 5) The equations are then modified to insert constants where appropriate, and then call fsolve() to solve the system. This is the general scheme. What would also be nice is an interface for users to add in new categories of equations. Anyway, this is something for a later update. For now, I'll start working on building the initial interface with the assumption that categories of equations already exist. This is where, if you want to contribute, you can help by posting the categories as shown in the format above. I'll start with a simple example: Code: { Graph 3D | QPI | SolveSys |
|||
01-09-2015, 09:56 PM
Post: #2
|
|||
|
|||
RE: Creating an equation library
Hi Han!
I like the way you've addressed the development of the library. I'll see your progress closely . A hug . |
|||
01-10-2015, 03:45 AM
(This post was last modified: 01-10-2015 04:07 AM by Han.)
Post: #3
|
|||
|
|||
RE: Creating an equation library
Here's a very basic skeleton. This version does not handle systems that have free variables. Ideally, in the case of free variables, then the initial guesses should be used to establish a solution. For example, if (x,y,z) = (x,2x-1,3x+1), then the initial guess of (4,5,6) should produce the solution (4,7,13) using x=4. This will be implemented over time. For now, what you get is basically a proof of concept.
Code: export eqldata; You must first create a Note named "EqLib" whose contents is: Code: { Run eqlib() and choose "Linear Motion" (the only option). The next screen allows you to pick which equations to use. Leave all selected. The screen after that allows you to set up the known values. Those which should be treated as constants can be checked so that they are not considered a variable in the system. Press OK. If a solution exists, the values are updated. Press cancel to quit. I chose x=1, x0=5, t=3, and a=9.8. This results in v0=-16.033333... and v=13.366666... Again, this is just a proof of concept. There are still a number of things I have to figure out about fsolve. Even when a system is solvable, if we give fsolve initial conditions, we sometimes get "Invalid dimension" errors. It appears that if fsolve is given more equations than the number of variables, it claims invalid dimension even though that may not be the case (e.g. some of the equations are equivalent). An alternative to fsolve would be to implement our own system solver using something similar to that in SOLVESYS for the HP48/HP49. A brief glance at the source code suggests a Newton-like algorithm using the Jacobian. Graph 3D | QPI | SolveSys |
|||
01-10-2015, 04:12 PM
Post: #4
|
|||
|
|||
RE: Creating an equation library (updated)
Hello Han,
thank you so much for working on an equation library. I tried to use it in the EMU. Everything went fine but i receive the "not supported" msgbox for any value assigned to the variables. Apart from this I would like to understand why you chose to store the equations in a note and not in a simple list. Which advantages you see in doing like this? Second question, why do you prefer to use fsolve() instead of programming the solve app? just to manage the overall process? Thanks Giancarlo |
|||
01-10-2015, 04:34 PM
(This post was last modified: 01-10-2015 04:43 PM by Han.)
Post: #5
|
|||
|
|||
RE: Creating an equation library (updated)
(01-10-2015 04:12 PM)Giancarlo Wrote: Hello Han, If the system and initial guesses result in anything other than a single solution, then the generic "not supported" message will appear. The current skeleton of an engine does not handle any case beyond the single-solution case (for the moment; that is not the plan for the future, of course). If you simply entered initial guesses without specifying any of the "variables" to be constants, then there would be free variables (since we have more variables than equations), resulting in a system with infinite solutions. As for why I stored the data in notes -- it's to prevent the erasure of data when one opens the source code of the program. Any variable associated with the program will be reset. Now, we could embed the data into the source code, but as you know, pressing [Shift][Esc] erases the entire program without any prompts. I don't know how many times I have cursed the virtual calculator or the real calculator because I didn't notice that the [Shift] operator was already active and accidentally erased my entire program. Saving it in the notes means we avoid this issue. Since the plan is to also implement an interface to add equations and manage the library, there is no real reason for any user to manually edit the corresponding notes (since it too has the same issue as programs with the [Shift][Esc]). Graph 3D | QPI | SolveSys |
|||
01-10-2015, 06:39 PM
(This post was last modified: 01-10-2015 06:50 PM by akmon.)
Post: #6
|
|||
|
|||
RE: Creating an equation library (updated)
Hello Han. Your Project is very interesting, and believe me, I was thinking of programming something similar to Solvesys. In my case using a process to extract the variables of the equations, avoiding to make a variable list, excepting if you deliberately insert a list explaining its purpose, like your example. So it´s a good idea to extract them automatically, as you wrote. Don´t know if there is a command like TVARS on HP 49 to get the variables of a formula.
By the way, I cannot find the solutions on you examples in my emulator. After inserting values a Box appears saying: no solutions or infinite solutions, and then time value returns to 0. I´ll try on my physical calculator. Another suggestion, the possibility to go back to the formula menu from the variable menu. And finally, fsolve Works better than solve App, let´s see if it behaves well with more examples. With this update (6975), I managed to solve a system of equations using fsolve where solve App couldn´t. |
|||
01-10-2015, 08:41 PM
(This post was last modified: 01-10-2015 08:56 PM by Han.)
Post: #7
|
|||
|
|||
RE: Creating an equation library (updated)
To obtain the answers I got, you must specify that x, x0, t, and a are constants (check box) next to the variable.
Edit: I see the issue; there is a priority conflict between local and CAS variables. This should be fixed relatively soon. Graph 3D | QPI | SolveSys |
|||
01-10-2015, 08:49 PM
Post: #8
|
|||
|
|||
RE: Creating an equation library (updated)
I did it, but no luck.
|
|||
01-10-2015, 09:43 PM
(This post was last modified: 01-10-2015 09:44 PM by Han.)
Post: #9
|
|||
|
|||
RE: Creating an equation library (updated)
Try this slight update:
Code: export eql_data; The issue was that the source code used the local variable 't' which also happens to be a variable that we declare as a CAS variable via indirection. Fortunately (or unfortunately depending how you view the separation of Home and CAS), the CAS variables seem to be completely separate. Therefore, we can represent the value of the variables as entries within a list we call eql_initvals. Then, by declaring variables (or purging) within the CAS and setting them equal to eql_initvals(i), we do not run into any collision between local and non-local variables. This scheme can be broken if for some reason we decide to create equations whose variables are also of the same name as those used within our source code! It is my hope, however, that the variable names I used are unique enough that they never appear in any useful equation that people use in real work :-) Graph 3D | QPI | SolveSys |
|||
01-10-2015, 10:49 PM
Post: #10
|
|||
|
|||
RE: Creating an equation library (updated)
Now it Works fine. Thank you for the new source code.
Another suggestion. Is it posible to add an equation directly on the program? I think I´ve left some clues to understand I am a big fan of SOLVESYS library, my mind is forcing to suggest your program to be as similar as posible to that one. My apologies. The purpose is to have a program that can read a note file with all equations (eqlib), or simply make a fast calculus inserting directly one or several equations for an specific problem (solvesys). All in one code. fsolve is better than solve App for numeric solution (a high percentaje of complex equations problems you just need the final numeric value). |
|||
01-10-2015, 11:07 PM
Post: #11
|
|||
|
|||
RE: Creating an equation library (updated)
(01-10-2015 10:49 PM)akmon Wrote: Now it Works fine. Thank you for the new source code. Yeah, the idea was to basically make a hybrid of SOLVESYS and the EQLIB from the HP48 days. Graph 3D | QPI | SolveSys |
|||
01-13-2015, 03:11 PM
Post: #12
|
|||
|
|||
RE: Creating an equation library (updated)
Hi Han,
That is an excellent prototype. I will follow progress closely. And you are correct, it would be great to be able to incorporate images in with the choose box... |
|||
01-13-2015, 04:21 PM
Post: #13
|
|||
|
|||
RE: Creating an equation library (updated)
The solver portion is done and behaves much like the SOLVESYS solver. The slight difference is that the solver falls back to fsolve in the case where there are more variables than there are equations (usually infinite solutions). In this case, the non-constant initial values are ignored. If there are more equations than variables, then a least squares approach will be used.
What I am working on now is the interface. Sadly, the CHOOSE() and MSGBOX() commands are just not sufficient for what I need. Moreover, they keep switching the view back to the command line view rather than just drawing directly onto the current view. Graph 3D | QPI | SolveSys |
|||
01-13-2015, 06:51 PM
Post: #14
|
|||
|
|||
RE: Creating an equation library (updated)
Fantastic, I´m waiting for the code to test some linear and no linear system of equations.
|
|||
01-13-2015, 11:12 PM
(This post was last modified: 01-13-2015 11:15 PM by Helge Gabert.)
Post: #15
|
|||
|
|||
RE: Creating an equation library (updated)
Nice project, Han!
Do you plan on having check boxes for variables that are allowed to be complex as well? (The user might want the solution(s) to be restricted to reals in some cases, but at other times not). |
|||
01-14-2015, 06:32 AM
Post: #16
|
|||
|
|||
RE: Creating an equation library (updated)
After some testing, I have run into a snag. Apparently the recent firmware changed how the command SVD outputs its results -- and the results themselves!
Graph 3D | QPI | SolveSys |
|||
01-14-2015, 05:08 PM
Post: #17
|
|||
|
|||
RE: Creating an equation library (updated)
I noticed that for arbitrary n x m matrices, the supposedly rectangular vector (the second element returned in the SVD output list), has dimension n if n<m, or m if n>m, so you can't just call diag(outputlist(2)) in order to reconstruct.
Is this the problem? The results, in general, seem to be OK, though. |
|||
01-14-2015, 05:45 PM
(This post was last modified: 01-14-2015 05:46 PM by Han.)
Post: #18
|
|||
|
|||
RE: Creating an equation library (updated)
No, that isn't the issue. Try this matrix:
SVD([[3,4.5,4.5],[4.5,12.25,1.25],[4.5,1.25,12.25]]); Just copy that line and paste it into the command line of the emulator. I get some "noise" (complex-valued matrices and vectors); see attached screenshot. The bug is also present in hardware (latest firmware) Graph 3D | QPI | SolveSys |
|||
01-14-2015, 09:03 PM
(This post was last modified: 01-14-2015 09:33 PM by Helge Gabert.)
Post: #19
|
|||
|
|||
RE: Creating an equation library (updated)
Yes I see what you are saying - the matrix is symmetric, and Wolfram alpha gives a purely real solution. Even with complex checked off in Home settings, and use i checked off in CAS settings it returns the complex elements in the U matrix.
But . . . presume the output is stored in L1, then, in CAS, L1(1)*diag(L1(2))*TRN(L1(3)) returns the original matrix - - so couldn't one argue that the complex SVD output is also correct? |
|||
01-14-2015, 11:03 PM
Post: #20
|
|||
|
|||
RE: Creating an equation library (updated)
(01-14-2015 09:03 PM)Helge Gabert Wrote: Yes I see what you are saying - the matrix is symmetric, and Wolfram alpha gives a purely real solution. Even with complex checked off in Home settings, and use i checked off in CAS settings it returns the complex elements in the U matrix. Interesting... I didn't even bother to check that particular calculation. What I don't understand is why there is even a complex component when the characteristic polynomial has real roots. Graph 3D | QPI | SolveSys |
|||
« Next Oldest | Next Newest »
|
User(s) browsing this thread: 2 Guest(s)