![]() |
WP 34s: Trying to understand SLV - Printable Version +- HP Forums (https://archived.hpcalc.org/museumforum) +-- Forum: HP Museum Forums (https://archived.hpcalc.org/museumforum/forum-1.html) +--- Forum: Old HP Forum Archives (https://archived.hpcalc.org/museumforum/forum-2.html) +--- Thread: WP 34s: Trying to understand SLV (/thread-202586.html) |
WP 34s: Trying to understand SLV - Miguel Toro - 10-22-2011 Hi, I am having a hard time with SLV. I wanted to transfer the 15c Cash flow analysis to the 34s (Advanced functions handbook) but, even if the routine calculating NPV gives the expected result for every example I could find, when the same routine is called through SLV to solve IRR as with the 15C, the results are just off. Trying other examples from the 15c manual, I can see that the 34s solver sometimes should use different initial guesses to obtains result or not show a Solve failed message (page 184, section 13 on the original 15c owner handbook). What should I consider to make SLV to give better results? I really would like to port the CFA to the 34s. Thanks, Miguel
Thanks
Re: WP 34s: Trying to understand SLV - Paul Dale - 10-22-2011 The 34S solver is a completely different implementation to the 15c. For starters, it is using a quadraticly converging approximation which should converge much faster for many functions -- you'll see this in the 2nd best estimate which is often many digits less accurate unlike the same on the 15c. The 34S solver also solves to the display resolution -- so e.g. FIX 02 will give a very rough estimate, FIX 08 better etc. The 15c solver doesn't depend on the display settings. Conversely, the 34S integration doesn't depend on the display settings and the 15c's does.
Re: WP 34s: Trying to understand SLV - Miguel Toro - 10-23-2011 Hi Pauli, Is there a way to make this work with the 34s solver? This is an adaptation of the routine found on the 15c advanced functions handbook. The program works for NPV but, as I mentioned, the IRR does not get it right. For IRR there are two examples on page 43 and 44 that should yield 8.04 and 10.06 respectively. if, as written, this is not possible, is there another formula that I could use to calculate IRR? Thanks, Miguel
001 LBL'CFE' // Cash flows entry Re: WP 34s: Trying to understand SLV - Dieter - 10-23-2011 There are several reasons why your program does not work.
Change the program accordingly and it will work. Just tried it myself - it returns the same values as those in the advanced functions handbook. ;-) Dieter
Edited: 23 Oct 2011, 3:55 p.m. after one or more responses were posted
Re: WP 34s: Trying to understand SLV - Walter B - 10-23-2011 Buenas tardes Miguel, What spontaneously crossed my mind reading your original post: Why doesn't he use a financial calc for this? Honestly, we made the WP 34S (sic!) to be a scientific calc since HP's activities were a bit low in this area. Though I admit I voted for some %-functions and also a TVM solver many weeks ago, I'm most willing to kick those out if there will be any trouble with them. If you want a financial calc, get the 30b or 17bii+ Silver and you'll be done.
Walter
Re: WP 34s: Trying to understand SLV - Miguel Toro - 10-23-2011 Thank you very much Dieter, I will make the changes. Your detailed explanation helped me to better understand how SLV works. And a question: why not to publish yours as an article? Regards,
Miguel Edited: 23 Oct 2011, 3:58 p.m.
Re: WP 34s: Trying to understand SLV - Miguel Toro - 10-23-2011 Hola Walter Ah! But I want the one to control them all! Seriously, I always wanted the pocketable, simple calculator that can cope with finances as well as science and maths. And the 34s is what I am finding closest to this. Thanks,
Miguel
Re: WP 34s: Trying to understand SLV - Dieter - 10-23-2011 I think this a bit too special for an article, and all information can also be found in the manual (well, a bit hidden here and there as it relies on the reader to know how previous HPs worked ;-)). However, please note I made a final correction in the second code example, about the same time you wrote your reply. I assumed an error that in fact was not there. For the same reason point 4 (sign of returned NPV) is obsolete as well. #-) Dieter
Edited: 23 Oct 2011, 4:40 p.m.
Re: WP 34s: Trying to understand SLV - Dominic Richens - 10-23-2011 Dieter I think your example is a very illuminating demonstration of how SLV works. This is exactly the way that classic HP manuals explain some of the more complicated functions - take an example and implementing. Please consider adding a section to WP34s users' guide.
cheers!
Re: WP 34s: Trying to understand SLV - Paul Dale - 10-23-2011 Dieter, thanks for the insightful comments about this one.
Quote: I know of this problem, I haven't worked out how to fix it. The error is being thrown when the two estimates are equal when rounded but the function evaluation isn't rounding to zero. At least I think this is the cause. The problem is in the keystroke wrapper so should be fixable without using lots of flash.
Re: WP 34s: Trying to understand SLV - Dieter - 10-28-2011 I thinks this deserves a closer look. Imagine the solver has determined two successive guesses that agree in all displayed (or even all sixteen) digits. But does this also mean that the respective function result rounds to zero as well? Of course it doesn't, depending on the first derivative of the function.
Let us consider a very simple function like this in ALL mode: LBL 01So the solver should return sqrt(7) = 2,64... Let's see what it comes up with: 2 [ENTER] 3What happened here? The solution has a true value of 2,6457 51311 06459 05905... You see that the 16th significant digit is quite exactly right in the middle between 0 and 1. So the 16-digit result returned by the solver has to be ...064591 or maybe ...064590. It doesn't matter which of these two is returned - none of them will provide a function result that rounds to zero (which in this case I assume means "less than 5 E-17"). You either get 2 E-15 or -3 E-15. That's why solve will throw an error although in fact it has found the correct solution. There is some simple mathematics behind this: if the solution is varied by 1 ULP = 1 E-15 the function result will vary by as much as 2 * sqrt(7) * 1 E-15 = 5,3 E-15. So chances are there simply is no 16-digit value that will return a function result close enough to zero. Try the example modified to sqrt(2), with 1 and 2 as the initial guesses, and even in FIX 4 solve thinks it was unable to find a solution. Actually solve will almost always throw an error if the display mode is SCI or ENG. This seems to happen every time the function result is not exactly zero. Try the mentioned example in SCI 9 or even SCI 0 mode - you will always get an error message. That's why I think it does not make much sense if solve checks whether the function result rounds to zero or not. Even the best possible solution may return a result that is significantly different from zero. If you want to include an additional test one might think of varying the result by 1 ULP and see if the function result can be reduced this way. In the IRR example this leads to a slightly better result (in the last digit).
Dieter
Re: WP 34s: Trying to understand SLV - Dieter - 10-28-2011 For the record: here is your program with a few modifications. It is the same algorithm with a numeric improvement using ln1+x and e^x-1 as well as the changes discussed before. You may use this as a basic version for your own, better and even more reliable program. ;-) The result of the solver now is checked in three ways. Does the solver think it has found a solution? If not, do the last two guesses agree in their first six decimals? If not, is at least the NPV for this result less than half a cent? If not, throw an error, else display the result. Since I prefer the hotkeys A...D I used these instead of alpha labels. I also prefer to enter an interest rate and then simply press [B] instead of XEQ"NPV", then being prompted for the interest rate and finally get a result.
So here's my current, unfinished and experimental version. Use it at your own risk. BTW it's the first time I used the (dis)assembler - very nice. ;-) 001 LBL AHere's the example from the 15C Advanced Solutions Handbook p. 43: Keys DisplayThere are 8 cashflows in total. Now let's determine the NPV for an interest rate of 9%. 9 [B] NPV=Since the NPV is negative the IRR has to be less than 9%. What is its value? [C] IRR%=
Dieter
Re: WP 34s: Trying to understand SLV - Marcus von Cube, Germany - 10-28-2011 Dieter, it's still a good idea to have an alpha label at the beginning of the program so that it can be found in a flash region. Look at the vectors program in the library folder for an example.
Re: WP 34s: Trying to understand SLV - Neil Hamilton (Ottawa) - 10-28-2011 Quote: Thank you, Dieter! I appreciate the comment. Cheers...
Re: WP 34s: Trying to understand SLV - Miguel Toro - 10-28-2011 Thank you Dieter, I have already made the changes discussed before and was thinking about improving precision using ln1+x and e^x-1, but you were faster. :-) I really appreciate these improvements and the time you took to help. Regards,
Miguel Edited: 28 Oct 2011, 12:19 p.m.
Re: WP 34s: Trying to understand SLV - Miguel Toro - 10-28-2011 Hi Marcus, That was my original intention : to run this and other financial routines I am programing from a flash region. When they are ready, I will post them on an article, for whoever will be interested. Thanks to Dieter, I finally got how SLV works (it is really different from was I knew with the 35s or 32sII) and I am using the new knowledge a lot. :-) Regards,
Miguel Edited: 28 Oct 2011, 3:07 p.m.
Re: WP 34s: Trying to understand SLV - Paul Dale - 10-28-2011 I'm not seeing this one fail on the console emulator? What firmware revision are you using? Probably moot now, I've take the almost zero check out of the solver code. It won't fail anymore if the guesses converge. The user will have to check that a zero was found instead of a discontinuity.
Re: WP 34s: Trying to understand SLV - fhub - 10-29-2011 Quote:Looking at the previous and the current XROM code for the solver 'm now really confused! :-(
Here's the previous version: slv_fin:: RCL ZIn your todays change you've remove the 4 lines which I've marked with --- at the beginning.
But if I understand your previous version correctly, then if x[approx]0? it jumps to slv_found, but from there it jumps again to slv_fail_common (which gives an error). Isn't THIS the problem of the previous version? So I would say that the only wrong thing in your old solver version was, that this condition and the jump was wrong - or IOW: why does the label slv_found jump to slv_fail_common ???
Franz
Re: WP 34s: Trying to understand SLV - Paul Dale - 10-29-2011 I don't know what I wrote in the old code :-( It wasn't failing for the case Dieter presented but it wasn't correct either.
Re: WP 34s: Trying to understand SLV - fhub - 10-29-2011 Quote:Well, first I would say that the solver should indeed test if the function value is almost zero, and if not then maybe it could give an error message (something like "Dubios accuracy" or "Possible infinity"). Then I would prefer having the solution x in X and the function value f(x) in Y, so a simple x<->y would show if the solution is correct.
And finally I think you should only change your old XROM code as follows (i.e. swap the 2 JMP lines): slv_fin:: RCL ZFranz
Edited: 29 Oct 2011, 7:47 a.m.
Re: WP 34s: Trying to understand SLV - Dieter - 10-29-2011 I had used the emulator in version 2.2 1630. In the meantime the latest emulator version (wp34s-V22.zip) seems to be 2.2 1782 which I downloaded now. It returns the same results: P/R As mentioned, I think a final check for a near-zero-result is not a good idea at all. Take a look at the example. The best function results a 16-digit calculator can come up with are 2E-15 or -3E-15. Is this "approximately zero"? If you think so, consider the same function multiplied with 1E15, i.e. f(x) = 10^15*(x^2-7). In this case the best possible function results are 2 resp. -3. Which sure is not "approximately zero". ;-) BTW - the other example x^2 - 2 in FIX 4 fails as well in 2.2. 1782. There is a way to see whether the two last converging guesses actually are a valid solution. The result may be varied by 1 ULP until a sign change occurs. This is similar to the way Solve worked on the 34C and 15C: if the last guess did not return exactly zero they returned two final estimates with opposite signs of the function result, which were returned in Z and T as well. So the user was sure the actual solution had to be somewhere inbetween.
Dieter
Re: WP 34s: Trying to understand SLV - fhub - 10-29-2011 Quote:Or do it as I suggested: make this near-zero-check, but don't let the solver fail if it's not zero, but just display a 'warning' message instead (now we even have such a MSG command).
Franz
Re: WP 34s: Trying to understand SLV - Paul Dale - 10-29-2011 The solver near zero check was been removed in 1785 so you don't have it yet :-( In fact I'm pretty disillusioned with the approximately zero test. It seems to be almost useless -- rounding a near zero number generally doesn't change it.
Re: WP 34s: Trying to understand SLV - Walter B - 10-30-2011 Quote:May well be we know the reason now.
Walter
Re: WP 34s: Trying to understand SLV - Dieter - 10-30-2011 Fine, so I'll have the modified SLV version once the emulator gets an update. ;-) Quote:I wonder how this test is supposed to work. Is it the same as ROUND followed by X=0? In this case it will only work in FIX mode, since in all other cases the value in X is simply rounded to its displayed number of significant digits. Which never will get zero - unless the unrounded value already is.
Anyway, the test may be useful in programming ...once it has been carefully documented. In FIX n it actually tests if abs(x) is less than 0,5x10-n. Quote:You are right, I just checked both the 34C manual and the device itself. #-) However, I still think returning the two function results in Z and T is a good idea. Dieter
Edited: 30 Oct 2011, 10:55 a.m.
Re: WP 34s: Trying to understand SLV - Dieter - 11-01-2011 In the meantime I updated the emulator replacing wp34s.exe and wp34sgui.exe with their current versions (as of sunday evening UTC). VERS now shows 2.2 1789. Switching to program mode, 510 available steps are displayed instead of the usual 506. Is this correct? I then tested the same example (solve x^2 - 7) on this newer version. With the same initial guesses 2 and 3 in ALL mode it still throws the same error: Solve Fa1LEd, although the result is fine and X and Y agree in all digits. In FIX 11 the same result is returned without an error message.
Dieter
Re: WP 34s: Trying to understand SLV - Marcus von Cube, Germany - 11-01-2011 Don't rely on the current builds! I'm heavily working on the inner workings and many check-ins are just for safety or cross checking by my fellow developers.
If we are done solve and integrate can be nested to any depth, and unused program space can be used as data. Be patient!
Re: WP 34s: Trying to understand SLV - Walter B - 11-01-2011 From a user's point of view, we should close v2.2 then at a build number it was sufficiently stable. Then do all the mess in an incremented version.
Re: WP 34s: Trying to understand SLV - fhub - 11-01-2011 Quote:How true! Today (SVN 1804) I've compiled my program with wp34s_asm.pl and got "Illegal opcode" in the emulator (also SVN 1804). Looking at the program in the flash I saw commands like GTO without any address, or XEQ 1 (single digit) ...
BTW, what's this new LOCL and 200 instead of 100 for the address range? Franz
Edited: 1 Nov 2011, 1:14 p.m.
Re: WP 34s: Trying to understand SLV - Dieter - 11-01-2011 Some kind of "inaccurate" flag sure is a nice idea - but where is the limit? It depends on the function resp. its first derivative and the value of the root SLV has found. So I'd say there should be no final close-to-zero test. Instead a simple trick may be used: have the user round the function result to zero, if desired. At the same time this is a simple but efficient technique to cancel solve's iteration as soon as the desired accuracy level of the function result is reached. This idea was also used in the IRR example (function result < 0,005? => result ok). If the user really wants a final "approximately zero" check he may simply add X~~0? Clx as the final steps of his function.
Dieter
Re: WP 34s: Trying to understand SLV - Marcus von Cube, Germany - 11-01-2011 Send me your file and I'll try it. Read my posting above: I'm working so don't expect that the software is in a usable state. That's why we have a release package that should work.
LOCL is an abbreviation. :-)
Re: WP 34s: Trying to understand SLV - fhub - 11-01-2011 Quote:WOW, that's really a comprehensive explanation! And since I'm a clever guy I guess it's an abbreviation for LOCAL. ;-) Now I know what to do with it ... |