LFD Book Forum (http://book.caltech.edu/bookforum/index.php)
-   Homework 1 (http://book.caltech.edu/bookforum/forumdisplay.php?f=130)
-   -   Question 7; calculating slope in Python (http://book.caltech.edu/bookforum/showthread.php?t=4663)

 learnmlsam 03-23-2016 03:21 AM

Question 7; calculating slope in Python

Hi I was trying to implement this in Python. To get the target function which is a line of form y = mx+c, I started by calculating the slope (m). If line passes through the points (x1,y1) and (x2,y2) then m = (y2-y1)/(x2-x1). But as you know these points are randomly picked and in some cases I get (x2-x1) = 0; which generates divide by zero exception.
My question is How did you deal with this problem??

 galo 04-13-2016 01:04 PM

Re: Question 7; calculating slope in Python

How are you generating your random points? If you use numpy to generate the coordinates of one point, it shouldn't happen. Try this:
Code:

`x, y = np.random.uniform(-1,1,2)`

 All times are GMT -7. The time now is 04:23 PM.