Linear Regression Code Formula
Show older comments
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
Accepted Answer
More Answers (0)
Categories
Find more on Polynomials in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!