The Museum of HP Calculators

HP Forum Archive 17

[ Return to Index | Top of Index ]

Derivative on 35s
Message #1 Posted by Vincze on 23 Aug 2007, 2:16 p.m.

I know on HP 48gx, it fairly easy to find derivative of an equation where the slope equal 0 and determine if it the low point of curve or high point of curve. I know how to do manually, with chain rule, set to zero and solve, but is there easy way to have 35s do this?

      
Re: Derivative on 35s
Message #2 Posted by Peter Niessen on 23 Aug 2007, 2:56 p.m.,
in response to message #1 by Vincze

Doesn't the SOLVE set a flag when it hits a minimum/maximum instead of a root?

            
Re: Derivative on 35s
Message #3 Posted by Vincze on 23 Aug 2007, 3:11 p.m.,
in response to message #2 by Peter Niessen

I don't know. Are you saying, let it iterate until it hit zero?

            
Re: Derivative on 35s
Message #4 Posted by Peter Niessen on 23 Aug 2007, 5:23 p.m.,
in response to message #2 by Peter Niessen

Try solving for the root of, e.g. (x-2)^2+1:

LBL F
RCL X
2
-
x^2
1+
RTN

then

FN=F
SOLVE X

will give you "NO ROOT FOUND", but a 2 in the display.

Of course, as soon as your function has a root, this method will not work.

You probably could do (limited) symbolic derivatives using RPN, using the indirect array, since differentiating in RPN is easier than in ALG:

e.g. x^2

X x^2

can be diff'd by replacing x^2 by 2 *.

Addition and subtraction just stays the same (+ and -).

Product rule is a bit more advanced: You have to replace

X Y *

by

X Y' * X' Y * +

and have to evaluate X', Y' by recursion.

All what's left is to find a suitable representation of the symbols as numbers in the indirect registers, but that's a triviality :-)

Cheers, Peter.

Edited: 23 Aug 2007, 5:24 p.m.

      
Re: Derivative on 35s
Message #5 Posted by Gene Wright on 23 Aug 2007, 3:15 p.m.,
in response to message #1 by Vincze

Yes, but unfortunately, you have to put the derivative formula in the solver and solve for zero. :-)

            
Re: Derivative on 35s
Message #6 Posted by Vincze on 23 Aug 2007, 3:40 p.m.,
in response to message #5 by Gene Wright

So if dy/dx = 3x^3+4.2x^2 - 15, find 9x^2+8.4x and then set to 0 or 9x^2+8.4x = 0, and just enter that in solver? That easy enough...duh :) Been long week I guess.

What if you had a complex equation that not as simple as one above though, is there easy way to differentiate it and solve? Something like:

dy/dx = (sqrt(43x^3/52x+5)*654.2^2-54/458)/sqrt(675^2+98)

                  
Re: Derivative on 35s
Message #7 Posted by Gene Wright on 23 Aug 2007, 3:54 p.m.,
in response to message #6 by Vincze

If you can express the derivative as an equation on the 35s, then you can solve for a zero.

                        
Re: Derivative on 35s
Message #8 Posted by Vincze on 23 Aug 2007, 4:26 p.m.,
in response to message #7 by Gene Wright

No, I think you nem understand what I asking. Let me think about how to explain this better.

Only reason that we set to zero, is to find out if it minimum point or maximum point on curve. We must find 1st derivative first. Solving for zero is very easy, and I understand, but sometime finding first derivative is not so easy. Is there way that 35s can do that? Such as if I type in 3x^3+4.2x^2 - 15 it find 9x^2+8.4x is first derivative. Or if I type in (sqrt(43x^3/52x+5)*654.2^2-54/458)/sqrt(675^2+98) it tell me what 1st derivative is. It should return a formula, not a value. I guess I asking if it can differentiate.

                              
Re: Derivative on 35s
Message #9 Posted by Gene Wright on 23 Aug 2007, 4:28 p.m.,
in response to message #8 by Vincze

It cannot symbolically differentiate like you suggest. The graphing models with a CAS can, but not the 35s.

                                    
Re: Derivative on 35s
Message #10 Posted by Vincze on 23 Aug 2007, 4:37 p.m.,
in response to message #9 by Gene Wright

That not answer I was wish for my friend. :( Well, I have 48gx that can do it I guess, but I rather leave that calculator at home. I wonder how hard it would be to write program to differentiate? It would have to ask for variable you wish to differentiate. Hmmm, I must think about. I have feeling though it would not be easy to symbolically manipulate equation.

I wonder if our friend Valentin could do? He probably say I eat too much paprika and it going to my head and rusting brain cells.

                                          
Re: Derivative on 35s
Message #11 Posted by Dave Shaffer (Arizona) on 23 Aug 2007, 4:41 p.m.,
in response to message #10 by Vincze

You can always calculate the derivative numerically:

evaluate [f(x+dx) - f(x)]/dx

You may have to play with the size of dx, and there may be unstable regions, of course - just as we learned in Calc I .

                                                
Re: Derivative on 35s
Message #12 Posted by Vincze on 23 Aug 2007, 4:51 p.m.,
in response to message #11 by Dave Shaffer (Arizona)

Yes, but how would you do that on the 35s? I do not have my manual with me (Gene, when we going to have PDF of manual?) Honestly though, I prefer symbolic result as it show me more.

                                                      
Re: Derivative on 35s
Message #13 Posted by Stefan Vorkoetter on 23 Aug 2007, 5:18 p.m.,
in response to message #12 by Vincze

Quote:
Yes, but how would you do that on the 35s? I do not have my manual with me (Gene, when we going to have PDF of manual?) Honestly though, I prefer symbolic result as it show me more.

Then the HP 35s isn't the calculator you want. The 35s is purely numeric.

Stefan

                                                
Re: Derivative on 35s
Message #14 Posted by Stefan Vorkoetter on 23 Aug 2007, 5:16 p.m.,
in response to message #11 by Dave Shaffer (Arizona)

You can use this in conjunction with the solver then, solving a program instead of an equation. For example, to find the minimum/maximum of 3*x^2+2*x+1.

F001 LBL F
F002 EQN 3*X^2+2*X+1
F003 RTN

D001 LBL D D002 RCL D D003 STO+ X D004 XEQ F001 D005 x<>y D006 STO- X D007 x<>y D008 XEQ F D009 - D010 RCL/ D D011 RTN

Program F implements the function you want to find the minimum/maximum of.

Program D computes F'(x) using the approximation (F(x+dx)-F(x))/dx, where you pre-specify dx in register D.

Now use SOLVE:

FN= D
0.001
STO D
SOLVE X

This yields X=-0.3338.

You can now easily determine F(X) by just pressing:

XEQ F

Which yields F(X) is 0.6667.

Checking this symbolically, the derivative is 6x + 2, which is 0 for x=-1/3.

The difference in the answer is due to the approximation to the derivative. Using a smaller value in D will result in a more precise answer.

Stefan

                                                      
Re: Derivative on 35s
Message #15 Posted by Dave Shaffer (Arizona) on 23 Aug 2007, 6:25 p.m.,
in response to message #14 by Stefan Vorkoetter

Very slick. Thanks for implementing my suggestion!

                                                      
Re: Derivative on 35s
Message #16 Posted by Vincze on 24 Aug 2007, 8:30 a.m.,
in response to message #14 by Stefan Vorkoetter

That work to show you values, but in business you need to see the curve (or understand it). This used many time to solve for profit maximization of cost minimization curve. Your example yes tell me where point is (0.3338), and it also tell me that it is cost minimization curve from value 0.6667 which positive which mean that curve is increasing after zero.

I guess that could work if you need value. Okay, I was just hoping it could show symbolically some how too.

What I don't understand though is your formula. Any you can explain steps in D program.

                                                            
Re: Derivative on 35s
Message #17 Posted by Peter Niessen on 24 Aug 2007, 9:18 a.m.,
in response to message #16 by Vincze

Hi Vincze,

in the good old days, one would take a piece of paper, write down the registers

T
------------------------------
Z
------------------------------
Y
------------------------------
X
------------------------------
and the variables

D
-------------------------------
X
and fill in the values they assume after each program step. I hope you will find this instructive. Best of all, you can do it yourself immediately and don't have to wait for a reply from some forum.

Cheers, Peter.

Edited: 24 Aug 2007, 9:18 a.m.

                                                                  
Re: Derivative on 35s
Message #18 Posted by Vincze on 24 Aug 2007, 9:45 a.m.,
in response to message #17 by Peter Niessen

That very good tip, thank you my friend. I have question about two steps though. What is difference between D004 and D008? Do they not do same thing? If I understand correctly, after you hit SOLVE X, it go to line D001, which run through D program and call F program on line D004 and line D008?

Edited: 24 Aug 2007, 9:47 a.m.

                                                                        
Re: Derivative on 35s
Message #19 Posted by Howard Owen on 24 Aug 2007, 1:47 p.m.,
in response to message #18 by Vincze

If you key that into an HP35s, the D008 line will become 'XEQ F001' so yes, they are identical.

Regards,
Howard

                                                            
Re: Derivative on 35s
Message #20 Posted by Stefan Vorkoetter on 24 Aug 2007, 9:35 a.m.,
in response to message #16 by Vincze

The fact that F(x) > 0 where F'(x) = 0 doesn't mean that F(x) is increasing after zero.

Consider the curve, G(x) = -3*x^2-2*x+1. G'(x) = 0 at x=-1/3, G(-1/3) = 4/3. Here too G(x) > 0 where G'(x) = 0, yet G(x) decreases to either side of this point.

As far as the D program goes, here it is with comments:

D001 LBL D
D002 RCL D          
D003 STO+ X            // add value of D to value in X
D004 XEQ F001          // compute F(x+D)
D005 x<>y              // bring D back into the x stack register
D006 STO- X            // subtract from value in X to restore x
D007 x<>y              // bring F(x+D) back into x stack register
D008 XEQ F             // compute F(x)
D009 -                 // compute F(x+D) - F(x)
D010 RCL/ D            // compute (F(x+D) - F(x)) / D
D011 RTN

This could probably be made shorter, but I didn't want to make it too unclear.

Stefan

                                                                  
Re: Derivative on 35s
Message #21 Posted by Vincze on 24 Aug 2007, 9:52 a.m.,
in response to message #20 by Stefan Vorkoetter

Thank you my friend, this very interesting. I find it fascinating how effortlessly you conceptualize what calculator need to do and how calculator work. I still struggle with that.

Quote:
The fact that F(x) > 0 where F'(x) = 0 doesn't mean that F(x) is increasing after zero.
I beg to differ my friend. When you set to zero and solve if you get positive, it mean you find minimum point on curve, and if you find negative, it mean maximum point on curve. At least that how we learn in Hungary, but result is still the same

In your examples if you plot out you will see. In first example, we get positive result when we solve for x when set to zero and the plot show curve going up after point where x=0. In second example, when we set to zero and solve for x, we get negative number. When we plot, you see that curve going down after point where x=0.

Regardless of this minor error in analyzing result, your program is very good and I kindly thank you!

Edited: 24 Aug 2007, 10:04 a.m.

                                                                        
Re: Derivative on 35s
Message #22 Posted by Alain Mellan on 24 Aug 2007, 10:06 a.m.,
in response to message #21 by Vincze

As a good exercise: modify the program above to compute (F(x+d)-F(x-d))/2d

Numerical Recipes says it has better accuracy than (F(x+d)-F(x))/d

Edited: 24 Aug 2007, 10:06 a.m.

                                                                              
Re: Derivative on 35s
Message #23 Posted by Stefan Vorkoetter on 24 Aug 2007, 10:54 a.m.,
in response to message #22 by Alain Mellan

Tried that, and you're right, it works better. Intuitively this makes sense, because you're estimating the derivative based on two points on either side of x, instead of x and a point further along.

I had to add a fair bit of extra code to D to make it work though. Unfortunately, the routine one passes to SOLVE isn't allowed to mess up the value of the register one is solving for, hence there's code that has to put it back the way it was. The revised D program is:

LBL D
RCL D
STO+ X
XEQ F        // compute F(x+D)
x<>y
ENTER        // compute 2D
+
STO- X
x<>y
XEQ F        // compute F(x-D)
-
x<>y         // setup to divide by 2D

RCL D        // restore X to original x value
STO+ X
x<>y         // leave (F(x+D)-F(x-D))/2D on stack
RTN

Stefan

Edited: 24 Aug 2007, 11:00 a.m.

                                                                        
Re: Derivative on 35s
Message #24 Posted by Stefan Vorkoetter on 24 Aug 2007, 10:37 a.m.,
in response to message #21 by Vincze

Sorry, but you are mistaken. The following output is from the text version of Maple:

> g := -3*x^2-2*x+1;
                   2
          g := -3 x  - 2 x + 1

> g1 := diff(g,x); g1 := -6 x - 2

> solve(g1=0,x); -1/3

> subs(x=-1/3,g); 4/3

> plot(g,x=-1..0.4);

AAAA + AAA AAA + AA AA +1.2 AA A + A A+ AA * A +1 AA +AA A + AA AA +0.8 AA + A A + A AA +0.6 A A + A AA + A A +0.4 A A + A A + AA A +0.2 A A + A A + A *++++++++-++++++++++++++++++-++++++*++ + AA -1 -0.8 -0.6 -0.4 -0.2 + 0.2 0.4 + A +-0.2 A + A

The sign of G(x) for the value of x at which G'(x) = 0 has nothing to do with whether x is the location of a minimum or a maximum.

Stefan

                                                                              
Re: Derivative on 35s
Message #25 Posted by Pal G. on 24 Aug 2007, 2:06 p.m.,
in response to message #24 by Stefan Vorkoetter

If I lean back and squint, it looks like the display on my hp 50g.

Cheers, Pal

                                                                              
Re: Derivative on 35s
Message #26 Posted by Dave Shaffer (Arizona) on 24 Aug 2007, 6:28 p.m.,
in response to message #24 by Stefan Vorkoetter

Quote:
The sign of G(x) for the value of x at which G'(x) = 0 has nothing to do with whether x is the location of a minimum or a maximum

That's right. You have to either look at the curve, or, mathematically equivalently, take the second derivative and look at its sign.


[ Return to Index | Top of Index ]

Go back to the main exhibit hall