Opaque functions and fsolve / numerical methods
04-07-2021, 08:34 PM
Post: #1
 compaqdrew Junior Member Posts: 8 Joined: Mar 2021
Opaque functions and fsolve / numerical methods
Suppose I have an opaque f(x) on the reals. By "opaque" I mean, we don't peek at the function definition, we rely on evaluating it at specific reals. For the sake of example here is a definition to (not) peek at

Code:
EXPORT OPAQUE(I) BEGIN IF I > 3 THEN RETURN I; END; RETURN I*3+1.2; END;

Code:
 OPAQUE(9)        // 9

Unsurprisingly, we cannot symbolically solve equations like OPAQUE(x)==9. But perhaps surprising, we cannot solve numerically either:

Code:
 fsolve(OPAQUE(x)==9,x)  // [] //Prime the solver with a guess that is the correct answer fsolve(OPAQUE(x)==9,x,9)  //"fsolve((OPAQUE(x)) = 9,x,9) Error: Bad Argument Type"

The latter might be because of another annoyance, the function cannot be evaluated with subst:

Code:
 subst(OPAQUE(x),x=9) //Error: Bad argument type

I suppose I can solve this graphically, but is there a more practical way to use the numerical methods than with fsolve?

My theory is it's trying to pass some CAS type into function input and see if it can exploit the definition of OPAQUE somehow. However, since the function doesn't have a CAS "definition" the nonreal input is a "Bad argument type". But it's a mystery how to force the real input from a numerical solver...
Post: #2
 roadrunner Senior Member Posts: 396 Joined: Jun 2015
RE: Opaque functions and fsolve / numerical methods
If you make it a CAS function:

#cas
opaque(i):=
BEGIN
IFTE(i>3,i,i*3+6/5);
END;
#end

then in CAS fsolve((opaque(x)) = 9,x) returns: [2.6,9.]
04-08-2021, 04:54 AM
Post: #3
 compaqdrew Junior Member Posts: 8 Joined: Mar 2021
RE: Opaque functions and fsolve / numerical methods
How did you figure that out? I can't find a reference to "CAS functions" in the manual nor to the "#cas" statement (...directive?) I was able to turn up a few forum threads but mostly discussing syntax and not really any differences in the programming model or why to prefer one vs the other.

I also found FNROOT, which seems like a noncas version of fsolve. I guess it makes some sense to use like functions with like, but I'm fuzzy on how cas/noncas actually differ in their programming model or typesystem.

My intuition was that a numerical solver would want to operate on numbers and not symbolic representations, but it seems for fsolve this is not the case...
Post: #4
 roadrunner Senior Member Posts: 396 Joined: Jun 2015
RE: Opaque functions and fsolve / numerical methods
(04-08-2021 04:54 AM)compaqdrew Wrote:  How did you figure that out?

I guess just from reading this forum for the last gillion years or so.

fsolve uses bisection and i believe it does do numerical solutions; i think CAS just solves the equation differently and sends that to fsolve. Maybe the prime gurus can chime in on that one.

Because bisection usually doesn't like discontinuities I was surprised fsolve found both solutions separated by one. In general you will probably have to give it a range to search in that avoids discontinuities.