Calculating the gradient at Gradient Ascent Algorithm

6 次查看(过去 30 天)
Hi, The code below is suppose to perform gradient ascent after implementation of the missing parts. There are several things I dont' know how to do, the first one being how to "Calculate the gradient at StartPt" which is suppose to be done in the for loop. I would very much appriciate help from you guys.
Thank you very much.
function GradientAscentEg
% plot the landscape
ezmesh(@ComplexLandscape,[-3 7],[-3 7])
% Enter maximum number of iterations of the algorithm, learning rate and
% mutation range
NumSteps=50;
LRate=0.1;
MaxMutate=1;
% TO DO: choose a random starting point with x and y in the range, (-2, 2)
GradAscent(StartPt,NumSteps,LRate);
% HillClimb(StartPt,NumSteps,MaxMutate);
function GradAscent(StartPt,NumSteps,LRate)
PauseFlag=1;
hold on;
for i = 1:NumSteps
% TO DO: Calculate the 'height' at StartPt using SimpleLandscape
% TO DO: Plot a point on the landscape in 3D
% use plot3(x,y,z,,'r*','MarkerSize',10)
% to get a marker you can see well
% TO DO: Calculate the gradient at StartPt
% TO DO: Calculate the new point and update StartPoint
% Make sure StartPt is within the specified bounds
StartPt = max([StartPt;-2 -2]);
StartPt = min([StartPt;2 2]);
% Pause to view output
if(PauseFlag)
x=input('Press return to continue\nor 0 and return to stop pausing\n');
if(x==0) PauseFlag=0; end;
end
end
hold off

回答(1 个)

Walter Roberson
Walter Roberson 2016-11-7
What is it that you are trying to take the gradient of? Is it the function handle @ComplexLandscape ? Is it @SimpleLandscape ? What are their input variables?
If you are trying to take the the numeric gradient of a function near the starting point, then you need to evaluate the function at 9 points total, all combinations of x0, x0-dx, x0+dx, y0, y0-dy, y0+dy . Then you can use gradient()
  2 个评论
N/A
N/A 2016-11-7
I need to calculate the gradient at a point and then use this gradient to update the point I'm at. SimpleLandscapeGrad returns the elements of the gradient as a vector.
Walter Roberson
Walter Roberson 2016-11-7
Then it sounds like you just have to call SimpleLandscapeGrad and pass in the initial point.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Simulation 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by