Optim deriv(fn,var) second derivative problem

Hello,

I am trying to get and plot the first and the second derivative of an anonymous function using deriv(f,var) from the optim package. The first derivative is fine, but the second looks like noisy or low resoluted.

My testcode:

f=@(x) x^3;
g=@(x) deriv(f,x);
h=@(x) deriv(f,x,:,:,2);

hold all;
fplot(f,[-3,3]);
fplot(g,[-3,3]);
fplot(h,[-10,10]);

Plot:

Looking at the command window, result for h(2)=11.813 , this should be a whole number.
image

Enlight me please, what is the problem and what is the proper way to get a nice smooth graph for the second derivative too.

( The result of h=@(x) deriv(g,x) is the same)

My system

  • 64bit Windows 10 Pro 20H2 19042.1288
  • x86_64-w64-mingw32
  • Graphics card: nVidia Quadro M1000M
  • Octave version: 6.1.0
  • Installation method: Downloaded and installed “octave-6.1.0-w64-installer.exe” from Download

Any help would be appreciated. Thank you in advance.

Cheers,
bulgakov

I assume the deriv function in your script is from the optim package. According to the documentation, the derivation step (third arguments) defaults to h = 1e-7. Increasing this value to 1e-6 does the trick for me.

f=@(x) x.^3;
g=@(x) deriv (f,x);
h=@(x) deriv (f,x, 1e-6, 2, 2);

hold on;
lims = [-10, 10];
fplot (f,lims);
fplot (g,lims);
fplot (h,lims);

image

1 Like

This solved the problem with the testcode and with my taskcode too.

Thank you.

Cheers,
bulgakov