Ask Your Question

Solve/find_root for miscellaneous special function elliptic_kc?

asked 2023-05-11 05:04:53 +0100

JimR gravatar image

I am looking to use solve() or find_root() with a function defined using elliptic_kc (sage.functions.special.EllipticKC), however Sage produces an error. The context of this is for use in a coplanar waveguide impedance calculation.


sage: find_root(elliptic_kc(x)==2, x, 0, 0.5)


'TypeError: unable to simplify to float approximation'.


sage: solve(2.0==elliptic_kc(x), x, to_poly_solve=True)


TypeError: 'sage.symbolic.expression.Expression' object is not subscriptable

Is there a convenient way to perform a solve/numerical root for these special functions?

edit retag flag offensive close merge delete


Oh huh, I found a solution using scipy.optimize.root_scalar. It's not the cleanest, but I suppose it works.

sage: def el(x): return elliptic_kc(x) - 2.0

sage: root_scalar(el, bracket=(0,1)).root

JimR gravatar imageJimR ( 2023-05-11 05:13:22 +0100 )edit

To display code blocks, indent them by four spaces.

slelievre gravatar imageslelievre ( 2023-05-19 22:00:28 +0100 )edit

1 Answer

Sort by ยป oldest newest most voted

answered 2023-05-12 03:58:11 +0100

dan_fulea gravatar image

Defining a function instead worked for me:

sage: f = lambda x: elliptic_kc(x) - 2
sage: find_root(f, 0, 0.9)

Alternatively, one can use the solve functionality from pari/gp:

sage: gp("solve(x=0, 0.7, ellK(x^(1/2)) - 2)")

Note that the pari/gp function ellK is not the same (complete) elliptic $K$ function from sage. For instance, the values in $1/4$ differ:

sage: elliptic_kc(1/4.)


? ellK(1/4)
%12 = 1.5962422221317835101489690714979498795


? ellK(1/2)
%13 = 1.6857503548125960428712036577990769895

Just as a note: pari/gp can give us more decimal places:

? \p 150
   realprecision = 154 significant digits (150 digits displayed)
? solve(x=0, 0.7, ellK(x^(1/2)) - 2)
%14 = 0.643856219147754646866721157651083249845248156171320703143872009317482774610540686232984439485823088513555922361027026184004141998761703004038574916462
edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower


Asked: 2023-05-11 05:04:53 +0100

Seen: 55 times

Last updated: May 12