% Orthogonal linear least square fit of xdata and ydata vectors
% p=linortfit(xdata,ydata) gives the the coefficient-vector p that
% corresponds to the linear expression: y=p(1)+p(2)*x, where p
% is minimized with respect to the objective function
% %prepare some data
% %orthogonal linear fit
% %compare with normal linear regression
% %plot to compare data with linear fits
Per Sundqvist (2021). Orthogonal Linear Regression (https://www.mathworks.com/matlabcentral/fileexchange/6716-orthogonal-linear-regression), MATLAB Central File Exchange. Retrieved .
I have a set of data points in 2D: (x,y)
I would like to fit them into a straight line that is described by formula: x.cosθ +y.sinθ= ρ, in stead of: y=ax+b
How can i find two parameters : θ, ρ
Looking forward to your reply!
Thank you very much !
This function is numerically and mathematically VERY poor. Use #16800 instead.
So practical, thank you.
Saved me some thinking time!
Clearly there is a difference between linear regression and orthogonal regression, but the ORTHOGONAL regression is indeed very solvable with either svd or eig. Fminsearch is not at all necessary, and is in fact a very inefficient way to solve this problem. Its not even that accurate.
Derivatives of sum((p(1)+p(2)*xdata-ydata).^2/(1+p(2)^2)) w.r.t p gives NOT a linear system to solve! It is easy to proof. Therefore you MUST use fminsearch. I.e., there is difference between linear regression and orthogonal fitting of a line to data points
Total least squares is a linear problem (see contribution with SVD solution). Using a non-linear optimiser (fminsearch) leads to unnecessary problems (getting stuck in local minima; inefficient; etc).
Ok, what dou you mean by approximation?
Another approach, orthogonal linear fit using a SVD approximation:
M = [mean(xdata) mean(ydata)] ;
[U,S,V]=svd( [(xdata - mean(xdata), ydata - mean(ydata)] );
N=(-1/V(end,end)) * V(:,end); % scaling
P = [N(1) - M*N] ; % Slope and Intercept
Inspired: Orthogonal Linear Regression
Find the treasures in MATLAB Central and discover how the community can help you!Start Hunting!