Polynomial graph(using plot function), Deflection Problem
이전 댓글 표시

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
Robert Daly
2021년 6월 25일
I think this is just what polynomials do.
Have you tried the native matlab function for fitting polynomials?
p = polyfit(x,y,n)
gives thepolynomial parametrs that fit the best for an n degree polynomial.
then
y1 = polyval(p,x1)
is used to create y vales using the parameters and the given x coordinates.
Sanghyuk Kim
2021년 6월 25일
편집: Sanghyuk Kim
2021년 6월 25일
Sanghyuk Kim
2021년 6월 25일
답변 (1개)
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
...
카테고리
도움말 센터 및 File Exchange에서 Polynomials에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!