Fitting a lines to a scatter plot?

383 Ansichten (letzte 30 Tage)
Zach Dunagan
Zach Dunagan am 31 Jan. 2018
Kommentiert: Mehernaz Savai am 27 Mai 2022
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?

Akzeptierte Antwort

KSSV
KSSV am 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 Kommentar
Mehernaz Savai
Mehernaz Savai am 27 Mai 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 :

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (4)

Image Analyst
Image Analyst am 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.

Zach Dunagan
Zach Dunagan am 1 Feb. 2018
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 Kommentare
Image Analyst
Image Analyst am 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
Image Analyst am 2 Feb. 2018
You did see the complete demo I posted below, didn't you?

Melden Sie sich an, um zu kommentieren.


Image Analyst
Image Analyst am 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);
  6 Kommentare
Image Analyst
Image Analyst am 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.
Zach Dunagan
Zach Dunagan am 26 Feb. 2018
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');

Melden Sie sich an, um zu kommentieren.


Chandra Mouli
Chandra Mouli am 29 Apr. 2020
hey @Image Analyst can u help me in fitting my graph for a equation?
  5 Kommentare
Chandra Mouli
Chandra Mouli am 30 Apr. 2020
Did u go through my pdf ?
Image Analyst
Image Analyst am 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.

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Particle & Nuclear Physics finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by