Defining functions at removable singularity points

59 views
Skip to first unread message

Mikhail Katliar

unread,
Jan 31, 2018, 9:29:36 AM1/31/18
to CasADi
Consider the function f(x,y)=x*sqrt(x^2+y^2). Its 1st derivatives w.r.t. x and y are:

df(x,y)/dx = x^2/sqrt(x^2+y^2)+sqrt(x^2+y^2),
df(x,y)/dy = x*y/sqrt(x^2+y^2)

The derivatives are not defined at x=0, y=0, and evaluating them in CasADi will give NaN. However, the limits of the derivatives when x->0, y->0 exist and are equal to 0. Therefore, the 1st derivatives can be made continuous everywhere by defining df(x,y)/d(x,y)=[0,0] at x=0, y=0.

The question is: how to define the function f in CasADi so that the way its derivatives are calculated at the singular point is also defined?

Joel Andersson

unread,
Jan 31, 2018, 10:52:11 AM1/31/18
to CasADi
The hacky answer is probably to replace

sqrt(x) with sqrt(x+1e-100)

and

x/y with x*y/(y*y + 1e-100)

Joel

Mikhail Katliar

unread,
Feb 2, 2018, 9:06:01 AM2/2/18
to CasADi
Hi Joel,

the hacky answer is what also came to our mind. We tried it but we still have issues with the numerics. Is there a less hacky way?

Joel Andersson

unread,
Feb 2, 2018, 9:38:38 AM2/2/18
to CasADi
What issues with the numerics do you get? NaN? Did you try


df(x,y)/dx = x^2/sqrt(x^2+y^2 + eps)+sqrt(x^2+y^2 + eps),
df(x,y)/dy = x*y/sqrt(x^2+y^2 + eps)

with some small eps?

Mario Olivari

unread,
Feb 2, 2018, 11:47:25 AM2/2/18
to CasADi
Hi Joel,

Indeed, this was our solution. We replaced all the sqrt in our function by adding eps=1e-14.
Then, we used rootfinder to calculate the roots of the function.
Unfortunately, rootfinder gave NaN with eps=1e-14.

Only increasing eps to 1e-5 fixes the problem.



Reply all
Reply to author
Forward
0 new messages