The Museum of HP Calculators

HP Forum Archive 21

 HP 15C LE extremums Message #1 Posted by Richard Berler on 19 May 2013, 8:25 p.m. I meant to say that I'm solving f(x)-f(x-.001)=0 with my methodology...is there a better way?

 Re: HP 15C LE extremums Message #2 Posted by Richard Berler on 19 May 2013, 8:30 p.m.,in response to message #1 by Richard Berler I thought my initial post posted (it didn't). Is this the best way to find extremums? This finds "x". I then need to have the original function available to plug the "x" in order to find f(x) at the extremum.

 Re: HP 15C LE extremums Message #3 Posted by Paul Dale on 19 May 2013, 10:14 p.m.,in response to message #2 by Richard Berler There are better ways to estimate a functions derivative at a point. The WP 34S uses a four, six or ten point method e.g. At the very least use (f(x+h) - f(x-h)) / (2h). - Pauli Edited: 21 May 2013, 7:51 a.m. after one or more responses were posted

 Re: HP 15C LE extremums Message #4 Posted by Marcus von Cube, Germany on 20 May 2013, 10:09 a.m.,in response to message #3 by Paul Dale if you just look for f'==0 you can save the division.

 Re: HP 15C LE extremums Message #5 Posted by Thomas Klemm on 20 May 2013, 10:17 a.m.,in response to message #3 by Paul Dale Your link to Wikipedia is broken.

 Re: HP 15C LE extremums Message #6 Posted by Paul Dale on 21 May 2013, 7:51 a.m.,in response to message #5 by Thomas Klemm Oops, fixed now. - Pauli

Re: HP 15C LE extremums
Message #7 Posted by Thomas Klemm on 20 May 2013, 10:12 a.m.,
in response to message #1 by Richard Berler

## Example

f(x) = sin(x)/x

Find the extremum between and 2.

```LBL 0
SIN
LSTx
/
RTN
```

```LBL 1
RCL 0
I
GSB 0
Re<>Im
RTN
```

## Solution

We use a small value h in register 0:
```0.00001
STO 0

ENTER
ENTER
+
SOLVE 1
4.49341
GSB 0
-0.21723
```

Kind regards
Thomas

Using calculus we end up with: x = tan(x). We can solve that with a simple fixed-point iteration. Just fill the stack with , enter 4 as starting guess and repeat the following steps:
```TAN-1
+
```
This list is the result:
```4.00000
4.46741
4.49218
4.49335
4.49341
```

Edited: 20 May 2013, 10:40 a.m.

 Re: HP 15C LE extremums Message #8 Posted by carey on 20 May 2013, 1:43 p.m.,in response to message #7 by Thomas Klemm There is a great little article (very readable) on the complex-step method of calculating numerical derivatives, entitled "Complex-Step Derivatives: How Did I Miss This?" by Michael Sherman (Stanford University): http://biomedicalcomputationreview.org/2/3/8.pdf Edited: 20 May 2013, 1:45 p.m.

 Re: HP 15C LE extremums Message #9 Posted by Richard Berler on 20 May 2013, 2:41 p.m.,in response to message #7 by Thomas Klemm That is neat! Your example worked great. Next post with article nice as well. I'm not getting the syntax for other examples, however. How would I enter as my LBL 0 f(x)= x^2 - 3*x -10 An extremum should be at x=1.5. f(x)=-12.25 Thanks!

 Re: HP 15C LE extremums Message #10 Posted by Thomas Klemm on 20 May 2013, 3:00 p.m.,in response to message #9 by Richard Berler f(x)= x^2 - 3*x -10 ```LBL 0 x2 LSTx 3 * - 10 - RTN ``` Cheers Thomas If you're missing the fact that the stack is filled with x you could use the following: ```LBL 1 RCL 0 I ENTER ENTER ENTER GSB 0 Re<>Im RTN ``` This makes using Horner's scheme to evaluate polynomials somewhat easier: ```LBL 0 3 - * 10 - RTN ``` Edited: 20 May 2013, 3:10 p.m.

 Re: HP 15C LE extremums Message #11 Posted by Richard Berler on 20 May 2013, 3:44 p.m.,in response to message #10 by Thomas Klemm Hi Thomas, When I enter your steps, I get 0 for solve 1 (and solve 0 incorrectly gives a root of 4 for intial guesses of 0,12 instead of 5). If I go: LBL 0 2 y^x x exchange y 3 * - 1 0 - RTN I get the correct root of 5 for solve 0 (solve f(x)=0) Solve 1 gives me 0 when using LBL 1 RCL 0 I GSB 0 Re exchange Im RTN where .00001 is stored in "0" Can you reproduce my experience? How do I get the correct results that you came up with?

 Re: HP 15C LE extremums Message #12 Posted by Richard Berler on 20 May 2013, 4:01 p.m.,in response to message #11 by Richard Berler still get 0 for solve 1 when I add RCL 0 / after Re exchange Im

 Re: HP 15C LE extremums Message #13 Posted by Dieter on 20 May 2013, 4:04 p.m.,in response to message #11 by Richard Berler First, please do not use 2 y^x when you want to get x^2. There is a dedicated x^2 key on virtually any scientific HP, which works much faster, preserves x in LastX and is even potentially more accurate. Your approach assumes that x fills the whole stack so that it can be recalled with x<>y in your LBL 0 routine. However, in LBL 1 it's this routine that provides the x-value, so you will have to enter a few ENTER commands that fill the stack before GSB 0 is called. Also the LBL 1 routine lacks the final division by h. Which usually (but not always) does not matter much if you want to solve for f'(x)=0, but I think you should add that division there. With these adjustments everything works fine. All this can be done shorter and faster if the function is coded the way Thomas and I proposed. Also see my detailled post below. ``` 0 ENTER 12 f SOLVE 0 => 5,0000 f(5) = 0 0 ENTER 5 f SOLVE 1 => 1,5000 f'(1,5) = 0 ``` Dieter

 Re: HP 15C LE extremums Message #14 Posted by Richard Berler on 20 May 2013, 4:22 p.m.,in response to message #13 by Dieter Blind as a bat re x^2 key. Last question...(and thanks for the last x discussion)...how do I implement that in this example...the HP 15C manual has a solve example: f(x)=5000*(1-e^(-x/20))-200*x using 0,12 a local max should occur at about x=4.4629,f(x)=107.4258

 Re: HP 15C LE extremums Message #15 Posted by Thomas Klemm on 20 May 2013, 6:04 p.m.,in response to message #14 by Richard Berler I've modified the expression a little: 5000*(1-e^(-x/20))-200*x = -200*(25*(e-x/20 - 1) + x) ```LBL 0 20 CHS / ex 1 - 25 * + 200 CHS * RTN ``` Be careful when using [CHS]: it changes only the real part of a complex number! Make sure to use it just with real constants as in -20. It took me some time to figure that out. ```4 ENTER 5 SOLVE 1 => 4.426871026 GSB 0 => 107.4257948 ``` Cheers Thomas Edited: 20 May 2013, 6:29 p.m.

 Re: HP 15C LE extremums Message #16 Posted by Dieter on 21 May 2013, 7:26 a.m.,in response to message #14 by Richard Berler Quote: how do I implement that in this example You do it the same way for each and every arbitrary function you come across. Just define your function at LBL 0 and leave the rest as it is (be sure to add those three ENTERs in the LBL 1 routine). So simply rewrite your new function at LBL 0 - that's all. If you have problems implementing simple functions in 15C code, please take a closer look at the basic chapters on programming in the 15C manual. A literal implementation of your last case may look like this: ``` LBL 0 ENTER ; may be omitted ENTER ; if the stack already ENTER ; is filled with x 2 0 CHS / e^x 1 x<>y - 5 EEX 3 x x<>y 2 0 0 x - RTN ``` Please note the already mentioned special behaviour of CHS with complex numbers, which is crucial here. Therefore CHS was only used to enter a negative real constant -20, but not for changing the sign of a computed result. ``` 0 ENTER 12 f SOLVE 1 => 4,4629 GSB 0 => 107,4258 ``` Dieter

 Re: HP 15C LE extremums Message #17 Posted by Thomas Klemm on 21 May 2013, 8:09 a.m.,in response to message #16 by Dieter Quote: 0 ENTER 12 And now I suddenly understand this: Quote: using 0,12 a local max should occur Quote: special behaviour of CHS with complex numbers, which is crucial here This doesn't happen with the HP-42S though. And if somebody is still in doubt why it would have been a good idea to provide complex variants for all functions of the HP-35S: that's why. You can still use this neat trick with polynomials but it fails with trigonometric functions or a simple square root. Cheers Thomas Edited: 21 May 2013, 8:41 a.m.

 Re: HP 15C LE extremums Message #18 Posted by Richard Berler on 21 May 2013, 11:01 a.m.,in response to message #17 by Thomas Klemm A most interesting discussion. Excellent! Amazing what happens when one (me) isn't afraid to uncover some lack of knowledge!

 Re: HP 15C LE extremums Message #19 Posted by Richard Berler on 21 May 2013, 12:06 p.m.,in response to message #18 by Richard Berler I'm dangerous...I get an error 8 when I try solve 1 on x^(3/5) * (4-x) GSB 0 works for values that should represent max, i.e. 1.5 which should yield a max of 3.1886. Any way around this? Thanks again!

 Re: HP 15C LE extremums Message #20 Posted by Thomas Klemm on 21 May 2013, 12:29 p.m.,in response to message #19 by Richard Berler ```LBL 0 .6 Y^X X<>Y 4 X<>Y - * RTN ``` ```1 ENTER 2 SOLVE 1 => 1.500000000 ``` ```GSB 0 => 3.188561254 ``` Works for me. Cheers Thomas

 Re: HP 15C LE extremums Message #21 Posted by Kiyoshi Akima on 21 May 2013, 1:58 p.m.,in response to message #20 by Thomas Klemm For what it's worth you can shave a step by replacing X<>Y 4 X<>Y with 4 RollUp.

 Re: HP 15C LE extremums Message #22 Posted by Thomas Klemm on 21 May 2013, 2:10 p.m.,in response to message #21 by Kiyoshi Akima It's always worth to save a byte here or there. Thanks for pointing that out. Best regards Thomas

 Re: HP 15C LE extremums Message #23 Posted by Dieter on 21 May 2013, 1:58 p.m.,in response to message #19 by Richard Berler Quote: I get an error 8 when I try solve 1 on x^(3/5) * (4-x) Then you must be doing something wrong. ``` LBL 0 4 x<>y - LstX , 6 y^x * RTN 0 ENTER 10 f SOLVE 1 => 1,5000 GSB 0 => 3,1886 ``` Where's the problem? Dieter

 Re: HP 15C LE extremums Message #24 Posted by Richard Berler on 21 May 2013, 4:05 p.m.,in response to message #23 by Dieter I was inefficient like a novice...but don't know why it works as subroutine 0, but generates error 8 on solve 1: LBL 1 enter enter enter CHS 4 + x exchange y 3 enter 5 / y^x * RTN

 Re: HP 15C LE extremums Message #25 Posted by Dieter on 21 May 2013, 4:42 p.m.,in response to message #24 by Richard Berler You forgot the special behaviour of the CHS function for complex values, as already discussed in this thread. So do not use CHS to change the sign of an already computed result in X, since it will change only the sign of the real part of X, but not that of the imaginary part! That's why your CHS after the three ENTERs is causing the problem here. Your implementation ``` CHS 4 + ``` will work fine for reals (as in GSB 0), but not for complex numbers (as used at LBL 1). You have to use ``` 4 x<>y - ``` here, and everything will work fine. Both for real and for complex values. Dieter Edited: 21 May 2013, 4:44 p.m.

 Re: HP 15C LE extremums Message #26 Posted by Richard Berler on 21 May 2013, 4:58 p.m.,in response to message #25 by Dieter Again, thanks, and thanks for the patience!

Re: HP 15C LE extremums
Message #27 Posted by Thomas Klemm on 21 May 2013, 7:53 p.m.,
in response to message #24 by Richard Berler

## Homework

1. Single-step through GSB 1 both with a value a little smaller and a little bigger than 1.5, (e.g. 1.49 and 1.51). Use the (i)-key to monitor the imaginary part of the intermediate results. Is there a change in the sign of the derivatives? How is that related to the Error 8 you encountered?

2. Repeat the steps above with Kiyoshi's solution:

```LBL 0
.6
Y^X
4
R^
-
*
RTN
```

3. Edit your post. Insert [pre] before and [/pre] after your listing. Remove the additional line-feed between the steps.

Kind regards
Thomas

PS: Bonus points if you succeed in writing yx instead of y^x.

 Re: HP 15C LE extremums Message #28 Posted by Thomas Klemm on 21 May 2013, 2:22 p.m.,in response to message #19 by Richard Berler Quote: I'm dangerous... Just wondered what your next example will be. Maybe something like Valentin's extremely flat maximum that is difficult to calculate numerically? I faintly remember a cos was involved but I might be wrong. Ok, found it in message #52 of hp 35s not very impressive: Quote: Numerically find a root of Sin(x + Cos(x)) = 1 for x in [0, Pi]. ```LBL 0 COS + SIN RTN ``` ```1 ENTER 2 SOLVE 1 => 1.569585997 ``` Quote: Of course the exact root is x = Pi/2 = 1.57079632679489661923132169163975144209858469968755291... Cheers Thomas PS: Marcus von Cube posted in message #67: Quote: I did some playing around with the function. It's not only flat at the root, it's damn flat: the first 5 derivatives of the function are all zero at the root. Edited: 21 May 2013, 8:37 p.m.

 Re: HP 15C LE extremums Message #29 Posted by Thomas Klemm on 20 May 2013, 4:14 p.m.,in response to message #11 by Richard Berler If you expect that the stack is filled with x, use this program instead: ```LBL 1 RCL 0 I ENTER ENTER ENTER GSB 0 Re<>Im RTN ``` HTH Thomas PS: You can use the [pre]-tag for listings: just click on the button [Preformatted] and REPLACE THIS TEXT WITH YOUR LISTING.

 Re: HP 15C LE extremums Message #30 Posted by Dieter on 20 May 2013, 3:26 p.m.,in response to message #9 by Richard Berler It's done exactly the same way as in the previous example. Define your function f(x) as usual. For instance like this: ``` LBL 0 x^2 LstX 3 x - 1 0 - RTN ``` Then simply call f(x) for your (real) x plus a small imaginary part h. Finally get the derivative by dividing the imaginary part of the result by h. First, store a suitable value for h in R0, e.g. 10-10: ``` 1 EEX 10 CHS STO 0 ``` Enter the routine for the first derivative: ``` LBL 1 RCL 0 ; now x holds the complex number x + h*i I ; and the 15C switches to complex mode GSB 0 ; simply call f(x) for x+hi Re<>Im ; Get the imaginary part of the result RCL/ 0 ; and divide it by h to obtain the derivative CF 8 ; optional clean-up: quit complex mode RTN ``` Now you can solve directly for f'(x)=0, using LBL 1 as the function to solve. Assume the minimum is somewhere between x = 0 and 5. ``` 0 ENTER 5 f SOLVE 1 => 1,5000 local minimum x GSB 0 => -12,2500 corresponding y ``` Dieter Edited: 20 May 2013, 3:33 p.m.

Go back to the main exhibit hall