Linear Regression Code Formula

조회 수: 22 (최근 30일)
Aisha Shamim
Aisha Shamim 2019년 4월 24일
답변: Star Strider 2019년 4월 24일
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

채택된 답변

Star Strider
Star Strider 2019년 4월 24일
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.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Polynomials에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by