打開python運行環(huán)境。
成都創(chuàng)新互聯自2013年起,是專業(yè)互聯網技術服務公司,擁有項目成都網站建設、網站建設網站策劃,項目實施與項目整合能力。我們以讓每一個夢想脫穎而出為使命,1280元吉林做網站,已為上家服務,為吉林各地企業(yè)和個人服務,聯系電話:18982081108
導入微分的模塊包:from sympy import *。
定義符號變量:x = symbols('x')
定義一個函數:f = x**9
diff = diff(f,x)求導
最后輸入diff,即可顯示其變量值了。
眾多python培訓視頻,盡在python學習網,歡迎在線學習!
1、首先打開python的編輯器軟件,編輯器的選擇可以根據自己的喜好,之后準備好一個空白的python文件:
2、接著在空白的python文件上編寫python程序,這里假設當x>1的時候,方程為根號下x加4,當x-1時,方程為5乘以x的平方加3。所以在程序的開始需要引入math庫,方便計算平方和開方,之后在函數體重寫好表達式就可以了,最后調用一下函數,將結果打印出來:
3、最后點擊軟件內的綠色箭頭,運行程序,在下方可以看到最終計算的結果,以上就是python求分段函數的過程:
#!/usr/bin/env?python
#?-*-?coding:?utf-8?-*-
#?File?name:?parabolic
#???Project?name:?parabolic_equation
"""
..?moduleauthor::
..?Module..?name?parabolic?of?procjet?parabolic_equation
"""
from?sympy?import?*
import?matplotlib.pyplot?as?plt
import?numpy?as?np
def?_filterComplex(inputvalue,?description='inputvalue'):
try:
str(inputvalue).index('I')
except?ValueError:
return?False
else:
return?True
def?_checkBool(inputvalue,?description='inputvalue'):
"""
:param?inputvalue:
:param?description:
:return:
"""
if?not?isinstance(inputvalue,?bool):
raise?TypeError(
'The?{0}?must?be?boolean.?Given:?{1!r}'.format(description,?inputvalue))
def?_checkNumerical(inputvalue,?description='inputvalue'):
"""
:param?inputvalue:
:param?description:
:return:
"""
try:
inputvalue?+?1
except?TypeError:
raise?TypeError(
'The?{0}?must?be?numerical.?Given:?{1!r}'.format(description,?inputvalue))
def?_drawTowPara(expr_1,?expr_2,??inputmin,?inputmax?,step=0.1):
"""
:param?expr_1:
:param?expr_2:
:param?inputmin:
:param?inputmax:
:param?step:
:param?expr_1_evalwithY:
:param?expr_2_evalwithY:
:return:
"""
_checkNumerical(inputmin,?'xmin')
_checkNumerical(inputmax,?'xmax')
_checkNumerical(step,?'step')
y1List?=?[]
x1List?=?[]
y2List?=?[]
x2List?=?[]
if?expr_1.vertical?is?True:
x1List?=?np.arange(inputmin,?inputmax,?step)
for?x?in?x1List:
y1List.append(expr_1.evaluates_Y(x))
else:
y1List?=?np.arange(inputmin,?inputmax,?step)
for?y?in?y1List:
x1List.append(expr_1.evaluates_X(y))
if?expr_2.vertical?is?True:
x2List?=?np.arange(inputmin,?inputmax,?step)
for?x?in?x2List:
y2List.append(expr_2.evaluates_Y(x))
else:
y2List?=?np.arange(inputmin,?inputmax,?step)
for?y?in?y2List:
x2List.append(expr_2.evaluates_X(y))
plt.plot(x1List,?y1List,?'+')
plt.plot(x2List,?y2List,?'-')
plt.show()
def?_solveCrossing(expr_1,?expr_2):
"""
:param?expr_1:
:param?expr_2:
:return:
"""
x?=?Symbol('x')
y?=?Symbol('y')
print?"Given?the?first?expression:?{0!r}".format(expr_1.expr)
print?"Given?the?first?expression:?{0!r}".format(expr_2.expr)
ResultList?=?solve([expr_1.expr,?expr_2.expr],?[x,?y])
Complex?=?False
ResultListTrue?=?[]
for?i?in?range(0,?(len(ResultList)),1):?
if?_filterComplex(ResultList[i][0],?'x')?or?_filterComplex(ResultList[i][1],?'y'):
Complex?=?True
else:
ResultListTrue.append(ResultList[i])
if?len(ResultListTrue)?==?0?and?Complex:
print?"Two?hyperbolic?do?not?intersect,?and?there?is?imaginary?value."
elif?len(ResultListTrue)?==?1:
print?"Two?hyperbolic?tangent.:"?
print?ResultListTrue
else:
print?"Two?hyperbolic?intersection,?and?Points?are:"?
for?iterm?in?ResultListTrue:
print?iterm
class?Parabolic():
"""
"""
def?__init__(self,?a,?b,?c,?vertical=True):
"""
:return:
"""
_checkNumerical(a,?'a')
_checkNumerical(b,?'b')
_checkNumerical(c,?'c')
_checkBool(vertical,?'vertical')
self.a?=?a
self.b?=?b
self.c?=?c
self.vertical?=?vertical
self.y?=?Symbol('y')
self.x?=?Symbol('x')
self.xarray?=?[]
self.yarray?=?[]
if?vertical?is?True:
self.expr?=?(self.x**2)*self.a?+?self.x*self.b?+?self.c
else:
self.expr?=?(self.y**2)*self.a?+?self.y*self.b?+?self.c
def?__repr__(self):
"""
:return:
"""
if?self.vertical?is?True:
return?"The?Equation?look?like:?{0!r}".format(self.expr)
else:
return?"The?Equation?look?like:?{0!r}".format(self.expr)
def?evaluates_X(self,?inputvalue):
"""
:param?inputvalue:
:return:
"""
_checkNumerical(inputvalue,?'y')
return?self.expr.subs(self.y,?inputvalue)
def?evaluates_Y(self,?inputvalue):
"""
:param?inputvalue:
:return:
"""
_checkNumerical(inputvalue,?'x')
return?self.expr.subs(self.x,?inputvalue)
def?getArrays(self,?inputmin,?inputmax,?step=1):
"""
:param?inputmin:
:param?inputmax:
:param?step:
:return:
"""
_checkNumerical(inputmin,?'xmin')
_checkNumerical(inputmax,?'xmax')
_checkNumerical(step,?'step')
if?self.vertical?is?True:
for?x?in?range(inputmin,?inputmax,?step):
self.xarray.append(x)
self.yarray.append(self.evaluates_Y(x))
else:
for?y?in?range(inputmin,?inputmax,?step):
self.yarray.append(y)
self.xarray.append(self.evaluates_X(y))
def?drawPara(self,?inputmin,?inputmax,?step=1):
"""
:param?inputmin:
:param?inputmax:
:param?step:
:return:
"""
_checkNumerical(inputmin,?'xmin')
_checkNumerical(inputmax,?'xmax')
_checkNumerical(step,?'step')
yList?=?[]
xList?=?[]
if?self.vertical?is?True:
xList?=?np.arange(inputmin,?inputmax,?step)
for?x?in?xList:
yList.append(self.evaluates_Y(x))
else:
yList?=?np.arange(inputmin,?inputmax,?step)
for?y?in?yList:
xList.append(self.evaluates_X(y))
plt.plot(xList,?yList,?'+')
plt.show()
if?__name__?==?'__main__':
pa1?=?Parabolic(-5,3,6)
pa2?=?Parabolic(-5,2,5,?False)
print?pa1
print?pa2
_solveCrossing(pa1,?pa2)
_drawTowPara(pa1,?pa2,?-10,?10,?0.1)
# 這就是你想要的,代碼解決了你的大部分問題,可以求兩條雙曲線交點,或者直線與雙曲線交#點,或者兩直線交點. 不過定義雙曲線時候使用的是一般式.也也盡可能做了測試,如果有#問題的話,追問吧
f(x)過(x0,y0)的切線
當(x0,y0)在f(x)上時,由切線的斜率是f'(x0),所以方程是(y-y0)/(x-x0)=f'(x0)
當(x0,y0)不在f(x)上時,設切點是(x1,y1),
方程為(y-y0)/(x-x0)=f'(x1)
y1=f(x1)
(y1-y0)/(x1-x0)=f'(x1)由這兩個方程可解出(x1,y1)就可求出方程
用sympy + matplot:
from sympy import Point, Circle, Line, var
import matplotlib.pyplot as plt
var('t')
c1 = Circle(Point(0, 0), 2)
c2 = Circle(Point(4, 4), 3)
l1 = Line(c1.center, c2.center)
p1 = l1.arbitrary_point(t).subs({t: -c1.radius / (c2.radius - c1.radius)})
p2 = l1.arbitrary_point(t).subs({t: c1.radius / (c1.radius + c2.radius)})
t1 = c1.tangent_lines(p1)
t2 = c1.tangent_lines(p2)
ta = t1 + t2
fig = plt.gcf()
ax = fig.gca()
ax.set_xlim((-10, 10))
ax.set_ylim((-10, 10))
ax.set_aspect(1)
cp1 = plt.Circle((c1.center.x, c1.center.y), c1.radius, fill = False)
cp2 = plt.Circle((c2.center.x, c2.center.y), c2.radius, fill = False)
tp = [0 for i in range(4)]
for i in range(4):
start = ta[i].arbitrary_point(t).subs({t:-10})
end = ta[i].arbitrary_point(t).subs({t:10})
tp[i] = plt.Line2D([start.x, end.x], [start.y, end.y], lw = 2)
ax.add_artist(cp1)
ax.add_artist(cp2)
for i in range(4):
ax.add_artist(tp[i])