home

Scientific Computing (Psychology 9040a)

Fall, 2021

Cost Landscapes: sample solution


% part 1: 1D cost landscape

%% Map J

x = linspace(-10,10,201); % sample from -10 to +10 in 200 steps
J = mycostfun1d(x);       % compute cost function over all values
figure                    % visualize the cost landscape
plot(x,J,'b-','linewidth',2);
xlabel('X');
ylabel('J');

%% Optimize x

X0 = -4.0;                                 % initial guess
[Xf,FVAL] = fminsearch('mycostfun1d', X0); % optimize!
hold on
plot(X0, mycostfun1d(X0), 'g.', 'markersize', 20)
plot(Xf, FVAL, 'r.', 'markersize', 20)
text(X0,mycostfun1d(X0),'   initial guess')
text(Xf,FVAL,'   minimum value')

% part 2: 2D cost landscape

%% Map J

x = linspace(-3,3,51); % sample from -3 to +3 in 50 steps
y = linspace(-3,3,51);
XY = combvec(x,y);
J = mycostfun2d(XY');     % compute cost function over all values
[Y,X] = meshgrid(x,y);    % reshape into matrix form
Z = reshape(J,length(x),length(y));
figure                    % visualize the cost landscape
meshc(X,Y,Z);
shading flat
xlabel('X');
ylabel('Y');
zlabel('J');

%% Optimize (x,y)

X0 = [2.5, 2.5];                          % initial guess
[Xf,FVAL] = fminsearch('mycostfun2d', X0); % optimize!
hold on
z0 = get(gca,'zlim');
z0 = z0(1);
plot3([Xf(1),Xf(1)],[get(gca,'ylim')],[z0 z0],'color','r','linewidth',2);
plot3([get(gca,'xlim')],[Xf(2),Xf(2)],[z0 z0],'color','r','linewidth',2);

mycostfun1d.m:

function J = mycostfun1d(X)
%
% note use of dot notation on .* and ./ operators
% this enables the computation to happen for
% vector values of x
%
J = (X .* exp(-X.*X)) + ((X.*X)./20);

mycostfun2d.m:

function J = mycostfun2d(X)
%
% note use of dot notation on .* and ./ operators
% this enables the computation to happen for
% vector values of x
%
x = X(:,1);
y = X(:,2);
J = (x .* exp(-(x.*x)-(y.*y))) + (((x.*x)+(y.*y))./20);