Problem 43553. Linear Least Squares (L2 fitting problem )
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
Problem Comments
-
1 Comment
Hello, Luca Fenzi. This looks like an interesting problem. However, it can still be improved a bit. (1) It is recommended that at least four test cases be provided, though more are often necessary to prevent hard-coded workarounds. (2) The Test Suite does not rigorously check the individual elements of the output vector, only their sum; that leaves it open to passing completely incorrect submissions as demonstrated in Solution 1575892. (3) In the Problem Statement, it is highly unlikely that the output would be exactly [4, -1]; the presence of random perturbations means that the output might be [4.3, -1.2] or [3.9, -0.8], say. Although there is a suitable allowance in the Test Suite for 'random' variation in the outputs, it would still be advisable to amend the Problem Statement. —DIV
Solution Comments
Show commentsProblem Recent Solvers15
Suggested Problems
-
2026 Solvers
-
Solve the set of simultaneous linear equations
420 Solvers
-
What is the distance from point P(x,y) to the line Ax + By + C = 0?
372 Solvers
-
684 Solvers
-
4946 Solvers
More from this Author2
Problem Tags
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!