Fitting a lines to a scatter plot?

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?

 Accepted Answer

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

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 :

Sign in to comment.

More Answers (4)

Image Analyst
Image Analyst on 31 Jan 2018
See my attached polyfit demo. The m-file will create this plot:
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.
Here is my code... How would I fit a trend line to this scatter plot?
% clear variables and windows
clc, clear, clear all
% 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
% ven volume flow rate vs fan power
figure('Name', 'plot 1');
scatter(Q_ven, B_fL, 'filled')
xlabel('volumetric flow rate (m^3/s)');
ylabel('power (W)');
title('Power vs Volumetric Flow Rate');
% ven volume flow rate vs delP fan
figure('Name', 'plot 2');
scatter(Q_ven, delP_fan_inH2O_Pa, 'filled');
xlabel('volumetric flow rate (m^3/s)');
ylabel('delta pressure venturimeter (Pa)');
title('Volumetric Flow Rate vs Delta Pressure Venturi');
Hmmm, can't upload my excel files. Attached is a photo of both plots. If one of you wants me to send you the excel files I can through email.

2 Comments

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.
You did see the complete demo I posted below, didn't you?

Sign in to comment.

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);

6 Comments

Is there a way to do these plots in a loop and how to plot a trend line to each 5 points per plate?
How can I change the color because if I used filled for all of them it uses the same color over again. I have repeats.
% clear variables and windows
clc, clear, clear all
% 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
ven = 248.84 * [fri_12(:, 6); fri_150(:, 6); tue_1(:, 6)];
fan = 248.84 * [fri_12(:, 5); fri_150(:, 5); tue_1(:, 5)];
% 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;
k = 1;
% for plates 1 - 15
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
% power with respect to venturi volumetric flow
figure('Name', 'plot 2');
scatter(Q_ven(:, 1), B_fL(:, 1), 'filled')
hold on;
scatter(Q_ven(:, 2), B_fL(:, 2), 'filled')
hold on;
scatter(Q_ven(:, 3), B_fL(:, 3), 'filled')
hold on;
scatter(Q_ven(:, 4), B_fL(:, 4), 'filled')
hold on;
scatter(Q_ven(:, 5), B_fL(:, 5), 'filled')
hold on;
scatter(Q_ven(:, 6), B_fL(:, 6), 'filled')
hold on;
scatter(Q_ven(:, 7), B_fL(:, 7), 'filled')
hold on;
scatter(Q_ven(:, 8), B_fL(:, 8), 'filled')
hold on;
scatter(Q_ven(:, 9), B_fL(:, 9), 'filled')
hold on;
scatter(Q_ven(:, 10), B_fL(:, 10), 'filled')
hold on;
scatter(Q_ven(:, 1), B_fL(:, 11), 'filled')
hold on;
scatter(Q_ven(:, 12), B_fL(:, 12), 'filled')
hold on;
scatter(Q_ven(:, 13), B_fL(:, 13), 'filled')
hold on;
scatter(Q_ven(:, 14), B_fL(:, 14), 'filled')
hold on;
scatter(Q_ven(:, 15), B_fL(:, 15), 'filled')
grid on;
legend('D = 133 mm', 'D = 88.87 mm', 'D = 24.45 mm', 'D = 50.8 mm', 'D = 65 mm', 'D = 76.3 mm', 'D = 25.4 mm', 'D = 50.8 mm', 'D = 50.8 mm', 'D = 65 mm', 'D = 65 mm', 'D = 76.3 mm', 'D = 88.87 mm', 'D = 133 mm ', 'D = 130 mm');
xlabel('Q_ven (m^3/s)');
ylabel('Power (W)');
title('Q_ven vs B_fL');
% delP fan with respect to ventrui volumetric flow
figure('Name', 'plot 2');
scatter(Q_ven(:, 1), fan_m(:, 1), 'filled')
hold on;
scatter(Q_ven(:, 2), fan_m(:, 2), 'filled')
hold on;
scatter(Q_ven(:, 3), fan_m(:, 3), 'filled')
hold on;
scatter(Q_ven(:, 4), fan_m(:, 4), 'filled')
hold on;
scatter(Q_ven(:, 5), fan_m(:, 5), 'filled')
hold on;
scatter(Q_ven(:, 6), fan_m(:, 6), 'filled')
hold on;
scatter(Q_ven(:, 7), fan_m(:, 7), 'filled')
hold on;
scatter(Q_ven(:, 8), fan_m(:, 8), 'filled')
hold on;
scatter(Q_ven(:, 9), fan_m(:, 9), 'filled')
hold on;
scatter(Q_ven(:, 10), fan_m(:, 10), 'filled')
hold on;
scatter(Q_ven(:, 1), fan_m(:, 11), 'filled')
hold on;
scatter(Q_ven(:, 12), fan_m(:, 12), 'filled')
hold on;
scatter(Q_ven(:, 13), fan_m(:, 13), 'filled')
hold on;
scatter(Q_ven(:, 14), fan_m(:, 14), 'filled')
hold on;
scatter(Q_ven(:, 15), fan_m(:, 15), 'filled')
grid on;
xlabel('Q_ven (m^3/s)');
ylabel('delP (Pa)');
title('delP vs Q_ven');
One of the inputs to scatter() is the color of the markers.
Use subplot(). Did you ever look at my code? Notice how I got rid of those figure calls you made and replaced them with subplot().
I used subplot.
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.
I will show you what I did. I am not worried about it because I got my plots to work with a fitted line and this was one of my first homework assignments for a class.
% author: Zach Dunagan
% clear variables and windows
clc, clear, close all
% importing data from excel
data = xlsread('allData.xlsx');
% extracts columns from data
ven = 248.84 * [data(:, 6)]; % extracts venturi data
fan = 248.84 * [data(:, 5)]; % extracts fan data
A_elec = [data(:, 8)]; % extracts power data
% defined variables
rho_a = 1.225; % kg/m^3
C_v = 0.99; % constant
D_ven = 2.6 * 0.0254; % convert from in to m
A_t = (pi * D_ven^2) / 4; % area of the throat m^2
d1 = 5.096 * 0.0254; % flow tube diameter, convert from in to m
d2 = 2.6 * 0.0254; % venturi flow diameter
beta = d2/d1; % diameter ratio for approach factor
E = 1 / sqrt(1 - beta^4); % approach factor
% different line styles and colors
style = {'-r','-k','-b','-.r','-.k','-.b','-or','-ok','-ob'};
i = 1;
j = 1;
p = 0;
% for plates 1 - 9
for i = 1:9
% 5 data points per plate
for j = 1:5
p = p + 1;
% stores ven_m and fan_m to floating point
ven_m(j, i) = ven(p);
fan_m(j, i) = fan(p);
A_elec_m(j, i) = A_elec(p);
% 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);
% pressure head
H(j, i) = B_fL(j, i) / (rho_a * Q_ven(j, i));
% delP fan
delP_fan(j, i) = fan(p);
% efficiency eta
% A_fL = B_fL
n(j, i) = (B_fL(j, i) / A_elec_m(j, i));
end
% fan electric power vs Q dot ven
subplot(2, 3, 1);
%figure(i:4);
g1 = polyfit(Q_ven(:, i), A_elec_m(:, i), 2);
x1 = linspace(Q_ven(1, i) ,Q_ven(end, i), 9);
f1 = polyval(g1, x1);
plot(x1, f1, style{i})
xlabel('Q dot (m^3/s)');
ylabel('Power (W)');
hold on;
grid minor;
title('Fan Electric Power vs Ventrui Volumetric Flow');
% pressure head vs Q dot ven
subplot(2, 3, 2);
%figure(i, j);
g2 = polyfit(Q_ven(:, i), H(:, i), 2);
x2 = linspace(Q_ven(1, i) ,Q_ven(end, i), 9);
f2 = polyval(g2, x2);
plot(x2, f2, style{i})
xlabel('Q dot (m^3/s)');
ylabel('Pressure Head (m)');
hold on;
grid minor;
title('Fan Pressure Head vs Ventrui Volumetric Flow');
% delta P fan vs Q dot ven
subplot(2, 3, 4);
%figure(i, j);
g3 = polyfit(Q_ven(:, i), delP_fan(:, i), 2);
x3 = linspace(Q_ven(1, i) ,Q_ven(end, i), 9);
f3 = polyval(g3, x3);
plot(x3, f3, style{i})
xlabel('Q dot (m^3/s)');
ylabel('delP fan (Pa)');
hold on;
grid minor;
title('delP Across Fan vs Ventrui Volumetric Flow');
% efficiency vs Q dot ven
subplot(2, 3, 5)
%figure(i, j);
g4 = polyfit(Q_ven(:, i), n(:, i), 2);
x4 = linspace(Q_ven(1, i) ,Q_ven(end, i), 9);
f4 = polyval(g4, x4);
plot(x4, f4, style{i})
xlabel('Q dot (m^3/s)');
ylabel('percent (%)');
hold on;
grid minor;
title('Eta vs Ventrui Volumetric Flow');
end
legend('D = 25.45 mm', 'D = 50.80 mm', 'D = 50.80 mm', 'D = 65.00 mm', 'D = 65.00 mm', 'D = 76.30 mm', 'D = 88.87 mm', 'D = 133.00 mm', 'D = 130.00 mm');

Sign in to comment.

Chandra Mouli
Chandra Mouli on 29 Apr 2020
hey @Image Analyst can u help me in fitting my graph for a equation?

5 Comments

OK, sure, but you don't seem to have posted your question (yet). What is the link to it?
see the formulae of mine is veru huge ....and i am gettig no idea how to do it....i attached my pdf document , in that the formulae -2 .
Just break it into pieces, like
numerator1 = .......
denominator1 = ......
numerator2 = .....
denominator2 = ......
etc. I'm sure you can do it.
Did u go through my pdf ?
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.

Sign in to comment.

Categories

Find more on Graphics Performance in Help Center and File Exchange

Asked:

on 31 Jan 2018

Commented:

on 27 May 2022

Community Treasure Hunt

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

Start Hunting!