I have written a code that will calculate the equation of the best fit-line for a given set of data-points.
The procedure is based on least square approximation, which, in simple words,works by finding a line that is at a minimum distance possible from all the points.
Let’s say you have the x-axis points stored in a matrix, ‘x’ & the y-axis points stored in a matrix ‘y’. Then the following code returns the value of the slope, ‘m’ and intercept ‘c’ of the best fit-line, and also the fitted points which are basically the points of the y-axis obtained from the equation of the best fit-line.
//Linear Fit //To fit a given set of data-points to a line. //Written By: Manas Sharma(www.bragitoff.com) funcprot(0); function [f,m,c]=linefit(x,y) n=size(x); if n(2)
Sample Demo:x=[1,2,3,4,5]; y=[2,4,6,8,10]; [yfit,m,c]=linefit(x,y)
In this example the y-axis points were simply twice of the value of x-axis points. Hence the equation of line, we get is y=mx+c=>y=2x.
Now let's try something not so straight forward.
Sample Demo:x=[1,2,3,4,5]; y=[0.5,2.5,3.3,4.1,5.5]; [yfit,m,c]=linefit(x,y)
We can now even plot the observed data-points and the fitted points(yfit) and compare the two.plot2d(x,y,-5) //dotted points for the original/observed data-points plot2d(x,yfit,5) //red line for the fitted points
I have created a module in SCILAB which contains the above macro, and once installed can be used as an in-built function. You can download it from here: https://atoms.scilab.org/toolboxes/curvefit
Leave your questions/suggestion/corrections in the comments section down below and I’ll get back to you soon.
[wpedon id="7041" align="center"]