Polynomial graph(using plot function), Deflection Problem

9 Ansichten (letzte 30 Tage)
Sanghyuk Kim
Sanghyuk Kim am 25 Jun. 2021
I implemented the first, second, and third regression models for the data and showed them using the plot function.
But, for the 3rd regression model, the graph was bent, such as the part in red.
How can we solve this problem?
I think a 3rd regression model is like a 3rd polynomial, so there should be no bends.
This is the code
load accidents
x = hwydata(:,6); %Population of states
y = hwydata(:,4); %Accidents per state
scatter(x,y,'filled')
X = [ones(size(x)) x];
b = regress(y,X)
hold on
YFIT = b(1) + b(2)*x;
plot(x,YFIT)
X2 = [ones(size(x)) x x.^2];
b2 = regress(y,X2);
YFIT2 = b2(1) + b2(2)*x + b2(3)*x.^2 ;
plot(x,YFIT2)
X3 = [ones(size(x)) x x.^2 x.^3];
b3 = regress(y,X3);
YFIT3 = b3(1) + b3(2)*x + b3(3)*x.^2 + b3(4)*x.^3 ;
plot(x,YFIT3)
xlabel('Registered vehicles[thousands](x)');
ylabel('Accidents per state(y)');
plot1_legend=legend('Data','1st Order','2nd Order', '3rd Order')
hold off
  3 Kommentare
Sanghyuk Kim
Sanghyuk Kim am 25 Jun. 2021
Bearbeitet: Sanghyuk Kim am 25 Jun. 2021
I think
p = polyfit(x,y,n)
is using for interpolation model that includes all data. I want to implement a regression model.
i.e. it is not a model that includes all data.
thanks for your Ans.
Sanghyuk Kim
Sanghyuk Kim am 25 Jun. 2021
3rd regression model is
YFIT3 = 201.6374 + 0.0815*x + (9.7323e-6)*x.^2 - (2.6249e-10)*x.^3
My guess is that the error is probably due to the coefficient being too small.
how to fix it?

Melden Sie sich an, um zu kommentieren.

Antworten (1)

Sulaymon Eshkabilov
Sulaymon Eshkabilov am 25 Jun. 2021
You need to employ polyfit() for your anticipated fit models or just least squares method with Vandermonde matrix with \. SInce you are trying to find a fit model with a single variable x.
E.g.:
x = hwydata(:,6); %Population of states
y = hwydata(:,4); %Accidents per state
FITmodel1 = polyfit(x, y, 1); % Linear fit
FITmodel2 = polyfit(x, y, 2); % Quadratic fit
FITmodel3 = polyfit(x, y, 3); % Cubic fit
%% OR
X1 = [ones(size(x)) x ];
FITmodel1 = X1\y; % Linear fit model
FITmodel1_val = FITmodel1(1)+FITmodel1(2)*x; % Calculated vals of Lin. fit model
X2 = [ones(size(x)) x x.^2];
FITmodel1 = X2\y; % Quadratic fit model
FITmodel1_val = FITmodel1(1)+FITmodel1(2)*x+FITmodel1(2)*x.^2; % Calculated vals of Quad. fit model
...

Kategorien

Mehr zu Polynomials finden Sie in Help Center und File Exchange

Produkte


Version

R2021a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by