# I have x and y data, How can I do linear fit to the data, find out c of linear fit line and slope with respect to y=m*x ?

116 views (last 30 days)

Show older comments

##### 0 Comments

### Answers (2)

Image Analyst
on 8 Dec 2013

Edited: Image Analyst
on 8 Dec 2013

See my demo. The main lines to focus on are

linearCoefficients = polyfit(x, y, 1)

yFit = polyval(linearCoefficients, xFit);

Here's the demo.

% Demo to illustrate how to use the polyfit routine to fit data to a polynomial

% and to use polyval() to get estimated (fitted) data from the coefficients that polyfit() returns.

% Demo first uses a linear fit, then uses a cubic fit.

% Initialization steps.

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 = 20;

%============= LINEAR FIT ===================================

x = linspace(-10, 10, 20); % Make 20 samples along the x axis

% Linear relation, with noise

slope = 1.5;

intercept = -1;

noiseAmplitude = 15;

y = slope .* x + intercept + noiseAmplitude * rand(1, length(x));

% Plot the training set of data.

subplot(2, 1, 1);

plot(x, y, 'ro', 'MarkerSize', 8, 'LineWidth', 2);

grid on;

xlabel('X', 'FontSize', fontSize);

ylabel('Y', 'FontSize', fontSize);

title('Linear Fit', 'FontSize', fontSize);

% Enlarge figure to full screen.

set(gcf, 'Units', 'Normalized', 'OuterPosition', [0 0 1 1]);

% Give a name to the title bar.

set(gcf, 'Name', 'Demo by ImageAnalyst', 'NumberTitle', 'Off')

% Do the regression with polyfit

linearCoefficients = polyfit(x, y, 1)

% The x coefficient, slope, is coefficients(1).

% The constant, the intercept, is coefficients(2).

% Make fit. It does NOT need to have the same

% number of elements as your training set,

% or the same range, though it could if you want.

% Make 300 fitted samples going from -15 to +20.

xFit = linspace(-15, 20, 500);

% Get the estimated values with polyval()

yFit = polyval(linearCoefficients, xFit);

% Plot the fit

hold on;

plot(xFit, yFit, 'b', 'LineWidth', 2);

legend('Training Set', 'Fit', 'Location', 'Northwest');

%============= CUBIC FIT ===================================

x = linspace(-10, 10, 20); % Make 20 samples along the x axis

% Cubic relation, with noise

c1 = 1;

c2 = 2;

c3 = -10;

c4 = 4;

noiseAmplitude = 500;

y = c1 .* x .^3 + c2 .* x .^2 + c3 .* x + c4 + noiseAmplitude * rand(1, length(x));

% Plot the training set of data.

subplot(2, 1, 2);

plot(x, y, 'ro', 'MarkerSize', 8, 'LineWidth', 2);

grid on;

xlabel('X', 'FontSize', fontSize);

ylabel('Y', 'FontSize', fontSize);

title('Cubic Fit', 'FontSize', fontSize);

% Do the regression with polyfit

cubicCefficients = polyfit(x, y, 3)

% The x coefficient, slope, is coefficients(1).

% The constant, the intercept, is coefficients(2).

% Make fit. It does NOT need to have the same

% number of elements as your training set,

% or the same range, though it could if you want.

% Make 300 fitted samples going from -13 to +12.

xFit = linspace(-13, 12, 500);

% Get the estimated values with polyval()

yFit = polyval(cubicCefficients, xFit);

% Plot the fit

hold on;

plot(xFit, yFit, 'b', 'LineWidth', 2);

grid on;

legend('Training Set', 'Fit', 'Location', 'Northwest');

sixwwwwww
on 8 Dec 2013

If you are given values of x and y and both x and y hase same number of elements in them then you can compute m and c as follow:

fitvars = polyfit(x, y, 1);

m = fitvars(1);

c = fitvars(2);

If you don't have linear line then you can use higher values then 1 in the first line of code. I hope it helps. Good luck!

##### 0 Comments

### See Also

### Community Treasure Hunt

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

Start Hunting!