How to handle the differentiation of non-analytic smooth functions (the bump function)

208 views
Skip to first unread message

Lin

unread,
Sep 13, 2019, 1:57:01 PM9/13/19
to CasADi
Dear there,

I am wondering if casADi can handle the differentiation of the so-called bump function? Wiki link for bump function. It is a smooth function, but it's non-analytical.
If the answer is yes, can you provide a Matlab example of how to code the function?

Thanks a lot!

Best,
Lin

P.S. The following code doesn't work.
fm=zeros(size(x));
fm(abs(x-R)<R) = exp(1./((x(abs(x-R)<R)-R).^2-R^2));

Joel Andersson

unread,
Sep 22, 2019, 4:59:58 PM9/22/19
to CasADi
For radius 1, you can try:

= casadi.SX.sym('x');
x2 = x*x;
phi = if_else(x2<1, exp(-1/(1-x2)), 0);

 Working with non-differentiable functions in CasADi is risky, lots of things can go wrong. So I wouldn't recommend it unless you are already pretty familiar with the tool.

Joel

Bruno M L

unread,
Sep 24, 2019, 3:41:11 PM9/24/19
to CasADi
Interesting function!

Dear Joel, for what I understand the bump function is differentiable (smooth) but it is divided in two parts. Each part is analytic and differentiable so I believe if_else can work.

Dear Lin, if it works, please share with us.

Bruno

Lin

unread,
Sep 24, 2019, 4:57:54 PM9/24/19
to CasADi
Dear Joel,

The if_else function works perfectly.
Thanks for the help!

Best,
Lin

Lin

unread,
Sep 24, 2019, 5:03:05 PM9/24/19
to CasADi
Dear Bruno, 

The if_else function works perfectly.
Here is the plot of the casadi calculated derivatives.
The Matlab script:
function yout = testBump
clc
clear

x= casadi.SX.sym('x',1);

molli= casadi.Function('molli',{x},{Mollifier(x)});

deriva = casadi.Function('deriva',{x},{jacobian(molli(x),x)});

%%
figure
xx= -0.5:0.001:2.5;

subplot(2,1,1)
plot(xx, full(molli(xx)))
legend('Bump function')
grid on

subplot(2,1,2)
yout = full(deriva(xx));
plot(xx, full(deriva(xx)))
legend('derivative of bump function')
grid on


end

function y = Mollifier(x)
    R     = 1;
    m     = 1;
    x2    = (x-R).^2;
    y     = if_else(x2<R^2, exp(m./(x2 - R^2)), 0);      
end


bump_function.png
Reply all
Reply to author
Forward
0 new messages