quadfit2.m 748 B

12345678910111213141516171819202122232425262728293031323334353637383940
  1. function [H, d, c, e] = quadfit2(xs, Fs, at_ori)
  2. % function [H, d, c, e] = quadfit2(xs, Fs, [at_ori])
  3. if nargin<3,
  4. at_ori = 0;
  5. end;
  6. n = size(xs, 2);
  7. if at_ori, % if (0,0) is the extremum, then fit quadratic terms only
  8. X = zeros(n, 3);
  9. for p = 1:n,
  10. x = xs(:,p);
  11. X(p,:) = [x(1)^2/2 x(1)*x(2) x(2)^2/2];
  12. end;
  13. else
  14. X = zeros(n, 6);
  15. for p = 1:n,
  16. x = xs(:,p);
  17. X(p,:) = [x(1)^2/2 x(1)*x(2) x(2)^2/2 x(1) x(2) 1];
  18. end;
  19. end;
  20. % solve for A in X*A = Fs,
  21. A = pinv(X)*Fs(:);
  22. H = zeros(2,2);
  23. d = zeros(1,2);
  24. c = 0;
  25. H(1,1) = A(1);
  26. H(1,2) = A(2); H(2,1) = H(1,2);
  27. H(2,2) = A(3);
  28. if ~at_ori,
  29. d(1) = A(4);
  30. d(2) = A(5);
  31. c = A(6);
  32. end;
  33. e = Fs - (diag(xs'*H*xs*0.5)' + d*xs + c);