Pratical solving a least square problem - How to find the best fit?

152 Views Asked by At

I want to curve fit the straight line equation:

$$ y = kx + m$$

Where $k$ is the slope and $x$ is the variable and $m$ is where it cuts onto the y-axis.

So assume that we have a plot who looks like this:

enter image description here'

Code:

R = 1:50;
Y = R.*rand(1, 50);
plot(Y);

And I want to find the best fit by minimizing this cost function:

$$V(k,m) = \sum(Y - \hat{Y})^2 = \sum(Y-(kx + m))^2$$

So how can I find my best $k$ and best $m$ which minimize the cost function $V(k,m)$ ?

I want to solve this with a for-loop.

1

There are 1 best solutions below

1
On BEST ANSWER

Here is the answer. I can't believe that all mathematics about linear regression was so difficult and hard explained, and my answer is the easiest one and does the job!

The estimated line of best fit

enter image description here

The line is about

$$y = 0.25x + 0.4$$

The cost function

enter image description here

The code:

%% Random values
R = 0.5:0.5:50;
Y = R.*rand(1, length(R));
figure(1)
plot(Y);


% Set up vectors
V = zeros(1, length(R)*length(R)); % Cost function vector
x = linspace(0, 100, length(R)); % x-axis vector
N = linspace(0, 0.6, length(R)); % Values for k and m

L = 0; % Initial
Vlast = 1e+6; % Initial
for i = 1:length(R)
  for j = 1:length(R)

    % Compute the estimated Y
    Yh = N(i).*x + N(j); % N(i) = k and N(j) = m

    % Count
    L = L + 1;

    % Compute the cost function
    V(L) = sum(Y - Yh).^2;

    % Check if the last cost value was higher
    if V(L) < Vlast
      Vlast = V(L); % Over write
      k = N(i); % Remeber
      m = N(j); % Remeber
    end

  end
end

% Plot the cost function 
figure(2)
plot(1:length(V), V)

% Plot the estimated line
figure(1)
hold on
plot(x, k.*x + m)