How do i plot the line of best fit?
90 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Nathen Eberhardt
el 16 de Abr. de 2020
Comentada: Star Strider
el 16 de Abr. de 2020
So in Pressure Experiments.mat, there is (6,32) data set. I need the whole 5th row and the 6th row of this data.So I made a scatter plot of this data with :
%Load experimental data
load('PressureExperiments.mat', 'PData')
disp(PData)
%Seperating data
A = PData(5,:) %petrol pressure data
B = PData(6,:) %Number of escaping hydrocarbons
figure(1)
scatter(A,B)
How do i make a line of best fit for this? and plot the best line on the same figure?
Thanks
0 comentarios
Respuesta aceptada
Star Strider
el 16 de Abr. de 2020
Try this:
%Load experimental data
load('PressureExperiments.mat', 'PData')
% disp(PData)
%Seperating data
A = PData(5,:); %petrol pressure data
B = PData(6,:); %Number of escaping hydrocarbons
P = polyfit(A, B, 1); % Linear Fit
Bfit = polyval(P, A);
figure(1)
scatter(A,B)
hold on
plot(A, Bfit,'-r')
hold off
grid
If you want statistics on the fit, and you have the Statistics and Machine Learning Toolbox, use the regress or fitlm functions.
.
5 comentarios
Star Strider
el 16 de Abr. de 2020
The easiest way (assuming that the relation holds beyond the region-of-fit, not always a reliable assumption):
B = polyval(P, 10)
producing:
B =
66.3339
In general, it is not advisable to extrapolate that far beyond the region of it, unless (on the basis of other knowledge) you can be reasonably certain that the linear (or any other) relaionship is generally valid. With a mathematical model of this relationship, it would be possible to use linear or nonlinear parameter estimation techniques to devise a relation that would ber more generally applicable.
.
Más respuestas (1)
the cyclist
el 16 de Abr. de 2020
Use polyfit to find the line of best fit.
Use the plot command to plot that line
Use the hold command so that the new plot does not overwrite the scatter plot.
0 comentarios
Ver también
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!