File Exchange

image thumbnail

Orthogonal Linear Regression

version (586 Bytes) by Per Sundqvist
Fits a line y=p0+p1*y to a dataset (xdata,ydata) in an orthogonal way.


Updated 13 Jan 2005

No License

% 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
% sum((p(1)+p(2)*xdata-ydata).^2/(1+p(2)^2)).
% Example:
% %prepare some data
% xdata=0:0.1:10;
% ydata=2+7*xdata+6*randn(size(xdata));
% %orthogonal linear fit
% p=linortfit(xdata,ydata)
% yy=p(1)+p(2)*xdata;
% %compare with normal linear regression
% p0=polyfit(xdata,ydata,1);
% yy0=polyval(p0,xdata);
% %plot to compare data with linear fits
% plot(xdata,ydata,'.',xdata,yy,xdata,yy0,':');

Cite As

Per Sundqvist (2020). Orthogonal Linear Regression (, MATLAB Central File Exchange. Retrieved .

Comments and Ratings (14)

Guilherme G. Neto

Toan Cao

hi, everybody
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 !

D. G.

This function is numerically and mathematically VERY poor. Use #16800 instead.

Ozgur Erdogan

So practical, thank you.

John Bennett

Saved me some thinking time!

yin gang

yin gang

John D'Errico

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.

Per Sundqvist

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

Rolf Schulte

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).

Per Sundqvist

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

min naing


hla myint


MATLAB Release Compatibility
Created with R13SP1
Compatible with any release
Platform Compatibility
Windows macOS Linux

Inspired: Orthogonal Linear Regression

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!