Gradient Descent Implementation on a function, as opposed to an equation
7 次查看(过去 30 天)
显示 更早的评论
I have built a function
function [RMS] = ErrorFunction(ui,vi,wi,cx,cy,cz)
which outputs a certain error based on the six intial conditions I input to the model. Now, my model is iterative and the error depends on some intermediate parameters hence it is not possible to define a relationship between the error and six inputs. My aim is to minimize the error to zero using Gradient/Steepest Descent Method and I'm hoping somone would guide me in implementing it on a function, as opposed to a straightforward explicit relationship like, f(x,y) = 4x^2-4xy+2y^2
0 个评论
采纳的回答
Bjorn Gustavsson
2022-9-7
You could do something like this:
1, rewrite the function to take an array as input-parameter instead of a number of scalar parameters
function [RMS] = ErrorFunction([ui_vi_wi_cx_cy_cz])
2, calculate a numerical gradient (central difference):
curr_pars = [ui_vi_wi_cx_cy_cz]; % current point
h = 0.01; % you have to decide what a suitably small step is, and if you need different sized steps for the different input variables...
for i_v = numel(curr_pars):-1:1
cp = curr_pars;
cp(i_v) = cp(i_v) + h;
cm = curr_pars;
cm(i_v) = cm(i_v) - h;
gradErrorFunction(i_v) = (ErrorFunction(cp) - ErrorFunction(cm))/(2*h);
end
HTH
更多回答(1 个)
Torsten
2022-9-7
Use
fun = @(x)ErrorFunction(x(1),x(2),x(3),x(4),x(5),x(6))
as the function handle you work on in the steepest decent.
0 个评论
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!