Borrar filtros
Borrar filtros

How to make linear fit?

5 visualizaciones (últimos 30 días)
nancy
nancy el 22 de Jun. de 2018
Comentada: Image Analyst el 24 de Jun. de 2018
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.

Respuestas (2)

Star Strider
Star Strider el 22 de Jun. de 2018
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 comentarios
nancy
nancy el 22 de Jun. de 2018
Hi,
I want to use polyfit command. But how can I write a code in the script to use polyfit?
Star Strider
Star Strider el 22 de Jun. de 2018
Editada: Star Strider el 24 de Jun. de 2018
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.

Iniciar sesión para comentar.


Image Analyst
Image Analyst el 23 de Jun. de 2018
See my attached polyfit() demo.
  2 comentarios
nancy
nancy el 24 de Jun. de 2018
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 el 24 de Jun. de 2018
The demo runs fine. How did you change it? Post your data and code.

Iniciar sesión para comentar.

Categorías

Más información sobre Linear and Nonlinear Regression en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by