Python 函数绘图及函数图像微分与积分
作者:烧风 时间:2021-07-13 22:53:14
前言
在学校太闲,就写了这个程序,可以绘制函数图像,并且可以绘制其导函数图像和不定积分的图像,效果非常不错。
效果图
说明
1,程序无法绘制复数图像,若函数返回一个复数,将自动取模作为函数值进行绘制。
2,函数的表达式支持Python所有内置函数和math库,time库,random库的函数(要求函数返回的是数字)。
3,@(“g(x)”)和$(“g(x)”)是g(x)的导函数和不定积分的固定格式,其中g(x)是被微函数的表达式或者被积函数的表达式,”g(x)”或者'g(x)'表示它是个表达式而不是数字,若不加引号将无法正常微积分。
4,具体使用方法请看效果图,并理解代码。
代码
# Python 3.x
import turtle
from random import *
from math import *
from time import *
def draw():
turtle.pencolor(random(), random(), random())
global n, sf
n = 0
sf = 0
while n > width / (-2 * zoom):
f = evale(express, n)
drawf(n, f)
n -= (1 / zoom)
n = 0
sf = 0
while n < width / (2 * zoom):
f = evale(express, n)
drawf(n, f)
n += (1 / zoom)
def drawf(n, f):
if n == 0 or isnan(f.real):
turtle.penup()
else:
turtle.pendown()
if isnan(f.real):
f = 0
elif n.imag != 0:
f = abs(f)
if hight / -zoom < f.real < hight / zoom:
turtle.goto(n * zoom, f.real * zoom)
def drawxy():
turtle.clear()
turtle.pencolor(0, 0, 0)
turtle.penup()
turtle.goto(0, 0)
turtle.pendown()
turtle.goto(width / -2, 0)
turtle.goto(width / 2, 0)
turtle.goto(0, 0)
turtle.goto(0, hight / 2)
turtle.goto(0, hight / -2)
def format(s):
s = s.replace("^","**")
s = s.replace("@","differentiate")
s = s.replace("$","integral")
return s
def evale(s, n):
x = n
try:
return eval(s)
except BaseException:
return nan
def differentiate(s):
dx = 0.1 / zoom
f1 = evale(s, n)
df = evale(s, n + dx) - f1
return df / dx
def integral(s):
global sf
dx = copysign(1 / zoom, n)
f1 = evale(s, n)
df = (f1 + evale(s, n + dx)) / 2
sf = sf + df * dx
return sf
def rezoom(n):
global zoom
zoom = n
drawxy()
draw()
zoom = eval(input("zoom = "))
width = 1280
hight = 720
turtle.setup(width, hight)
turtle.hideturtle()
turtle.delay(0)
turtle.pensize(2)
drawxy()
while True:
express = format(input("f(x) = "))
draw()
while True:
i = input(": ")
if i == "break":
break
else:
try:
print(eval(i))
except BaseException:
print("Input error.")
注意
这个程序使用Python 3.x,若需要Python 2.x的程序,请自行修改。
来源:https://blog.csdn.net/hk_shao/article/details/80142914
标签:Python,函数图像,微分,积分
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
asp如何显示SQL数据库所有表的名称?
2010-06-08 09:30:00
关注oblog 关注xml-rpc 自己开发客户端小记
2009-10-04 20:27:00
python中对数据进行各种排序的方法
2022-05-31 15:26:55
centos下yum搭建安装linux+apache+mysql+php环境教程
2023-11-14 01:04:39
python清理子进程机制剖析
2021-08-10 22:05:19
![](https://img.aspxhome.com/file/2023/8/86618_0s.png)
在Matplotlib图中插入LaTex公式实例
2023-09-05 12:23:38
![](https://img.aspxhome.com/file/2023/4/99244_0s.jpg)
SQL Select语句完整的执行顺序
2008-09-28 21:22:00
Python之列表推导式最全汇总(中篇)
2022-05-25 20:56:59
![](https://img.aspxhome.com/file/2023/2/99272_0s.jpg)
Python实现GUI学生管理系统的示例代码
2022-06-01 01:47:56
FrontPage XP设计教程3——网页的布局
2008-10-11 12:20:00
![](https://img.aspxhome.com/file/UploadPic/200810/17/20081017122538589s.jpg)
JS 添加千分位与去掉千分位的示例
2010-08-20 06:18:20
用Pycharm实现鼠标滚轮控制字体大小的方法
2023-02-08 15:34:32
![](https://img.aspxhome.com/file/2023/2/108152_0s.jpg)
laravel 解决crontab不执行的问题
2023-11-15 01:45:18
![](https://img.aspxhome.com/file/2023/5/75875_0s.jpg)
Python和Bash结合在一起的方法
2023-11-02 20:53:27
Python使用pyfinance包进行证券收益分析
2023-06-02 16:55:13
![](https://img.aspxhome.com/file/2023/6/107186_0s.png)
在Python中调用ggplot的三种方法
2023-08-23 00:40:58
![](https://img.aspxhome.com/file/2023/7/62567_0s.png)
Python可视化工具如何实现动态图表
2023-05-22 19:21:27
![](https://img.aspxhome.com/file/2023/0/81710_0s.gif)
python实现银行账户系统
2023-05-27 17:49:08
![](https://img.aspxhome.com/file/2023/0/103340_0s.jpg)
python设置环境变量的原因和方法
2023-05-12 18:31:35
![](https://img.aspxhome.com/file/2023/1/77651_0s.png)
学会sql数据库关系图(Petshop)
2012-10-07 10:34:49