Skip to content

Instantly share code, notes, and snippets.

@gajomi
Created March 1, 2012 00:23
Show Gist options
  • Save gajomi/1946057 to your computer and use it in GitHub Desktop.
Save gajomi/1946057 to your computer and use it in GitHub Desktop.
An example of fmincon returning Hessian for linear model
clear g
T = 10;
m_exact = 1;
b_exact = 2;
x_exact = [m_exact; b_exact]
t = [0:T-1]/(T-1);
A = [t' ones(size(t'))];
y_exact = A*x_exact;
figure(1)
plot(t,y_exact,'-o')
G = @(x) (y_exact-A*x)'*(y_exact-A*x);
m = [.5:.01:3];
b = [.5:.01:3];
[mm,bb] = meshgrid(m,b);
mm = mm(:);bb = bb(:);
for i = 1:length(mm)
g(i) = G([mm(i);bb(i)]);
end
figure(2)
g = reshape(g,length(m), length(b));
imagesc(m,b,g)
xlabel('m')
ylabel('b')
x0 = [4.5;2.2];
[x_est,fval,flag,output,grad,hessian] = fminunc(G,x0);
theoretical_hessian = 2*A'*A;
disp(x_exact)
disp(x_est)
disp(theoretical_hessian)
disp(hessian)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment