Linear Least Squares (L2 fitting problem ) - MATLAB Cody - MATLAB Central

Problem 43553. Linear Least Squares (L2 fitting problem )

Difficulty:Rate

Given a set of real measurements

     (x(i), y(i))

find a line sol(1)x + sol(2) (more specifically furnish the vector with sol=[sol(1),sol(2)])such that it fits the data (it minimises the 2 norm)

Example: Input:

% INPUT
x=linspace(0,1,50);
y=4*x-1+ randn(50,1); % perturbed observations
% SOLUTION:
sol=[4,-1]

HINT : This problem can be expressed as a convex optimisation problem:

min_{sol} sum(sol(1)*x+sol(2)-y)^2   

Suggestion: use the following code to test your function:

plot(x,y,'.') % plot the data
hold on
plot(x,sol(1)*x+sol(2))
legend('measurements', 'L2 fit')

Solution Stats

53.57% Correct | 46.43% Incorrect
Last Solution submitted on Mar 21, 2023

Problem Comments

Solution Comments

Show comments
Registration Now Open for MathWorks AUTOMOTIVE CONFERENCE 2025
...
Hello Community, We're excited to announce that registration is now open for the...

Problem Recent Solvers15

Suggested Problems

More from this Author2

Community Treasure Hunt

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

Start Hunting!
Go to top of page