Hi; I want to plot a linear fit for the graphic in the attached file. And also I want to calculate slope of this linear fit? How can I do this? File is in the attached file.. Thanks a lot.

답변 (2개)

Star Strider
Star Strider 2018년 6월 22일

0 개 추천

Try this:
I = openfig('test.fig');
Ax = gca;
L1 = findobj(Ax, 'Type','line'); % Find ‘line’ Object
x = L1.XData; % Extract X-Data
y = L1.YData; % Extract Y-Data
XL = [min(x) max(x)]; % Get X-Limits
P = [x(:), ones(size(x(:)))] \ y(:); % Estimate Linear Fit Parameters
LinFit = [XL(:) [1;1]] * P; % Linear Fit
Slope = P(1);
hold on
plot(XL, LinFit, '-r', 'LineWidth',1.5)
hold off

댓글 수: 5

Steven Lord
Steven Lord 2018년 6월 22일
Or consider using the Basic Fitting UI directly in your figure.
Matt Dickson
Matt Dickson 2018년 6월 22일
Also consider using polyfit to perform the regression and simplify the code, if you want it done in the script.
Star Strider
Star Strider 2018년 6월 22일
My code estimates, calculates, and plots a linear fit.
It estimates the parameters as ‘P’, and then calculates a regression line in ‘LinFit’. This is likely more efficient than polyfit and polyval for a simple linear fit.
nancy
nancy 2018년 6월 22일
Hi,
I want to use polyfit command. But how can I write a code in the script to use polyfit?
Star Strider
Star Strider 2018년 6월 22일
편집: Star Strider 2018년 6월 24일
To use polyfit:
I = openfig('test.fig');
L = findobj(gca, 'Type','line'); % Find ‘line’ Object
x = L(2).XData; % Extract X-Data
y = L(2).YData; % Extract Y-Data
x = x(y > -2); % Remove Outliers
y = y(y > -2); % Remove Outliers
XL = [min(x) max(x)]; % Get X-Limits
P = polyfit(x, y, 1); % Estimate Linear Fit Parameters
LinFit = polyval(P, XL); % Linear Fit
Slope = P(1);
figure
plot(x, y)
hold on
plot(XL, LinFit, '-r', 'LineWidth',1.5)
hold off
EDIT Added plot image.

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

Image Analyst
Image Analyst 2018년 6월 23일

0 개 추천

See my attached polyfit() demo.

댓글 수: 2

nancy
nancy 2018년 6월 24일
When I execute the code; I have below message:
Polynomial is not unique; degree >= number of data points. > In polyfit at 71 In Untitled at 7
How can I solve it? Thanks a lot..
Image Analyst
Image Analyst 2018년 6월 24일
The demo runs fine. How did you change it? Post your data and code.

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

카테고리

도움말 센터File Exchange에서 Linear and Nonlinear Regression에 대해 자세히 알아보기

질문:

2018년 6월 22일

댓글:

2018년 6월 24일

Community Treasure Hunt

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

Start Hunting!

Translated by