Python Forum
Same line length in slope fields - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Homework (https://python-forum.io/forum-9.html)
+--- Thread: Same line length in slope fields (/thread-17785.html)



Same line length in slope fields - schniefen - Apr-24-2019

How would one get the same line length of all the lines in the slope field while keeping the correct slope of each line? Since the change in y is equal to the slope times the change in x, I suspect one would need to define a function in the second for-loop that takes the change in y and x as inputs, yet I have a hard time seeing how one would formulate such a function.

[attachment=621]

def f(x,y):
    return -y/x
for m in [i for i in range(-4,5) if i!=0]:
    for n in [i for i in range(-4,5) if i!=0]:
        slope=f(m/2,n/2)
        plt.plot([m/2,m/2+1/8],[n/2,slope*1/8+n/2],color='k')
        plt.ylim(-2,2,1/2)
        plt.xlim(-2,2,1/2)
        plt.grid(True)



RE: Same line length in slope fields - scidam - Apr-24-2019

You need to define line length first.


from pylab import plt, np
def f(x,y):
    return -y/x

line_len = 0.2

for m in [i for i in range(-4,5) if i!=0]:
    for n in [i for i in range(-4,5) if i!=0]:
        slope=f(m/2,n/2)
        slope = np.arctan(slope)   # we need to clarify what slope means, is it an angle? or tan(angle)? what do we want?
        plt.plot([m/2,m/2+line_len * np.cos(slope)],[n/2,line_len * np.sin(slope)+n/2],color='k')
        plt.ylim(-2,2,1/2)
        plt.xlim(-2,2,1/2)
        plt.grid(True)