linear least squares fitting - MATLAB Cody - MATLAB Central

Problem 258. linear least squares fitting

Difficulty:Rate

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

36.97% Correct | 63.03% Incorrect
Last Solution submitted on May 06, 2025

Problem Comments

Solution Comments

Show comments
LLMs with MATLAB updated to support the latest OpenAI Models
Large Languge model with MATLAB, a free add-on that lets you access...
2
3

Problem Recent Solvers109

Community Treasure Hunt

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

Start Hunting!
Go to top of page