The aim of this post is to explain how a linear regression is calculated with a very simple example. Assume we want to approximate a cloud of point with the following line : as illustrated bellow :

We want to minimize the error given by the square of the difference between points and the line :

By expending each term, the equation becomes :

As we want to minimize the error, the value of we are looking for is necessary a zero of the derivative of the error. Let’s calculate the derivative :

The error is minimized when :

It is now easy to calculate :

## Download

**Linear regression example for Matlab 0.40 KB**