Question 1 code

mail@pastecode.io avatarunknown
matlab
a month ago
824 B
2
Indexable
Never
z = @(x,y) (x.^2 + y - 11).^2 + (x.^2 + y - 7).^2;

%% question (a)
A = fminsearch(@(x) z(x(1), x(2)), [1,1])
B = fminsearch(@(x) z(x(1), x(2)), [-1,1])
C = fminsearch(@(x) z(x(1), x(2)), [-1,-1])
D = fminsearch(@(x) z(x(1), x(2)), [2,-1])

%% question (b)
r = -5: 0.1 : 5;
[X,Y] = meshgrid(r, r);;
surf(X, Y, z(X,Y), EdgeColor="none")

%% question (c)
hold on

% compiles part (a) results and their z-values into matrix E 
lmins = {A, B, C, D};
E = [];
for i = 1 : length(lmins)
    lmin = lmins{i};
    E = [E; lmin z(lmin(1), lmin(2))];
end 

% plotting and formatting the points
p = plot3(E(:,1), E(:,2), E(:,3), 'o');
p.MarkerSize = 10;
p.MarkerFaceColor = 'r';
p.MarkerEdgeColor = 'r';


%% general graph formatting
title("Question 1b and c")
xlabel("x")
ylabel("y")
zlabel("z(x,y)")