十年网站开发经验 + 多家企业客户 + 靠谱的建站团队
量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决
打开python运行环境。
成都创新互联自2013年起,是专业互联网技术服务公司,拥有项目成都网站建设、网站建设网站策划,项目实施与项目整合能力。我们以让每一个梦想脱颖而出为使命,1280元吉林做网站,已为上家服务,为吉林各地企业和个人服务,联系电话: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])