# SVD-Fit-Line

Version 0.01 (566 KB) by
Linear Least Squares Problems Using SVD, straight line fitting (2D and 3D)
16 Downloads
Updated 12 Aug 2023

# Linear Least Squares Problems Using SVD, straight line fitting (2D and 3D)

cuixingxing150@gmail.com

The set of points located in the plane points fitted to a straight line, using the SVD method can be very convenient to solve, given below are several methods to solve.

## Linear fitting of 2D point sets

x = 1:100;
y = x+10*randn(1,100);
plot(x,y,'ro',DisplayName="points");hold on;
legend

• Method1

Find the right singular vector corresponding to the largest singular value, i.e., the principal direction.

points = [x',y'];
A =  points- mean(points);
[U,S,V] = svd(A);
direction = V(:,1);% 主元方向
t = -10:100;
x_ = direction(1).*t;
y_ = direction(2).*t;
plot(x_,y_,'b-',DisplayName="svd fit")

legend;
• Method2

<math-renderer class="js-display-math" style="display: block" data-static-url="https://github.githubassets.com/static" data-run-id="77da7e281c1f4b5bf2f42b0e23308373">$$\left\lbrack U,S,V\right\rbrack =\textrm{svd}\left(A\right)$$</math-renderer>

I tried to make the <math-renderer class="js-inline-math" style="display: inline" data-static-url="https://github.githubassets.com/static" data-run-id="77da7e281c1f4b5bf2f42b0e23308373">$\textrm{Ax}=0$</math-renderer> form of non-zero least squares solution (the optimal solution is the column vector corresponding to the smallest singular value of V), so it was designed to be solved in the general form $ax+by+c=0$, but because a, b, c variables are not independent unless the constraints are imposed <math-renderer class="js-inline-math" style="display: inline" data-static-url="https://github.githubassets.com/static" data-run-id="77da7e281c1f4b5bf2f42b0e23308373">$a^2 +b^2 +c^2 =1$</math-renderer>, then it becomes a non-linear least squares, and so it was designed to be ineffective in the general form.

For the above data set and the general formula, if slightly modified, so that c = 1, the general formula becomes <math-renderer class="js-inline-math" style="display: inline" data-static-url="https://github.githubassets.com/static" data-run-id="77da7e281c1f4b5bf2f42b0e23308373">$a\ast x+b\ast y+1=0$</math-renderer>, this formula can express any plane can not cross the origin of the straight line, but the above data set to fit the straight line is obviously close to the origin, so the general formula is also invalid.

In summary, according to the characteristics of the data set, the general formula is best designed as a form <math-renderer class="js-inline-math" style="display: inline" data-static-url="https://github.githubassets.com/static" data-run-id="77da7e281c1f4b5bf2f42b0e23308373">$y=k*x+b$</math-renderer>, this formula can express any straight line in the plane can not be perpendicular to the <math-renderer class="js-inline-math" style="display: inline" data-static-url="https://github.githubassets.com/static" data-run-id="77da7e281c1f4b5bf2f42b0e23308373">$x$</math-renderer> axis. At this point the SVD solves for the non-chiral linear least squares solution <math-renderer class="js-inline-math" style="display: inline" data-static-url="https://github.githubassets.com/static" data-run-id="77da7e281c1f4b5bf2f42b0e23308373">$\textrm{Ax}=b$</math-renderer>.

% https://ww2.mathworks.cn/matlabcentral/answers/66555-using-svd-to-solve-systems-of-linear-equation-have-to-implement-direct-parameter-calibration-method
x = 1:100;
y = x+10*randn(1,100);
figure;plot(x,y,'ro');hold on;
A = [x',ones(length(x),1)];
b = y';
[U,S,V] = svd(A);
coff = (V*pinv(S)*U')*b; % or use coff = A\b;
fimplicit(@(x,y)coff(1)*x+coff(2)-y,DisplayName="svd ls fit")
legend;

## 3D point set linear fitting

%% fit 3d line
x = (1:100)+10*randn(1,100);
y = (1:100)+10*randn(1,100);
z = (1:100)+10*randn(1,100);
figure;
plot3(x,y,z,'b.')
grid on;hold on;

points = [x',y',z'];
avg = mean(points,1);
subtracted = points-avg;
[~,~,V] = svd(subtracted);
direction = V(:,1);
t = -100:100;
x_ = avg(1)+direction(1)*t;
y_ = avg(2)+direction(2)*t;
z_ = avg(3)+direction(3)*t;
plot3(avg(1),avg(2),avg(3),'ro')
plot3(x_,y_,z_,DisplayName="svd fit")
legend;

## References

http://graphics.ics.uci.edu/ICS6N/NewLectures/Lecture19.pdf

What is benefit to use SVD for solving Ax=b

### Cite As

cui,xingxing (2024). SVD-Fit-Line (https://github.com/cuixing158/SVD-Fit-Line/releases/tag/v0.01), GitHub. Retrieved .

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

### Community Treasure Hunt

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

Start Hunting!
Version Published Release Notes
0.01

To view or report issues in this GitHub add-on, visit the GitHub Repository.
To view or report issues in this GitHub add-on, visit the GitHub Repository.