Derivatives on HP 42S
08-26-2018, 04:47 PM
Post: #21
 lrdheat Senior Member Posts: 730 Joined: Feb 2014
RE: Derivatives on HP 42S
Hi Thomas,

The solver for dx=0 of some function fx, and then the running of fx to find f(x) of the extremum found by dx is quite successful. When I run dx for, say, pi/4 (let's be simple and use fx=sin x), I get ~0 instead of ~.707

Why is dx successful in solve, but not as a means to find dx of a specific x value?
08-26-2018, 08:00 PM
Post: #22
 Thomas Klemm Senior Member Posts: 1,687 Joined: Dec 2013
RE: Derivatives on HP 42S
(08-26-2018 04:47 PM)lrdheat Wrote:  Why is dx successful in solve, but not as a means to find dx of a specific x value?

(08-21-2018 12:34 AM)Thomas Klemm Wrote:  For a small discrete h, this can be approximated by

$$\frac{\partial f}{\partial x}\approx\frac{\Im[f(x + ih)]}{h}$$

Since we search for stationary points we set:

$$\frac{\partial f}{\partial x}=0$$

Which we approximate with just:

$$\Im[f(x + ih)]=0$$

To return an approximation for the derivative we have to divide $$\Im[f(x + ih)]$$ by $$h$$:
Code:
LBL "dF" MVAR "x" MVAR "h" RCL "x" RCL "h" COMPLEX XEQ "Fx" COMPLEX RCL÷ "h" END

Code:
LBL "Fx" SIN END

We can calculate the value of $$\frac{d}{dx}\sin(x)$$ at $$x=1$$:

1E-6
STO "h"
1
STO "x"
XEQ "dF"

x: 5.40302305868e-1

Compare this with $$\cos(1)$$:

1
COS

x: 5.40302305868e-1

Best regards
Thomas
08-26-2018, 08:39 PM
Post: #23
 Albert Chan Senior Member Posts: 1,896 Joined: Jul 2018
RE: Derivatives on HP 42S
(08-26-2018 04:47 PM)lrdheat Wrote:  When I run dx for, say, pi/4 (let's be simple and use fx=sin x), I get ~0 instead of ~.707

my guess is you forget to divide by h:

df(x) ~ Im((f(x + i*h)) / h

Code:
>>> from cmath import * >>> h = 1e-6 >>> sin(complex(pi/4, h).imag 7.0710678118666542e-07 >>> _ / h 0.70710678118666548
08-29-2018, 01:52 PM (This post was last modified: 08-27-2019 10:07 PM by Albert Chan.)
Post: #24
 Albert Chan Senior Member Posts: 1,896 Joined: Jul 2018
RE: Derivatives on HP 42S
A blog from the inventor of Complex Step Differentiation Algorithm.
It had a fully worked out example, showing very stable and accurate estimates:

https://blogs.mathworks.com/cleve/2013/1...entiation/

Real part of f(x + h*I) is also useful, for getting second derivative (involve subtraction):

http://ancs.eng.buffalo.edu/pdf/ancs_pap..._gnc06.pdf

Since we already have the value of y = f(x + h*I) of approximate extremum x, we can improve it:

Code:
from cmath import * f = lambda x: sin(x) ** exp(x)  # rework post 3 x, h = 14.137167, 1e-6          # try to improve extremum x y = f(complex(x, h)) # Newton's method, using f''(x) = 2/(h*h) * (f(x).real - y.real) x -= h/2 * y.imag / (f(x).real - y.real)

2nd order x = 14.137167
3rd order x = 14.1371669412
﻿ ﻿ ﻿ ﻿ Actual x = 14.137166942003920 ...
 « Next Oldest | Next Newest »

User(s) browsing this thread: 1 Guest(s)