How to use gradient function for 3 variable in function?
Show older comments
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.
Answers (1)
Jan
on 1 May 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.
Categories
Find more on Motor Drives in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!