How to use gradient function for 3 variable in function?

Hi, I have a nonlinear function such that myfunc(x(1), x(2), x(3), 5e6, 0, 're'). I want to evaluate the gradient of function at [x(1), x(2), x(3)]=[1.2, 1.5, 2.0]. I am trying to solve that problem by numerical gradient. Because myfunc consists of another functions and algorithms. How can I do this? Thanks.

Respuestas (1)

Jan
Jan el 1 de Mayo de 2011
Use finite differences:
h = sqrt(eps(x));
y1 = myfunc(x(1)-h(1), x(2)-h(2), x(3)-h(3), 5e6, 0, 're');
y2 = myfunc(x(1)+h(1), x(2)+h(2), x(3)+h(3), 5e6, 0, 're');
yd = (y2 - y1) ./ (2 * h);
you can use an approximation of the 2nd derivative to improve the stepsize h.

Etiquetas

Preguntada:

el 21 de Mzo. de 2011

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by