HP 35s derivatives/extremums


The program for derivatives,extremums works beautifully on the HP 15C LE. I can't come up with something that works on the HP 35s as it handles complex numbers in a less complete manner ( or I'm just ignorant on this...). Any ideas?

Here's a great post by Dieter that explained how it works on the HP 15C LE:

It's done exactly the same way as in the previous example.

Define your function f(x) as usual. For instance like this:

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:
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


The original message can be found here (cf. message #30). You will also find a formatted version of the 15C code there.

This method cannot be used directly on the 35s since it does not offer a direct way to decompose a complex number in order to obtain its complex part, as described in the LBL 1 routine of the 15C code.

Which does not mean it cannot be done at all. ;-)



Which does not mean it cannot be done at all. ;-)

Well, it can be done. Yesterday I first tried the method suggested by Matt to isolate the imaginary part. But I found it was not sufficiently exact for complex numbers where the imaginary part is rather small. According to my results, the better idea is determining the real part first, and then subtracting it to get a complex number with a zero real part. This way I got the following program that can evaluate zeros as well as minima and maxima. The function is the one used in the earlier thread from last May:

 y = -200 * (25 * (e-x/20 - 1) + x)
So here's my crude and not much tested 35s code that illustrates how it may be done on that machine as well. Use at your own risk. ;-)
 F001 LBL F    ; define function f(x)
F002 RCL X ; use variable X
F003 -20
F004 /
F005 e^x
F006 1
F007 -
F008 25
F009 *
F010 RCL+ X
F011 -200
F012 *
F013 RTN ; return f(x)

D001 LBL D ; determine derivative f'(x)
D002 i
D003 RCL* H ; user supplies a suitable h beforehand, e.g. 1E-6
D004 RCL+ X ; x + ih
D005 x<> X ; store this in X
D006 STO Y ; save old X in Y
D007 XEQ F001 ; call f(x) with x + ih
D008 RCL Y
D009 STO X ; restore original X
D010 Rdn
D011 ENTER ; get complex result z = f(x+ih)
D013 ARG
D014 COS
D015 x<>y
D016 ABS
D017 * ; real part
D018 - ; get 0 + Im(z)i
D019 ABS ; |Im(z)|
D020 LastX
D021 ARG
D022 SGN
D023 * ; adjust sign of Im(z)
D024 RCL/ H ; f'(x) ~= Im(z)/h
D025 RTN

Now let's see what we get. The procedure is different from earlier HPs like the 34C or 15C since the 35s uses lettered variables for the solver.

First, provide an appropriate small value for h:

 1E-6  STO H    
Second, define the derivative D as the function to solve:
 FN= D
The local maximum is somewhere between 0 and 10:
 0 STO X  10
SOLVE X => X= 4,4629 ' local maximum at x = 4,4629
XEQ F ENTER => 107,4258 ' corresponding y

We may also try the other example of the original post:

 y = x2 - 3x - 10

F001 LBL F
F002 RCL X
F003 RCL* X ; avoid x^2 since it will not work in complex mode. Use x*x instead.
F004 3
F005 RCL* X
F006 -
F007 10
F008 -
F009 RTN

First, find a root between x = 0 and x =12.
 FN= F
0 STO X 12
SOLVE X => X= 5,0000
Find the negative root as well:
 0 STO X -12
SOLVE X => X= -2,0000
Now find the minimum between the two roots:
 FN= D
-2 STO X 5
SOLVE X => X= 1,5000 ; local minimum at x = 1,5
XEQ F ENTER => -12,2500 ; corresponding y
I am sure there are better and more reliable ways to obtain the imaginary part of a complex number. As usual, any suggestions are welcome.



Hi Dieter,

The F program works, but I get no roots found on both attempts of the D program with the exact keystrokes that you gave on both examples. Can you check the D program again to see if there was a typo or a missing step?



found it...in D, after D010, we need to insert x<>y as D011!


On all platforms...34s, 15C, 50g..., minimums that occur for x<0 seem problematic for derivative programs for equations such as x^(5/3) + 5*x^(2/3). I don't get the min at -2 on these calculators with built in programs (tabvar) on 50g, solving for f'(x)=0 on the 34, and the complex method on the 35s and 15C.


This function can be minimised if you use the cube root rather than yx directly.

        LBL A

f'(x) A

-3 ENTER -1.5 SLV B gives -2.000000000024174

The yx function is at fault here I suspect.

- Pauli


This function can be minimised if you use the cube root rather than yx directly.

That's not yet minimized. ;-)
   LBL A
RCL+ Z ; stack is filled with x
f'(x) A
3 ENTER -1.5 SLV B gives -2.000000000024184

No problem - a minor tranformation does the trick:

    x5/3 + 5 x2/3
= x2/3 * (x + 5)
= (x2)1/3 * (x + 5)
The current program version for the 35s returns the desired solution:
  Y001 LBL Y
Y002 RCL X
Y003 RCL* X
Y004 3
Y005 1/x
Y006 y^x
Y007 5
Y008 RCL+ X
Y009 *
Y010 RTN
0 [ENTER] -5
E= -2,0000
XEQ F [ENTER] => F= 4,7622
I assume this approach should also work with other calculators.


Edited: 15 Sept 2013, 11:03 a.m.


Richard, the version I posted works fine. Of course you may replace the "roll down" command in line D011 with a "x<>y", if you prefer. But the original program returns correct results as well. I just checked it on a 35s (real hardware, not an emulator).

After the function call XEQ F001 the stack's x-register holds the complex result f(x+ih). The next three steps only restore the original (real) X and drop the stack so that the x-register holds f(x+ih) again.



My mistake...I thought "rnd" was for the rounding function as opposed to roll down! Didn't know why that was necessary, and of course, the program was impacted adversely. It did make me feel some sort of accomplishment to diagnose what was missing :)


You are absolutely right - RND is the round function, but that's not what the code says. D011 is not RND, but RDN. ;-)

Finally, here's a complete solution for zeros and extremes of a function. This time I tried the 35s emulator. Compared to the previous version the handling is much nicer and more comfortable.

 Y001 LBL Y
Y002 RCL X
Y003 ...
Y004 < your function here >
Y005 ...
Ynnn RTN
F001 LBL F
F002 STO X
F003 XEQ Y001
F004 STO F
D001 LBL D
D002 STO X
D003 XEQ M001
D004 STO D
M001 LBL M
M002 RCL H
M003 x=0?
M004 1E-6
M005 STO H
M006 i
M007 *
M008 RCL+ X
M009 x<> X
M010 STO Y
M011 XEQ Y001
M012 x<> Y
M013 STO X
M014 RCL Y
M016 ARG
M017 COS
M018 RCL Y
M019 ABS
M020 *
M021 -
M022 ABS
M023 LASTx
M024 ARG
M025 SGN
M026 *
M027 RCL/ H
M028 RTN
Z001 LBL Z
Z002 -25 ; code for function Y
Z003 -26 ; code for variable Z
Z004 GTO E004
E001 LBL E
E002 -13 ; code for function M
E003 -5 ; code for variable E
E004 STO J
E005 Roll down
E006 STO I
E007 Roll down
E008 STO X
E009 x<>y
E010 FN=(I)
E012 RCL X
E013 STO(J)
E014 VIEW(J)
E015 RTN

And here's how to use it:

 0. Enter program
1. Provide function y = f(x) at LBL Y. Assume x is stored in variable X.
If extremes have to be found, be sure that all operators and functions are defined in the complex domain.
2. Evaluate f(x)
<x> XEQ F [ENTER] => F= f(x)
3. Evalute the derivative f'(x)
Optional: provide a value for h
<h> STO H
If h = 0 the program assumes h = 1E-6
<x> XEQ D [ENTER] => D= f'(x)
4. Find zeroes of f(x)
Provide two guesses x1 and x2
<x1> [ENTER] <x2> XEQ Z [ENTER] => Z= <x_root>
5. Find potential extremes of f(x)
Provide two guesses x1 and x2
<x1> [ENTER] <x2> XEQ E [ENTER] => E= <x_extr>
Check f(x) near x_extr to see whether it's a minimum, a maximum or a saddle point
<x> XEQ F [ENTER] => F= f(x)
Here's an example:
  y = x^3 - x^2 - x + 1/2
= ((x - 1)*x - 1)*x + 0,5
Y001 LBL Y
Y002 RCL X
Y003 1
Y004 -
Y005 RCL* X
Y006 1
Y007 -
Y008 RCL* X
Y009 0,5
Y010 +
Y011 RTN
Assume h = 1E-6
1E-6 STO H

Evaluate f(x)
-3 XEQ F [ENTER] => F= -32,5000
-2 XEQ F [ENTER] => F= -9,5000
-1 XEQ F [ENTER] => F= -0,5000
0 XEQ F [ENTER] => F= 0,5000
1 XEQ F [ENTER] => F= -0,5000
2 XEQ F [ENTER] => F= 2,5000
3 XEQ F [ENTER] => F= 15,5000
Find the three real roots in x = -1...0, x = 0...1 and x = 1...2
-1 [ENTER] 0 XEQ Z [ENTER] => SOLVING... Z= -0,8546
0 [ENTER] 1 XEQ Z [ENTER] => SOLVING... Z= 0,4030
1 [ENTER] 2 XEQ Z [ENTER] => SOLVING... Z= 1,4516
Find the two extremes between the roots
-0,9 [ENTER] 0,4 XEQ E [ENTER] => SOLVING... E= -0,3333
Check the derivative
XEQ D [ENTER] => D= -2,5560 E-12
-0,3 XEQ F [ENTER] => F= 0,6830
-0,3333 XEQ F [ENTER] => F= 0,6852
-0,4 XEQ F [ENTER] => F= 0,6760
So there is a maximum at x = -0,3333 and f(x) = 0,6852
Find the second extreme:
0,4 [ENTER] 1,5 XEQ E [ENTER] => SOLVING... E= 1,0000
Check the derivative
XEQ D [ENTER] => D= 0,0000
1 XEQ F [ENTER] => F= -0,5000
So there must be a minimum at x = 1 and f(x) = -0,5

And finally here's the function used in the May 2013 thread:
 Y001 LBL Y
Y002 RCL X
Y003 -20
Y004 /
Y005 e^x
Y006 1
Y007 -
Y008 25
Y009 *
Y010 RCL+ X
Y011 -200
Y012 *
Y013 RTN

0 [ENTER] 12 XEQ E [ENTER] => SOLVING... E= 4,4629
XEQ F [ENTER] => F= 107,4258
4,4 XEQ F [ENTER] => F= 107,4060
4,5 XEQ F [ENTER] => F= 107,4189
=> maximum at x = 4,4629 and f(x) = 107,4258

Again, this program is a quick and dirty version that may have errors and other flaws. Use at your own risk, improve as you like and don't forget to share your improvements here. ;-)


Edited: 15 Sept 2013, 10:07 a.m.


A while ago I devised methods to decompose polar/rectangular values on the 35S. Yup!!!! And, to verify it's working corectly, I tried it with several examples from HP and TI manuals.

Since it worked so well, I revised the method to decompose complex mumbers. And yes, I ran that algorithm several times with several examples and yes, my decomposition for complex mumbers procedure worked every time.

Here it is:

If a+bi is in X register, then

* (X, multiply operator)
=== Imaginary part is in X-Register

* (X, multiply operator)
=== Real part is in X-Register

Edited: 12 Sept 2013, 7:00 p.m.



Now I have an HP 35s that can do arc trig and hyperbolic trig functions of complex numbers from earlier programming, and now can find extremums of a function by solving f'(x)=0!



You're welcome. My pleasure. I'm glad that these can help. And, as you can probably tell, you could extract the real part first.

Edited: 12 Sept 2013, 11:56 p.m.


This community created the ultimate rectangular / polar routines soon after the 35S was released. They preserve the unused stack, set lastX correctly and operate independent of the equation prompting flag.

You'd need some modifications to deal with complex numbers directly but they are a good basis for such work.

- Pauli


Actually, although that fantastic routine works very well for polar/rectangular conversions in the usual two-register stack form, it doesn't decompose X-Register a+bi formed complex numbers on the 35S.

Edited: 14 Sept 2013, 4:57 p.m.

Possibly Related Threads...
Thread Author Replies Views Last Post
  HP prime: derivatives Alberto Candel 9 1,185 11-29-2013, 06:45 PM
Last Post: Alberto Candel
  Extremums on Prime Richard Berler 2 499 10-13-2013, 01:21 PM
Last Post: Richard Berler
  HP 15C LE extremums Richard Berler 29 3,694 05-20-2013, 03:26 PM
Last Post: Dieter
  Derivation of (HP) Derivatives... Matt Agajanian 6 891 05-25-2012, 09:46 PM
Last Post: Crawl
  Finding extremums of function with complex derivate Lukas K. 7 932 10-17-2011, 06:06 AM
Last Post: bunuel66
  derivatives and indefinate integrals JeffG 3 517 03-15-2002, 10:23 AM
Last Post: Thibaut.be
  38G Derivatives and Integrals Brandon 0 360 10-15-1999, 11:24 PM
Last Post: Brandon

Forum Jump: