Polynomial graph(using plot function), Deflection Problem
10 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos

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 comentarios
Respuestas (1)
Sulaymon Eshkabilov
el 25 de Jun. de 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
...
0 comentarios
Ver también
Categorías
Más información sobre Polynomials en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!