Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a MATLAB script (not a function in matlab) to use the gradient descent met

ID: 3905255 • Letter: W

Question

Write a MATLAB script (not a function in matlab) to use the gradient descent method of optimization to minimize two different functions: f(x) = 10x12 + 10x22 and f(x) = 10x12 + x22 . For each function, use x0 = [0.5, 10]T (T meaning transpose) as the starting point and ||?f(x)|| < 10-6 as the stopping criterion.

Explanation / Answer

% Initialization Point x0 = [0.5 10]'; % ' indicates transpose % termination tolerance tol = 1e-6; % maximum number of allowed iterations maxiter = 1000; % minimum allowed perturbation dxmin = 1e-6; % step size alpha = 0.1; % initialize gradient norm, optimization vector, iteration counter, perturbation gnorm = inf; x = x0; niter = 0; dx = inf; % define the objective function: f1 = @(x1,x2) 10*x1.^2 + 10*x2.^2; f2 = @(x1,x2) 10*x1.^2 + x2.^2; % redefine objective function syntax for use with optimization: f1_new = @(x) f1(x(1),x(2)); f2_new = @(x) f2(x(1),x(2)); % define the gradient of the objective function g1 = grad(x) g1 = [20*x(1) + 20*x(2)]; % define the gradient of the objective function g2 = grad(x) g2 = [20*x(1) + 2*x(2)]; % gradient descent algorithm for f1: while and(gnorm>=tol, and(niter = dxmin)) % calculate gradient: g1 = grad(x); gnorm = norm(g1); % take step: xnew = x - alpha*g1; % check step if ~isfinite(xnew) display(['Number of iterations: ' num2str(niter)]) error('x is inf or NaN') end % update termination metrics niter = niter + 1; dx = norm(xnew-x); x = xnew; end x1opt = x; f1opt = f1_new(xopt); niter = niter - 1; % gradient descent algorithm for f1: while and(gnorm>=tol, and(niter = dxmin)) % calculate gradient: g2 = grad(x); gnorm = norm(g2); % take step: xnew = x - alpha*g2; % check step if ~isfinite(xnew) display(['Number of iterations: ' num2str(niter)]) error('x is inf or NaN') end % update termination metrics niter = niter + 1; dx = norm(xnew-x); x = xnew; end x2opt = x; f2opt = f2_new(xopt); niter = niter - 1;
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote