Derivatives on HP 42S
|
08-20-2018, 03:03 AM
Post: #1
|
|||
|
|||
Derivatives on HP 42S
Awhile back, a gem of a way to produce d/dx using the complex number capabilities of the HP 15LE was posted. It worked wonderfully, and could be used in solve to find extremums. I am stumbling over how to implement this on my HP 42S.
I attached the image of a sample program for the HP 15LE to take d/dx of (sin x)/x and the use of it in solve. How should I accomplish this on the HP 42S? |
|||
08-20-2018, 04:38 AM
Post: #2
|
|||
|
|||
RE: Derivatives on HP 42S
I've inlined the function:
Code: LBL "dF" Set MODES to RAD. Start SOLVER. Select Solve Program: dF Set small value h: 1E-5 h Set lower guess: π x Set upper guess: π 2 × x Solve for x: x x=4.49341 Kind regards Thomas PS: Link to original post |
|||
08-20-2018, 07:43 AM
Post: #3
|
|||
|
|||
RE: Derivatives on HP 42S
Here's another example:
\(f(x)=\sin(x)^{e^x}\) Search for \(\max(f(x))\) for \(x\in[14.13, 14.14]\). Code: LBL "dF" Set MODES to RAD. Start SOLVER. Select Solve Program: dF Set small value h: 1E-6 h Set lower guess: 14.13 x Set upper guess: 14.14 x Solve for x: x x=14.137167 Cheers Thomas |
|||
08-20-2018, 10:57 PM
Post: #4
|
|||
|
|||
RE: Derivatives on HP 42S
Hi Thomas,
When I try to XEQ the function in it's own little program, say "FX" within the derivative program sandwiched between the 2 complex commands, the correct answer is returned with a minute complex component. (I wish to have the function that I am finding the extremum point as it's own program that the derivative program can call...this allows me to then run the function program to evaluate f(x) of the extremum) I can then do a switch of x and y stack levels, but was wondering why I cannot simply get the real part of the extremum to be delivered to the x stack level. Richard |
|||
08-20-2018, 11:43 PM
Post: #5
|
|||
|
|||
RE: Derivatives on HP 42S
Not sure if I understand you correctly but you can split the program into:
Function Code: LBL "Fx" Derivative Code: LBL "dF" Set MODES to RAD. Start SOLVER. Select Solve Program: dF Set small value h: 1E-6 h Set lower guess: π x Set upper guess: π 2 × x Solve for x: x x=4.493409 Exit the solver with: EXIT EXIT Now you can run: XEQ "Fx" x: -0.217234 Is this what you wanted to achieve? Kind regards Thomas |
|||
08-20-2018, 11:54 PM
Post: #6
|
|||
|
|||
RE: Derivatives on HP 42S
(08-20-2018 07:43 AM)Thomas Klemm Wrote: Here's another example: Why would search for slope of 0 get the maximum ? Should it get the minimum, at x = 14.13 ? Slope at 14.13 = 5.2e-12 Also, Why use complex numbers to search real extremums ? |
|||
08-21-2018, 12:34 AM
(This post was last modified: 08-21-2018 12:47 AM by Thomas Klemm.)
Post: #7
|
|||
|
|||
RE: Derivatives on HP 42S
(08-20-2018 11:54 PM)Albert Chan Wrote: Also, Why use complex numbers to search real extremums ? From The Complex-Step Derivative Approximation: Quote:For a small discrete h, this can be approximated by Since we search for stationary points we set: \(\frac{\partial f}{\partial x}=0\) Which we approximate with just: \(\Im[f(x + ih)]=0\) Cheers Thomas This is why it's a pain point if a calculator claims to support complex numbers but does not implement all functions. Yes, I'm looking at you HP-35S! |
|||
08-21-2018, 01:35 AM
Post: #8
|
|||
|
|||
RE: Derivatives on HP 42S
(08-20-2018 11:54 PM)Albert Chan Wrote: Why would search for slope of 0 get the maximum ? Cf. Maxima and minima We search for a critical point in the interval [14.13,14.14], that is x where f'(x) = 0. Quote:Should it get the minimum, at x = 14.13 ? The function is somewhat pathological in that it's close to 0 most of the time and only 1 at the maximum which is where \(\sin(x)=1\). That means \(x=\frac{\pi}{2}+k\cdot2\pi\) for \(k\in\mathbb{Z}\). It's not defined (or then takes complex values) where \(\sin(x)<0\). Thus the minimal value is 0 when \(x=k\cdot\pi\) for \(k\in\mathbb{Z}\). So the minimal values in that section are \((4\pi, 0)\) and \((5\pi, 0)\) and the maximal value is \((\frac{9}{2}\pi, 1)\). The flatness of the function makes it hard to numerically find the stationary points. Cheers Thomas Since I used Free42 instead of the HP-42S it could very well be that the results don't agree. |
|||
08-21-2018, 02:24 AM
Post: #9
|
|||
|
|||
RE: Derivatives on HP 42S
Yes, it becomes very hard to find the critical points as one gets further away from 0: f(x) approaches it's maximum of 1 over smaller and smaller intervals, requiring an impossibly tight interval to enter into the search far from zero!
Your program was just what I wanted. I was very close, but your program was what I was after. Thanks again...never was exposed to the relationship between derivatives and complex numbers before you posted several years ago! Richard |
|||
08-21-2018, 06:14 AM
Post: #10
|
|||
|
|||
RE: Derivatives on HP 42S
(08-21-2018 01:35 AM)Thomas Klemm Wrote: The flatness of the function makes it hard to numerically find the stationary points. Here's another example: \(f(x)=\sin(x+\cos(x))\) This is the power series expansion for \(f\) about the point \(x=\frac{\pi}{2}\): \(1 - \frac{1}{72} (x - \frac{\pi}{2})^6 + \frac{1}{720} (x - \frac{\pi}{2})^8 + O((x - \frac{\pi}{2})^{10})\) You should start with the initial values 1 and 2. Kind regards Thomas |
|||
08-23-2018, 12:58 PM
Post: #11
|
|||
|
|||
RE: Derivatives on HP 42S
a bit out of topic, but here it goes: Big thank you, Thomas, for your posts here, I really enjoy reading them, so enlightening they are!
Cheers, |
|||
08-24-2018, 02:51 AM
Post: #12
|
|||
|
|||
RE: Derivatives on HP 42S
Hi Thomas,
When trying a more complicated equation: x^(4/3) + 4(x^(1/3)), I get a correct f(x) for all values of x (x=-6, f(x)=3.63, x=-4, f(x)=0 x=0, f(x)=0 x=2, f(x)=7.56) Instead of a min at x=-1, f(x)= -3, my dx program comes up with a min at x=-3. I know that this is probably not a concise program for FX, but it does work for this example. Why is DX not working? LBL FX STO X 4 Y^X 3 1/X Y^X STO B RCL X SIGN STO S RCL X * 3 1/X Y^X 4 * ABS RCL S * RCL B + END LBL DX MVAR A MVAR H RCL A RCL H COMPLEX XEQ FX COMPLEX END Thanks for your time! |
|||
08-24-2018, 05:52 AM
(This post was last modified: 08-24-2018 05:55 AM by Thomas Klemm.)
Post: #13
|
|||
|
|||
RE: Derivatives on HP 42S
(08-24-2018 02:51 AM)lrdheat Wrote: When trying a more complicated equation: x^(4/3) + 4(x^(1/3)), I get a correct f(x) for all values of x (x=-6, f(x)=3.63, x=-4, f(x)=0 x=0, f(x)=0 x=2, f(x)=7.56) The function can be written as \(y=\sqrt[3]{x}(x+4)\) Even though you might think otherwise the domain of this function is \(\{x\in\mathbb{R}:x\geqslant0\}\) The problem with the cubic root of negative numbers is which value to choose as the principal value. The advantage of choosing the negative, real value is that you don't have to deal with complex numbers. That's probably the reason for this schoolbook definition. The problem however is that this choice is not continuous: as soon as the exponent is slightly off from \(\frac{1}{3}\) you get a complex value anyway and then the principal value jumps to the branch of the value with the smallest argument. The HP-42S doesn't provide a cubic root function and thus we have to use \(y^x\) with an approximation to \(\frac{1}{3}\) which is the reason you get a complex result for a negative value. This is the program I used to calculate the values of the function: Function Code: LBL "Fx" It returns complex values for negative real values. Nothing can hinder us to still try to find a critical point for which I got: x=-4.00000057735 But for \(x=-4\) the function will return 0 i0 Which is the reason we get that result. I haven't stressed that but there are two conditions that the function has to meet:
The 2nd condition means you can't use operations like ABS or SIGN in your program to define the function. These functions aren't analytical. For the given function the minimum is at (0, 0). HTH Thomas |
|||
08-25-2018, 05:19 PM
Post: #14
|
|||
|
|||
RE: Derivatives on HP 42S
This is why, if just 1 function could be added to the HP 42S, I would choose xROOTy !
|
|||
08-25-2018, 06:05 PM
Post: #15
|
|||
|
|||
RE: Derivatives on HP 42S
You can still deliberately choose the other branch that makes the result real for negative input:
Code: LBL "Fx" With initial guesses -4 and 0 you get what you expected: x=-1 But now the function returns complex values for positive input. Kind regards Thomas |
|||
08-25-2018, 07:03 PM
(This post was last modified: 08-25-2018 07:48 PM by Albert Chan.)
Post: #16
|
|||
|
|||
RE: Derivatives on HP 42S
(08-25-2018 05:19 PM)lrdheat Wrote: This is why, if just 1 function could be added to the HP 42S, I would choose xROOTy ! seems this is trivial to implement. If x < 0 and y is odd integer, return -(-x) ^ (1/y), else x ^ (1/y) If you knew y is odd integer, it is even simpler, just check sign of x. This avoids the extremum search returning complex results. Just add the check straight into Fx Edit: complex number don't understand x < 0, may be real part of x instead ? |
|||
08-25-2018, 08:00 PM
Post: #17
|
|||
|
|||
RE: Derivatives on HP 42S
(08-25-2018 07:03 PM)Albert Chan Wrote: seems this is trivial to implement. This sounds a bit like: this is left as an exercise to the dear reader. Quote:If x < 0 and y is odd integer, return -(-x) ^ (1/y), else x ^ (1/y) Unfortunately \(x=\Re[z]\) and \(y=\Im[z]\) aren't analytic functions. So anything based on these values isn't analytic as well. Quote:Just add the check straight into Fx This violates the precondition of this algorithm: the function must be analytical You might get away in this case but it won't work if a stationary point happens to be at the place where you stitch the functions together. Cheers Thomas |
|||
08-25-2018, 09:20 PM
Post: #18
|
|||
|
|||
RE: Derivatives on HP 42S
(08-25-2018 08:00 PM)Thomas Klemm Wrote:(08-25-2018 07:03 PM)Albert Chan Wrote: If x < 0 and y is odd integer, return -(-x) ^ (1/y), else x ^ (1/y)Unfortunately \(x=\Re[z]\) and \(y=\Im[z]\) aren't analytic functions. Can you explain the word analytic ? Is this the cost of using complex numbers ? (even using the real part not allowed ?) Is f(x) = x^(1/3) an analytic function ? If x is complex, is it true that f(x) same as -f(-x) ? Googling "function not analytical": A complex function is said to be analytic on a region if it is complex differentiable at every point in. The terms holomorphic function, differentiable function, and complex differentiable function are sometimes used interchangeably with "analytic function" (Krantz 1999, p. 16). After above googling, click the graph, you get wikipedia explanation: non-analytic smooth function is a smooth function which is nowhere real analytic |
|||
08-26-2018, 04:54 AM
Post: #19
|
|||
|
|||
RE: Derivatives on HP 42S
(08-25-2018 09:20 PM)Albert Chan Wrote: Can you explain the word analytic ? Consider the complex valued function \(w=z^2\). You can calculate both the real and imaginary part of \(w=u+iv\): \(\begin{aligned} u &= x^2-y^2 \\ v &= 2xy \end{aligned}\) But these functions \(u(x, y)\) and \(v(x, y)\) are not independent. Instead the Cauchy–Riemann equations hold true: \(\begin{aligned} \frac {\partial u}{\partial x}&=\frac{\partial v}{\partial y} \\ \frac {\partial u}{\partial y}&=-\frac{\partial v}{\partial x} \end{aligned}\) And indeed: \(\begin{aligned} u_x &=2x=v_y \\ u_y &=-2y=-v_x \end{aligned}\) Thus this function is analytic. However this function isn't: \(\begin{aligned} u &= x^2+x-y^2 \\ v &= 2xy \end{aligned}\) Because \(u_x=2x+1\) but still \(v_y=2x\). For the other function that I mentioned, e.g. \(\Re[z]\) we have: \(\begin{aligned} u &= x \\ v &= 0 \end{aligned}\) This isn't analytic since \(u_x=1\) but \(v_y=0\). In short: If you define the function in terms of \(z\) the function is most probably analytic. However if you try to stitch together a complex function based on \(x\) and \(y\) chances are high that it's not analytic. Quote:Is f(x) = x^(1/3) an analytic function ? Yes. Its derivative is: \(\frac{d}{dz}\left(\sqrt[3]{z}\right)=\frac{1}{3z^{\frac{2}{3}}}\) Quote:If x is complex, is it true that f(x) same as -f(-x) ? No. Consider \(f(z)=z^2\). Here we have \(f(-z)=f(z)\). Cheers Thomas |
|||
08-26-2018, 01:54 PM
Post: #20
|
|||
|
|||
RE: Derivatives on HP 42S
(08-26-2018 04:54 AM)Thomas Klemm Wrote:(08-25-2018 09:20 PM)Albert Chan Wrote: If x is complex, is it true that f(x) same as -f(-x) ? Functions for which f(x) = f(-x) are called even functions, and functions for which f(x) = -f(-x) are called odd functions, on account of the fact that their Taylor series contain only even or odd powers of x, respectively. The resulting symmetry applies for complex numbers as well as it does for real ones. |
|||
« Next Oldest | Next Newest »
|
User(s) browsing this thread: 2 Guest(s)