필터 지우기
필터 지우기

How can I add a a best fit line, a scatter plot data? having trouble

조회 수: 84 (최근 30일)
juan Ortiz
juan Ortiz 2019년 4월 6일
답변: Saeed Bello 2021년 8월 29일
% using the excel data
[FileName, PathName] = uigetfile('*.xlsx','Select Excel files to analyze:','MultiSelect','off');
[status, sheets] = xlsfinfo([PathName, FileName]);
data_summary = xlsread([PathName, FileName],sheets{1});
% using struct to structure the data
data = struct('Heights',[],'Qs',[]);
data.heights=(data_summary(1:5,1));
data.flowrates=(data_summary(1:5,2:11));
figure (1)
for i=1:5
subplot(2,3,i)
histogram(data.flowrates(i,1:10),10)
xlabel('Volumetric flow rates [mL/s]','FontSize',10,'FontWeight','bold');
ylabel('Number of occurances','FontSize',10,'FontWeight','bold');
legend(['water exit height=' num2str(data.heights(i))]);
end
% Adding the average flow rate histogram
Qavg=mean(data.flowrates,2);
Qstd=std(data.flowrates,0,2);
errQ=Qstd./2;
subplot(2,3,6)
% fit=polyfit(data.flowrates,1);
% plot(polyval(fit,data.heights))
errorbar(data.heights,Qavg,errQ,'ro')
xlabel('Water exit height [cm]','FontSize',10,'FontWeight','bold');
ylabel('Flow rate [ml/s]','FontSize',10,'FontWeight','bold');
**
% I keep getting an error with what I have so far
  댓글 수: 1
Rik
Rik 2019년 4월 6일
You did not attach the excel file, nor did you post the complete error message.

댓글을 달려면 로그인하십시오.

답변 (2개)

Cris LaPierre
Cris LaPierre 2019년 4월 6일
I'd use polyfit.
This example shows you to do a simple linear regression.
x = 1:50;
y = -0.3*x + 2*randn(1,50);
p = polyfit(x,y,1);
f = polyval(p,x);
plot(x,y,'o',x,f,'-')
legend('data','linear fit')

Saeed Bello
Saeed Bello 2021년 8월 29일

Just add lsline after your scatter plot expression. For example

scatter(x,y)
lsline

카테고리

Help CenterFile Exchange에서 Interpolation에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by