Linear Regression Code Formula
27 ビュー (過去 30 日間)
古いコメントを表示
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
0 件のコメント
採用された回答
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 件のコメント
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Polynomials についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!