Polynomial graph(using plot function), Deflection Problem

9 次查看(过去 30 天)
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 个评论
Sanghyuk Kim
Sanghyuk Kim 2021-6-25
编辑:Sanghyuk Kim 2021-6-25
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 2021-6-25
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?

请先登录,再进行评论。

回答(1 个)

Sulaymon Eshkabilov
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
...

类别

Help CenterFile Exchange 中查找有关 Polynomials 的更多信息

产品


版本

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by