<p>我试图用python来教自己calc的基础知识</p>
<p>我使用matplotlib pyplot跟踪函数曲线,然后从指定的两个点(p1和p2)绘制一条割线。</p>
<p>我想我的大部分数学工作正常,但如果我不在Y轴上使用限制(曲线的Y.min,Y.max),那么割线有时会变得很长,并“压碎”我的曲线。</p>
<p>我只是不确定什么是限制直线的最佳数学方法,这样它就不会超出曲线的最大或最小y值:</p>
<p>未设置限制:
<a href="https://i.stack.imgur.com/55oZb.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/55oZb.png" alt="No Y Limit"/></a></p>
<p>曲线的限制设置为(Y.min,Y.max):
<a href="https://i.stack.imgur.com/OuIno.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/OuIno.png" alt="With Y Limit Set To Y.min,Y.max of curve"/></a></p>
<p>这里是我的代码:</p>
<pre><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()
</code></pre>