Linear Regression Code Formula

function [p_UDF] = LinReg(x, fx)
n = length(x)
sum_x = 0;
sum_x2 = 0;
sum_x3 = 0;
sum_x4 = 0;
sum_y = 0;
sum_xy = 0;
sum_x2y = 0;
for ii = 1:n
sum_x = sum_x + x(ii);
sum_x2 = sum_x2 + (x(ii)^2);
sum_x3 = sum_x3 + (x(ii)^3);
sum_x4 = sum_x4 + (x(ii)^4);
sum_y = sum_y + fx(ii);
sum_xy = (x(ii)*fx(ii));
sum_x2y = (x(ii)^2)*(fx(ii));
end
A = [n sum_x sum_x2;sum_x sum_x2 sum_x3;sum_x sum_x2 sum_x3 sum_x4]
Y = [sum_y;sum_xy;sum_x2y];
p_UDF = A\Y
% Script
x = 0:10
y = [%enter y-values]
p_UDF = LinReg(x,y)
p_FIT = polyfit(x,y)
myFIT = polyval(p_UDF,x)
plot(x,y,'or',x,myFIT)
_______________________________________________________________________________________________________________________________________
Can someone just please explain to me the difference between polyfit and polyval? I'm struggling a lot with figuring out the difference and how this code uses it

 Respuesta aceptada

Star Strider
Star Strider el 24 de Abr. de 2019

0 votos

The polyfit function creates a least-squares fit of the data to a polynomial, given the independent and dependent variable data, and the desired polynomial degree.
The polyval function uses the polynomial coefficients returned by polyfit, and the desired independent variable vector, and calculates the value of the fitted polynomial at the values of the independent variable supplied to it.

Más respuestas (0)

Categorías

Más información sobre Polynomials en Centro de ayuda y File Exchange.

Preguntada:

el 24 de Abr. de 2019

Respondida:

el 24 de Abr. de 2019

Community Treasure Hunt

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

Start Hunting!

Translated by