Sunday, July 28, 2013

Python Slope-Intercept

Python  is a wonderful programming language for quickly coding solutions to many problems. You can download python by going to python.org/download and selecting the version that’s best for your operating system.  Python can run from command line or though Idle that comes with most python downloads.   I have been working with python for almost a year now and it has become my “go to “for  
a lot of work in mathematics. It is not necessarily the “best” language for processor intensive mathematical algorithms, but it can offer efficient and quick solutions that can be easily applied to larger projects.  Recently, I have been doing some review of linear algebra and slope-intercept formulas. This is a fairly simple subject in mathematics but I thought it might be interesting to use python to solve some of these problems.   My solution might not be elegant but it is fairly simple.  I designed the program to take in two points (x1, y1), (x2, y2)  and output an equation of the line in slope-intercept form.   For those who may not be familiar slope-intercept form it is  y = mx + b where m is the slope and b is the y- intercept. The slope is calculated from the two given points by the formula (y1-y2)/(x1-x2) and the y -intercept can be found by 
b = y – mx.


Slope-intercept is useful in linear depreciation models.  For example:  if you bought a car at $81,292 and after 10 years it is only worth $33,121 to find out how much it will be worth after 13 years you can use slope-intercept to get a depreciation formula. The two points are at 0 years it is 81,292 (0, 81,292) and at 10 years it is 33,121 (10 , 33,121) substituting these numbers into our formulas you first find the slope which will represent the rate of depreciation.  You use the slope formula (y1-y2)/(x1-x2) 
(33,121 – 81,292) / (10-0) = -48,171 / 10 = -4817.1   and the y intercept is b = y – mx. 81292- 0(4817.1) = b= 81292 and putting it together you get y = (-4817.1) x + 81292 using this formula we can then determine the value of the car at 13 years by substituting 13 in for x
y = (-4817.1)13 + 81292 and we get 18,669.70 so after 13 years our car is worth $18,669.70
The Python code to find our linear depreciation formula is fairly straight forward


def slope():

    result = ''
    x = float(input("enter x one"))
    y = float(input("enter y one"))
    a = float(input("enter x two"))
    b = float(input("enter y two"))
    slope = (y-b)/(x-a)
    yint = y - (slope*x)
    if yint < 0:
        sign='-'
    else:
        sign='+'
    result = str("y="),str(slope),str("x"),sign,str(yint)
    return result
print(slope())


I use the input() method to take in the two points (x,y), (a,b)
then create a variable named “slope” and then immediately assign it to the formula to find the slope given 2 points (y-b)/(x-a). I then create a variable yint and assign it to the formula for the y-intercept. The if-else statement I use to aid with formatting because generally the + (plus sign) isn't displayed when you get a positive. The final line print(slope()) is there only for when you are running the code in console or idle if you were to convert this to a python module it won't be needed.  
I am sure there are more elegant and efficient ways of doing this as well as built-in python methods to make it display much better but that’s the beauty of python it’s easy to modify and play around with to get desired output style. 

1 comment:

  1. I have gone through your blog, it was very much useful for me and because of your blog, and also I gained many unknown information, the way you have clearly explained is really fantastic. Kindly post more like this, Thank You.

    Digital Marketing Training in Chennai

    Digital Marketing Course in Chennai

    ReplyDelete