For the function, f (x) = X2-4X + 2XY + 2Y2 + 2Y +14 Plot the surface function for X over [5 6]. and Y over [-4. -2],. Draw the contour plot for X over [0 10]. and Y over [-4. -2] and values for the contours of V=[1 1.25 1.5 2 2.5 3]; Write an m-file to find the minimum of the function using the gradient descent method. Use a starting value of [4. -4].
x = linspace(5,6,100);
y = linspace(-4,-2,100);
[X,Y] = meshgrid(x,y);
f = X.^2 - 4*X + 2*X.*Y + 2*Y.^2 + 2*Y + 14;
figure, surf(X,Y,f), title('Surface Plot')
xlabel('X'), ylabel('Y'), zlabel('f(x)')
x = linspace(0,10,100);
y = linspace(-4,-2,100);
[X,Y] = meshgrid(x,y);
f = X.^2 - 4*X + 2*X.*Y + 2*Y.^2 + 2*Y + 14;
v = [1.0 1.25 1.5 2 2.5 3];
figure, contour(X,Y,f,v,'ShowText','on')
xlabel('X'), ylabel('Y'), title('Contour Plot')
f = @(x) x(1)^2 - 4*x(1) + 2*x(1)*x(2) + 2*x(2)^2 + 2*x(2) + 14;
% x(1) => X, x(2) => Y
x0 = [4 -4];
x = fminunc(f, x0); % gradient search
fprintf('Solution: X = %.4f, Y = %.4f\n', x(1), x(2))
fprintf('Minimum value of f(x) = %.4f\n', f(x))
Output:
Solution: X = 5.0000, Y = -3.0000
Minimum value of f(x) = 1.0000
Get Answers For Free
Most questions answered within 1 hours.