In: Computer Science
The following code must be written using matlab
Create a contour plot of the following function f(x,y) = (x+y^2-10)^2 +(x^2+y^2+8) in range [-6,6] for both x and y
Clearly indicate in the plot the coordinates of the minimum ponts and the the corresponding function values in these points
ANSWER:
CODE TEXT
% contour plot
% defining function f
f = @(x,y) (x + y.^2 - 10).^2 + (x.^2 + y.^2 + 8);
% defining x , y vector from -6 to 6
x = -6:0.2:6;
y = -6:0.2:6;
% generating meshgrid using x and y vector
[X,Y] = meshgrid(x,y);
Z = f(X,Y);
% ploting contour using contour function of matlab,
contour(X,Y,Z);
% labeling plot
xlabel("x value");
ylabel("y value");
title("Contour on function in xy range [-6,6]");
% holding plot to display min values
hold on;
% finding min value in Z, using min function
minZ = min(Z(:));
% finding locations for minZ in Z
[xMin,yMin] = find(Z==minZ);
% ploting points for minZ using plot3 function of matlab
plot3(X(xMin,yMin),Y(xMin,yMin),Z(xMin,yMin),"ro");
% displaying legend
legend("Z contour","Min Points");
CODE IMAGE
OUTPUT IMAGE