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?
採用された回答
その他の回答 (4 件)
Image Analyst
2018 年 1 月 31 日
3 投票
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
2018 年 2 月 1 日
2 件のコメント
Image Analyst
2018 年 2 月 2 日
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
2018 年 2 月 2 日
You did see the complete demo I posted below, didn't you?
Image Analyst
2018 年 2 月 2 日
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 件のコメント
Zach Dunagan
2018 年 2 月 2 日
編集済み: Zach Dunagan
2018 年 2 月 2 日
Image Analyst
2018 年 2 月 2 日
One of the inputs to scatter() is the color of the markers.
Image Analyst
2018 年 2 月 3 日
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().
Zach Dunagan
2018 年 2 月 23 日
Image Analyst
2018 年 2 月 25 日
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
2018 年 2 月 26 日
Chandra Mouli
2020 年 4 月 29 日
0 投票
hey @Image Analyst can u help me in fitting my graph for a equation?
5 件のコメント
Image Analyst
2020 年 4 月 29 日
OK, sure, but you don't seem to have posted your question (yet). What is the link to it?
Chandra Mouli
2020 年 4 月 30 日
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 .
Image Analyst
2020 年 4 月 30 日
Just break it into pieces, like
numerator1 = .......
denominator1 = ......
numerator2 = .....
denominator2 = ......
etc. I'm sure you can do it.
Chandra Mouli
2020 年 4 月 30 日
Did u go through my pdf ?
Image Analyst
2020 年 4 月 30 日
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.
カテゴリ
ヘルプ センター および File Exchange で Graphics Performance についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!