Fitting a lines to a scatter plot?
308 views (last 30 days)
Show older comments
Hello,
I am taking a course with a ton of data analysis. I switched over to Engineering Equation Solver just because of the graphing capabilities, however I am not liking the limited options of using an array. The only reason I switched is because I am not too comfortable with plotting data and then fitting a line. Last data analysis assignment I had a lot of problems plotting lines on a scatter plot. What I am going to do now is take the array I have in EES and compress it to a matrix in Matlab.
Can someone explain to me how to fit a variety of trends to a scatter of data?
0 Comments
Accepted Answer
KSSV
on 31 Jan 2018
Fit = polyfit(x,y,1); % x = x data, y = y data, 1 = order of the polynomial i.e a straight line
plot(polyval(Fit,x))
1 Comment
Mehernaz Savai
on 27 May 2022
If you are looking to try out a variety of different fits for your data (Polynomial, Exponential, Smoothing spline etc.), consider Curve Fitting app from the Curve fitting Toolbox :
More Answers (4)
Image Analyst
on 31 Jan 2018
See my attached polyfit demo. The m-file will create this plot:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/181646/image.png)
You can make the graph as fancy as you desire. You have control over virtually everything in it, like marker size, line width, axes labels, font sizes and colors, legends, etc.
0 Comments
Zach Dunagan
on 1 Feb 2018
2 Comments
Image Analyst
on 2 Feb 2018
polyfit() should work. Did you try the demo I attached? Just plug in your x and y values and it should work. If you still can't figure it out, attach your workbook in a .zip file and I'll do it for you.
Image Analyst
on 2 Feb 2018
Try this:
% Initialization / clean-up code.
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 25;
% importing data from excel
fri_12 = xlsread('Fri12PM.xlsx'); % friday 12:00 PM
fri_150 = xlsread('Fri150PM.xlsx'); % friday 1:50 PM
tue_1 = xlsread('Tue1PM.xlsx'); % tuesday 1:00 PM
% delP venturi
delP_all_ven = [fri_12(:, 6); fri_150(:, 6); tue_1(:, 6)]; % inH2O
% converting inH2O to Pa
delP_ven_inH2O_Pa = 248.84 * delP_all_ven; % Pa
% delP fan
delP_all_fan = [fri_12(:, 5); fri_150(:, 5); tue_1(:, 5)];
% convert inH2O to Pa
delP_fan_inH2O_Pa = 248.84 * delP_all_fan; % Pa
% defined variables
rho_a = 1.225; % kg/m^3
C_v = 0.99;
D_tube = 5.096 * 0.0254; % convert from in to m
A_t = (pi * D_tube^2) / 4;
d1 = D_tube;
d2 = 2.6 * 0.0254; % convert from in to m
beta = d2/d1;
E = 1 / sqrt(1 - beta^4);
i = 1;
% equations
for i = 1:length(delP_all_fan)
% venturimeter flow rate m^3/s
Q_ven(i) = C_v * E * A_t * sqrt((2 * delP_all_ven(i)) / (rho_a));
% flow power supplied by fan
B_fL(i) = Q_ven(i) * delP_all_fan(i);
end
% Plot ven volume flow rate vs fan power
subplot(1, 2, 1);
scatter(Q_ven, B_fL, 'filled')
grid on;
xlabel('Volumetric Flow Rate (m^3 / sec)', 'FontSize', fontSize);
ylabel('Power (Watts)', 'FontSize', fontSize);
title('Power vs. Volumetric Flow Rate', 'FontSize', fontSize);
% Volumetric volume flow rate vs delP fan
subplot(1, 2, 2);
scatter(Q_ven, delP_fan_inH2O_Pa, 'filled');
grid on;
xlabel('Volumetric Flow Rate (m^3 / sec)', 'FontSize', fontSize);
ylabel('Delta Pressure Venturimeter (Pa)', 'FontSize', fontSize);
title('Volumetric Flow Rate vs. Delta Pressure Venturi', 'FontSize', fontSize);
% Enlarge figure to full screen.
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0, 0.04, 1, 0.96]);
% Make Q_ven and B_fL column vectors like the other variables.
Q_ven = Q_ven(:);
B_fL = B_fL(:);
%-------------------------------------------------------
% Data fitting to a line.
order = 1; % Or 2 to a quadratic.
% First the first plot.
coefficients1 = polyfit(Q_ven, B_fL, order);
% Make new x coordinates
x1 = linspace(min(Q_ven), max(Q_ven), 1000);
y1 = polyval(coefficients1, x1);
subplot(1, 2, 1);
hold on;
plot(x1, y1, 'r-', 'LineWidth', 3);
% Now the second plot.
coefficients2 = polyfit(Q_ven, delP_fan_inH2O_Pa, order);
% Make new x coordinates
x2 = linspace(min(Q_ven), max(Q_ven), 1000);
y2 = polyval(coefficients2, x2);
subplot(1, 2, 2);
hold on;
plot(x2, y2, 'r-', 'LineWidth', 3);
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/181759/image.png)
6 Comments
Image Analyst
on 25 Feb 2018
Here is your for loop:
for i = 1:15
% for 5 points per plate
for j = 1:5;
% 5x15 matrix for ven and fan
ven_m(j, i) = ven(i * j);
fan_m(j, i) = fan(i * j);
% venturi flow rate
Q_ven(j, i) = C_v * E * A_t * sqrt((2 .* ven_m(j, i)) / (rho_a));
% flow power supplied by fan
B_fL(j, i) = Q_ven(j, i) * fan_m(j, i);
end
end
Tell me what line you used subplot on, because I'm not seeing it.
Chandra Mouli
on 29 Apr 2020
hey @Image Analyst can u help me in fitting my graph for a equation?
5 Comments
Image Analyst
on 30 Apr 2020
Yes, I looked at it. I think it's perfectly within the capabilities of a smart engineer like you to create each term of that equation. It's not so hard if you just break it down into smaller chunks and then reassemble.
See Also
Categories
Find more on Modeling and Prediction in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!