-
Notifications
You must be signed in to change notification settings - Fork 0
/
lsqcurvefit_GLS.m
39 lines (32 loc) · 1.09 KB
/
lsqcurvefit_GLS.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
function [coeffs resnorm residuals e o l J L] = lsqcurvefit_GLS(f, b0, x, y, error_cov, varargin)
%%%%%%%%
% Performs generalized least squares using the built-in MATLAB function
% lsqcurvefit. Requires an error covariance matrix error_cov for the data
% y. All other inputs are the same as for lsqcurvefit.
%%%%%%%%
% Copyright MIT 2012
% Developed by Nilah Monnier & Syuan-Ming Guo
% Laboratory for Computational Biology & Biophysics
%%%%%%%%
if size(y,1)<size(y,2)
y = y';
end
if size(x,1)<size(x,2)
x = x';
end
[L p] = chol(error_cov, 'lower');
if p~=0
L = chol(corr_repair(error_cov,100,'cov'), 'lower');
disp('Warning: regularization failed')
end
f_transformed = @(b,x) L\f(b,x);
if nargin>7
[coeffs resnorm residuals e o l J] = lsqcurvefit(f_transformed, b0, x, L\y, varargin{1}, varargin{2}, varargin{3});
elseif nargin>5
[coeffs resnorm residuals e o l J] = lsqcurvefit(f_transformed, b0, x, L\y, varargin{1}, varargin{2});
else
[coeffs resnorm residuals e o l J] = lsqcurvefit(f_transformed, b0, x, L\y);
end
residuals = y - f(coeffs, x);
resnorm = sum(residuals.^2);
end