Jump to content

How to calculate best fit line?


Zananok

Recommended Posts

How to calculate best fit line?

For example:

X values: 1, 2, 3, 4, 5

Y values: 3, 1, 3, 1, 3

How would I find the equation for the best fit line through a mathematical view point (calculus) [y = mx + c]?

Edit: Tried google as one could ask me and no good results where found. Best i could find was: http://www.fourmilab.ch/hackdiet/www/subsubsection1_4_1_0_8_4.html; and this did not help me. I used these keywords: calculate best fit line

A reply would be highly appreciated.

Regards,

Zananok

Edited by Zananok
Link to post
Share on other sites

Plot them on your graph, and draw one line that goes through the middle of all those points and that line should represent those points.

If you're doing it on MS Excel, it can do it for you.

If there's no line that shows a positive or negative correlation (weak or strong) then there's really no correlation between those points and therefore there's no best fit line.

Link to post
Share on other sites

No, I am making a graph program for doing physic and chemistry experiments as MS Excel and other graphs are a pain to work with. One that did ->some<- of the job was a trial, so i decided to make a program that does all needed graphical things programatically for physics and chemistry graphs (which is pretty simple to use), but the last thing i need to code is to plot a best fit line programatically without user's input/help; but to do that i have to understand how to make a best fit line.

I thought of using the average Y value and calculate the difference of each point with that in mind, but it wouldnt work if the straight line was suppoused to be increasing as for example:

X: 1,2,3,4,5

Y: 1,2,3,4,5

It would work partially on this:

X values: 1, 2, 3, 4, 5

Y values: 3, 1, 3, 1, 3

And I have not found out so far on how to calculate the best fit line for it.

Edit: Thanks for the reply

Regards,

Zananok

Edited by Zananok
Link to post
Share on other sites

Thank you aboo, i was going to ask this question, and i tried and i was able to do it, for more details, this is the methode:

1- open an MS excell sheet.

2- enter your infomation (i entered the domain in the first raw, and the range in the second raw.

3- higlight them

4-then at the top barclick on the button of" Insert" .

5- click on " XY scatter button".

6-The scatter will be created for you.

7- If you only click on the graph, you will find that the number of icon in the top bar, inceased, choose "Layout".

8-Then choose the "best fit line" button.

Note; if you scattered graph has no best fit line then you will not be able to click on the button. As it in your example:

X values: 1, 2, 3, 4, 5

Y values: 3, 1, 3, 1, 3

In this case it has no best fit line.

Link to post
Share on other sites

Thank you aboo, i was going to ask this question, and i tried and i was able to do it, for more details, this is the methode:

1- open an MS excell sheet.

2- enter your infomation (i entered the domain in the first raw, and the range in the second raw.

3- higlight them

4-then at the top barclick on the button of" Insert" .

5- click on " XY scatter button".

6-The scatter will be created for you.

7- If you only click on the graph, you will find that the number of icon in the top bar, inceased, choose "Layout".

8-Then choose the "best fit line" button.

Note; if you scattered graph has no best fit line then you will not be able to click on the button. As it in your example:

X values: 1, 2, 3, 4, 5

Y values: 3, 1, 3, 1, 3

In this case it has no best fit line.

Thanks for explain; but i did get a best fit line on Excel:

y = 2.1429

Straight line as I would expect from this data:

X values: 1, 2, 3, 4, 5

Y values: 3, 1, 3, 1, 3

Also, i'm looking on how to find it through calculus not through excel or other graphical program as I am coding the program.

Regards,

Zananok

Link to post
Share on other sites

If you're looking to do it through calculus, I'd suggest using the first and the last y and x value, get an equation for the line and plot that. It'll touch your first and last points and hopefully be in the middle of the rest of them.

It's a straight line so you don't really need calculus to differentiate y = mx + b, because if you do, you'd only get m, which is the slope.

Link to post
Share on other sites

If you're looking to do it through calculus, I'd suggest using the first and the last y and x value, get an equation for the line and plot that. It'll touch your first and last points and hopefully be in the middle of the rest of them.

It's a straight line so you don't really need calculus to differentiate y = mx + b, because if you do, you'd only get m, which is the slope.

It would only be a straight line if this was the data:

X values: 1, 2, 3, 4, 5

Y values: 3, 1, 3, 1, 3

Using this data:

X: 1,2,3,4,5

Y: 1,2,3,4,5

The best fit line would be the line itself (y = x)

And if i just use the first and last point, it would not work with this data:

X values: 1, 2, 3, 4, 5

Y values: 3, 1, 3, 1, 3

as it starts with 3 and ends with 3, so it wouldnt be on the middle of the points, but on the top linking the point (1,3) with (5,3) making the line: y = 3; where it should actually be y = 2.1429 as previously stated.

Edit: and even if it ended with 1, the line would be bended when it actually should be straight as in slope = 0.

And again, thanks for the replies.

Regards,

Zananok

Edited by Zananok
Link to post
Share on other sites

Its ok, give me a second while i upload the images.

[Posted the links as i got his warning: You are not allowed to use that image extension on this board.]

http://www.darksunli...?sec=1290881776

http://www.darksunli...?sec=1290881964

(I made the images on a resonable size + ignore the uncertainties, forgot to remove it from the graph. The squares are the points.)

Graph1 Data:

X:1,2,3,4,5,6

Y:3,1,3,1,3,1

Graph2 Data:

X:1,2,3,4,5,6

Y:1,2,3,4,5,6

It resambles a x^3 graph, but the line is just a line, not suppoused to touch all the points, its y=mx + c. [best fit line].

Regards,

Zananok

Link to post
Share on other sites

Hey Zananok, the procedure to calculate a best-fit line is called least squares / least squares fitting. It can approximate data points to any polynomic function you want. I can introduce you to it via MSN / Skype since it's kind of hard to explain through the forum.

If you are interested send me a private message and I'll give you my email.

Regards.

Link to post
Share on other sites

y-y ̅=s_xy/〖s_x〗^2 (x-x ̅)

That looks terrible when copying and pasting from my IA :angryspeech:

Y minue the average of Y equals S sub xy divided by S sub x squared times (X minus the average of X)

s_xy=(∑(xy))/n-x ̅y ̅

S sub xy equals the (sum of X times Y divided by the total # of data points) minus (the average of X times the average of Y)

s_x=√(〖∑x〗^2/n-x ̅^2 )

S sub x equals the square root of the [(sum of x sqaured divided by the total number of data points) minus the average of X squared]

That is incredibly complicated when trying to explain on the forum, send me a PM of your E-mail and I can send out a worked example from my IA that explains it kind of nicely. Or talk with Hedron :joel:

Edited by Drake
Link to post
Share on other sites

@Hedron123: Thanks for the reply and i would be thankful if you couldd walk me through it ;)

@Drake: I never liked statistics, so i don't exactly remember right now if that is standard deviation, but after talking to some of my collegues; they reminded me that the best fit line is asctually the standard deviataion of the graph, however, if your example is not, i would still like to see your explanation in a better way as you suggested :o

Thanks for all the replies.

Regards,

Zananok

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...