HP Forums

Full Version: HP 42S Question
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I use a neat program for finding extremums by solving to find when d/dx=0. The derivative program goes like this

LBL "DX"
MVAR "X"
MVAR "H"
RCL "X"
RCL "H"
COMPLEX
XEQ "FX"
COMPLEX
END

This works in solve for most routine functions ("FX"). I am having a problem with a more complicated function, where the program for the function ("FX") executes successfully, but my derivative function ("DX") will not work in solve (generates an "invalid type" error).

An example is the following program "(FX") which executes successfully, but not in the solver in program "DX"

LBL "FX"
ENTER
ENTER
ENTER
X^2
5
-
X<0?
+/-
SQRT
END

Is there a way to handle this sort of thing where the DX program can work in solve?
"H" is some small epsilon such as 1E-5
The X<0? function doesn't work on complex numbers. That's what's causing the Invalid Type error message. This makes sense because the complex numbers are not an ordered set, unlike the reals. None of the inequality tests work with them, but X=Y? and X≠Y? do. N.B. Those tests do exhibit some behavior that might be surprising and mathematically questionable, specifically, they consider x and x+0i to be not equal.

In the case of real numbers, the sequence X<0? +/- is equivalent to ABS, but if you want this to work with complex numbers, you should replace it with the actual ABS function, if you want the square root of the magnitude of the number, or leave it out, since SQRT works on all complex numbers.
I had utilized the ABS function initially, it didn't work in "DX" as it is written, which is why I resorted to X<0?, +/-. I had been getting the same invalid error statement. I'm flummoxed at this point (my skill level, admittedly pedestrian).
If simply taking the square root is not an option, i.e. you want to force the real part of the complex number to be nonnegative, you could do this:

COMPLEX
RCL ST Y
SIGN
STO× ST Z
×
COMPLEX
(02-03-2019 10:11 PM)Thomas Okken Wrote: [ -> ]If simply taking the square root is not an option, i.e. you want to force the real part of the complex number to be nonnegative, you could do this:

COMPLEX
RCL ST Y
SIGN
STO× ST Z
×
COMPLEX

(08-25-2018 08:00 PM)Thomas Klemm Wrote: [ -> ]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

In your case I'd rather separate the functions:

\(f(z)=\sqrt{z^2-5}\)

\(g(z)=\sqrt{5-z^2}\)

What is your "more complicated function"?

Cheers
Thomas
Thanks, Thomas.

That was the more complicated function! Functions without absolute values or odd numbered nroots and such mostly work fine in "FX" and then in "DX".
Some background for the complex step differentiation and its limitations: https://blogs.mathworks.com/cleve/2013/1...entiation/
Thanks for the blog.
Reference URL's