Matlab: Plot objective and gradients (partial derivatives)

Given a function and the gradient/derivative function of it, this code can plot the function and for each dimension the partial derivatives.

If you want to minimize your objective function with a gradient based optimizer, then it sometimes comes handy to visualize the objective functions (or at least projections of it) and the gradient function. Then it is more easy to see if your gradients are correct and if the objective function is optimizable.

Of course another way to check if your gradients are correct is to compute the numerical gradient and compare the difference to your computed gradient.

NOTE ON THE CODE: If your matlab version does not provide the option MaxHeadSize whith quiver, you can have a look at http://www.mathworks.de/matlabcentral/fileexchange/34305-adjust--quiver-....

Here are some example images how the figure look like. In blue the objective function, in green the gradients/tangents.

objective and gradients matlab plot

objective and gradients matlab plot

The CODE for plotting the objective and the gradients:

plotGradAndObj.m

And here an example script to call it

RunExample.m

And here the Function that returns the objective and gradient:

exampleObjAndGrad.m

blog_tags: 

Add new comment

Filtered HTML

  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd> <pre> <img> <br> <p> <span>
  • Lines and paragraphs break automatically.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Use [collapse] and [/collapse] to create collapsible text blocks. [collapse collapsed] or [collapsed] will start with the block closed.

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
  • Code snippets in <code>...</code> or <source>...</source> automatically will be pretty printed.
  • Use [collapse] and [/collapse] to create collapsible text blocks. [collapse collapsed] or [collapsed] will start with the block closed.
CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.
Target Image