Answer the question
In order to leave comments, you need to log in
How to use the method of least squares to implement polynomial regression in Octave?
There is a set of experimental data
%Экспериментальные данные x и y
x=[-2 -1.3 -0.6 0.1 0.8 1.5 2.2 2.9 3.6 4.3 5 5.7 6.4];
y=[-10 -5 0 0.7 0.8 2 3 5 8 30 60 100 2 3 8];
%Метод наименьших квадратов
function s=mnk(c)
global x;global y;
s=0;
for i=1:length(x)
s=s+(log(y(i))-c(1)-c(2)*log(x(i))*c(3)*x(i))^2
endfor
end
global x;global y;
k=1% Степень полинома
c=[2:1:3];
%Экспериментальные данные x и y
x=[-2 -1.3 -0.6 0.1 0.8 1.5 2.2 2.9 3.6 4.3 5 5.7 6.4];
y=[-10 -5 0 0.7 0.8 2 3 5 8 30 60 100 2 3 8];
B=polyfit(x,y,k)%Находим коэффициенты
X1=-10:0.1:100; %Область построения графика
Y1=polyval(B,X1);
plot(x,y,X1,Y1);
grid();
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question