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
采纳的回答
更多回答(0 个)
类别
在 帮助中心 和 File Exchange 中查找有关 Polynomials 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!