How do i plot the line of best fit?

234 次查看(过去 30 天)
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

采纳的回答

Star Strider
Star Strider 2020-4-16
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 个评论
Nathen Eberhardt
Nathen Eberhardt 2020-4-16
I got one more question, how would i find the value of the number of hyrdrocarbons ecaping when the petrol pressure is ten? So How do i find B when A = 10 using the best fit line?
Star Strider
Star Strider 2020-4-16
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.
.

请先登录,再进行评论。

更多回答(1 个)

the cyclist
the cyclist 2020-4-16
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.

Community Treasure Hunt

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

Start Hunting!

Translated by