This extra credit computing project involves writing a MATLAB code to examine op
ID: 3696337 • Letter: T
Question
This extra credit computing project involves writing a MATLAB code to examine optimization. You will need to read and learn about multiple 2D and 3D optimization methods and learn how to use them. Be sure to examine the report guidelines as well and include sections in your report on the theory and background of the Steepest Ascent Method and the Golden Search method.
Write a MATLAB script program that finds both the minimum and maximum of three dimensional
functions using from which you minima.
the Steepest Ascent Method. Your inputs should be the function and an x and y range will choose multiple starting guesses. Your outputs should be the location of maxima and
First, create a matrix of at least four starting guesses based on the range
Evaluate the partial derivatives numerically using the Central Difference Method (not analytically) to determine the direction of maximum gradient
Use the Golden Search Method to evaluate the maximum of your 2D direction function Use the Hessian matrix to determine if you’ve reach a local minimum or maximum. Your test script should also plot the locations of the minima and maxima on a 3D plot.
To evaluate your program, use the function,
o Fun=@(q,r) 1.8.^(-1.5*sqrt(q.^2+r.^2)).*cos(0.5*r).*sin(q);
Explanation / Answer
Fun = @(q,r)1.8.^(-1.5*sqrt(q.^2+r.^2)).*cos(0.5*r).*sin(q);
Guess = [2,2; 2,-2; -2,2; -2,-2];
XRange = [-2 2]; YRange = [-2 2];
x = XRange(1,1) : 0.25 : XRange(1,2);
y = YRange(1,1) : 0.25 : YRange(1,2);
[X,Y] = meshgrid(x,y);
Z = 1.8.^(-1.5*sqrt(X.^2+Y.^2)).*cos(0.5*Y).*sin(X);
deltax = 0.01;
deltay = 0.01;
iMin = 1; iMax = 1; iSad = 1;
Min = zeros(1,5); Max = zeros(1,5); Saddle = zeros(1,5);
d = zeros(1,5); h_opt = zeros(1,5); xopt = zeros(1,5);
f1 = zeros(1,5); f2 = zeros(1,5);
x1 = zeros(1,5); x2 = zeros(1,5);
xU = zeros(1,5); xL = zeros(1,5);
GS_Error = zeros(1,5); SA_Error = zeros(1,5);
R = (5^.5-1)/2;
for w = 1:4
x_new = Guess(w,1); y_new = Guess(w,2);
OptZ_new = Fun(x_new, y_new);
while SA_Error>.01
x_old = x_new;
y_old = y_new;
OptZ_old = OptZ_new;
InitialGuess=Fun(x_new,y_new);
dfdx = (Fun(x_old+deltax,y_old)-Fun(x_old-deltax,y_old))/(2*deltax);
dfdy = (Fun(x_old,y_old+deltay)-Fun(x_old,y_old-deltay))/(2*deltay);
Funh = @(h)Fun(x_new+dfdx*h,y_new+dfdy*h);
while abs(GS_Error)>.01
d(i) = ((sqrt(5)-1)/2)*(xU(i)-xL(i));
x1(i)=xL(i)+d(i);
x2(i)=xU(i)-d(i);
f1(i)=Funh(x1(i));
f2(i)=Funh(x2(i));
if abs(f1(i))>abs(f2(i))
OptZ_high = x1;
x1 = x2;
x2 = OptZ_high - d(i);
elseif abs(f2(i))>abs(f1(i))
OptZ_low= x2;
x2 = x1;
x1 = OptZ_low +d(i);
GS_Error(i) = (1-R)*abs(xU(i)-xL(i))/xopt(i);
x_new=x_new+dfdx*h_opt;
y_new=y_new+dfdy*h_opt;
SA_Error = sqrt((x_new-x_old)^2+(y_new-y_old)^2);
end
i = i+1;
end
end
end
d2fdx2 = (Fun(x_new+deltax,y_new)-2*Fun(x_new,y_new)+Fun(x_new-deltax,y_new))/(2*deltax)^2;
d2fdy2 = (Fun(x_new,y_new+deltay)-2*Fun(y_new,x_new)+Fun(x_new,y_new-deltay))/(2*deltay)^2;
d2fdxdy = (Fun(x_new+deltax,y_new+deltay)-Fun(x_new+deltax,y_new-deltay)-Fun(x_new-deltax,y_new+deltay)+Fun(x_new-deltax,y_new-deltay))/(4*deltax*deltay);
Hdet=d2fdx2*d2fdy2-d2fdxdy^2;
if abs(Hdet) > 0 && d2fdx2 > 0;
Max = OptZ_new;
elseif abs(Hdet) > 0 && d2fdx2 < 0;
Min = OptZ_new;
elseif abs(Hdet) <0;
saddle = OptZ_new;
end
hold on
surf(X,Y,Z)
plot3(Max(1,1),Max(1,2),Min(1,1),Min(1,2),Saddle(1,1),Saddle(1,2),'m*','MarkerSize',10);
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.