HP 15C LE extremums



#2

I meant to say that I'm solving f(x)-f(x-.001)=0 with my methodology...is there a better way?


#3

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.


#4

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


#5

if you just look for f'==0 you can save the division.

#6

Your link to Wikipedia is broken.


#7

Oops, fixed now.

- Pauli

#8

cf.Complex variable methods

Example


f(x) = sin(x)/x

Find the extremum between and 2.

The program for the function


LBL 0
SIN
LSTx
/
RTN

The program to calculate the derivative


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


Addendum


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.


#9

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.

#10

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!


#11

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.


#12

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?


#13

still get 0 for solve 1 when I add

RCL 0

/

after

Re exchange Im

#14

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


#15

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


#16

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.

#17

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

#18

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.


#19

A most interesting discussion. Excellent! Amazing what happens when one (me) isn't afraid to uncover some lack of knowledge!


#20

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!


#21

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


#22

For what it's worth you can shave a step by replacing X<>Y 4 X<>Y with 4 RollUp.


#23

It's always worth to save a byte here or there. Thanks for pointing that out.

Best regards

Thomas

#24

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


#25

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


#26

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.


#27

Again, thanks, and thanks for the patience!

#28

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.

#29

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.

#30

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.

#31

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

  1. Define your function f(x) as usual. For instance like this:
      LBL 0
    x^2
    LstX
    3
    x
    -
    1
    0
    -
    RTN
  2. 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
  3. 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.


Possibly Related Threads…
Thread Author Replies Views Last Post
  Extremums on Prime Richard Berler 2 1,243 10-13-2013, 01:21 PM
Last Post: Richard Berler
  OT - A lucky find - Busicom Handy-LE LE-120A Cristian Arezzini 2 1,864 09-26-2013, 04:43 AM
Last Post: Cristian Arezzini
  Low power warning for HP-15C LE and batteries Nick_S 1 1,428 09-16-2013, 09:34 AM
Last Post: Hans Brueggemann
  HP 35s derivatives/extremums Richard Berler 16 4,582 09-14-2013, 04:55 PM
Last Post: Matt Agajanian
  HP 15C-LE replacement still available? Borja 16 5,621 08-22-2013, 11:16 AM
Last Post: Michael de Estrada
  JTAG on HP-12C and HP-15C LE Ingo 5 2,492 07-01-2013, 06:37 PM
Last Post: Paul Dale
  New 15C LE bug? Paul Dale 3 1,840 02-05-2013, 09:27 PM
Last Post: Mike Morrow
  HP 15C LE, Program Display Format Control Uli 4 2,392 01-20-2013, 01:22 AM
Last Post: Ethan Conner
  HP-15C LE, wow! New owner euphoria. Sasu Mattila 16 4,391 09-07-2012, 06:55 PM
Last Post: lars Bergström
  HP 15c LE bugs Alexander Oestert 11 3,444 09-06-2012, 10:29 AM
Last Post: Peter Murphy (Livermore)

Forum Jump: