T
T
The_Beginer2021-04-06 16:29:53
MATLAB
The_Beginer, 2021-04-06 16:29:53

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];


Is there a least squares method?
%Метод наименьших квадратов
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


And the working, but not completed part of the program on GNU Octave (I indicated in the MATLAB tags), but the programming language for both systems of computer mathematics is almost identical.

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();


When you run this program on the command line, ans = 0 comes out and no graph is plotted.
How can I further implement my task, that is, using the least squares method to solve the problem of polynomial regression.

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question