Problem 258. linear least squares fitting
Inputs:
- f: cell-array of function handles
- x: column vector of x values
- y: column vector of y values, same length as x
Output:
- a: column vector of coefficients, same length as f
In a correct answer the coefficients a take values such that the function
fit = @(x) a(1)*f{1}(x) + a(2)*f{2}(x) + a(3)*f{3}(x) +...+ a(end)*f{end}(x)
minimizes the sum of the squared deviations between fit(x) and y, i.e. sum((fit(x)-y).^2) is minimal.
Remarks:
- The functions will all be vectorized, so e.g. f{1}(x) will return results for the whole vector x
- The absolute errors of a must be smaller than 1e-6 to pass the tests
Solution Stats
Problem Comments
-
6 Comments
your functions are not all truly 'vectorized'; I would change @(x)1 to @(x)ones(size(x)) for a more consistent 'vectorized' behavior...
Yup, you're right. I made the adjustment...
not meaning to be picky, but in the third test you might want to use aref.' instead of aref' (if x take negative values, which occurs at random for this test, then log(x) takes complex values, and the aref' portion takes the conjugate of the complex valued results); perhaps it is simpler to make sure x takes only positive values...
and nice problem, by the way!
Thanks! Nice catch, I fixed that second issue too (those randomized tests have their drawbacks)
Sorry, but I can not understand the problem to solve.
Can anyone explain to me, please?
Solution Comments
Show commentsProblem Recent Solvers98
Suggested Problems
-
Back to basics 8 - Matrix Diagonals
924 Solvers
-
Make an awesome ramp for a tiny motorcycle stuntman
654 Solvers
-
Simple equation: Annual salary
4183 Solvers
-
Sum of diagonals elements of a matrix
222 Solvers
-
894 Solvers
More from this Author7
Problem Tags
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!