How do I perform a linear least squares fit

283 visualizaciones (últimos 30 días)
Alexander MacFarlane IV
Alexander MacFarlane IV el 21 de Dic. de 2018
Editada: Tamas Kis el 26 de Jul. de 2021
Hello.
I would like to perform a linear least squares fit to 3 data points.
The help files are very confusing, to the point where i can't figure out whether this is a base function of Matlab, I need the curve fitting toolbox, optimization toolbox, or both.
Thanks,
Alex

Respuesta aceptada

Star Strider
Star Strider el 21 de Dic. de 2018
You don’t need any toolboxes. Use polyfit (link) and polyval functions, or the mldivide (link) operator.

Más respuestas (2)

Image Analyst
Image Analyst el 21 de Dic. de 2018
Editada: Image Analyst el 21 de Dic. de 2018
See attached polyfit demo. Adapt as needed. All you need is base MATLAB - no toolboxes.
For example
coefficients = polyfit(x, y, 1);
yFitted = polyval(coefficients, x); % yFitted will be at the 3 points where x is. There will be 3 yFitted values.
If you want a lot more points, you can pass in more x to polyval():
xFit = linspace(min(x), max(x), 1000); % 1000 points.
yFitted = polyval(coefficients, xFit);
  1 comentario
Alexander MacFarlane IV
Alexander MacFarlane IV el 21 de Dic. de 2018
Thanks Star Strider and Image Analyst!
You guys are amazingly fast.

Iniciar sesión para comentar.


Tamas Kis
Tamas Kis el 25 de Jul. de 2021
Editada: Tamas Kis el 26 de Jul. de 2021
Function to perform curve fitting using linear least squares:
Linear and polynomial fits are linear least square fits, while the other fits (power, exponential, logarithmic) are approximated by first linearizing the data.

Categorías

Más información sobre Interpolation en Help Center y File Exchange.

Etiquetas

Productos


Versión

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by