secant line too long, trying not to use y limits
Question: I'm currently trying to use python to teach myself the basics of calc, so
please bare with me as I'm pretty beginner.

I am using matplotlib pyplot to trace a function curve and then draw a secant
line from two points I specify (p1 & p2).

I think I have most of the math working ok but if I dont use limits
(Y.min,Y.max of curve) on my Y axis then the secant line can sometimes get
really long and "crush" my curve.

I'm just not sure what would be the best mathematical approach to limiting the
line so it doesn't overshoot the curve's max or min y values:

No Limits Set: [![No Y
Limit](http://i.stack.imgur.com/55oZb.png)](http://i.stack.imgur.com/55oZb.png)

Limits Set to (Y.min,Y.max) of curve: [![With Y Limit Set To Y.min,Y.max of
curve](http://i.stack.imgur.com/OuIno.png)](http://i.stack.imgur.com/OuIno.png)

Heres my code:

    
    
    import matplotlib.pyplot as plt
    import numpy as np
    
    ls = np.linspace(-2,2,100)
    p1 = 2  #starts from zero
    p2 = 50 #starts from zero
    
    def draw_func(ls):
        x = ls
        y = x**2
        #y = (x**4-2*x**2+x)
        plt.plot(x,y)
        return y
    
    x = ls
    y = draw_func(ls)
    
    x1 = x[p1]
    x2 = x[p2]
    y1 = y[p1]
    y2 = y[p2]
    
    m = (y2-y1) / (x2-x1)
    
    delta_x = x2 - x1
    delta_x = x1 + (delta_x/2)
    
    b = y2 - y1
    b = y1 + (b/2)
    
    def draw_line(ls, m, b):
        x = ls
        y = (m * (x-delta_x)) + b
        plt.plot(x,y)
    
    draw_line(ls, m, b)
    
    plt.ylim(y.min(),y.max()) #This Is What I Was Hoping To Avoid
    
    plt.show()
    

Thanks!!


Answer: You can compute the coordinates of the curve's apex. Draw the line from the
start to that x value, instead of continuing through the entire range.

