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.
回答(1 个)
Jan
2011-5-1
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.
类别
在 帮助中心 和 File Exchange 中查找有关 Motor Drives 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!