Polynomial graph(using plot function), Deflection Problem

조회 수: 9 (최근 30일)
Sanghyuk Kim
Sanghyuk Kim 2021년 6월 25일
답변: Sulaymon Eshkabilov 2021년 6월 25일
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
Sulaymon Eshkabilov 2021년 6월 25일
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