Python 函數(shù)繪圖及函數(shù)圖像微分與積分
前言
在學(xué)校太閑,就寫了這個(gè)程序,可以繪制函數(shù)圖像,并且可以繪制其導(dǎo)函數(shù)圖像和不定積分的圖像,效果非常不錯(cuò)。
效果圖
說明
1,程序無法繪制復(fù)數(shù)圖像,若函數(shù)返回一個(gè)復(fù)數(shù),將自動(dòng)取模作為函數(shù)值進(jìn)行繪制。
2,函數(shù)的表達(dá)式支持Python所有內(nèi)置函數(shù)和math庫,time庫,random庫的函數(shù)(要求函數(shù)返回的是數(shù)字)。
3,@(“g(x)”)和$(“g(x)”)是g(x)的導(dǎo)函數(shù)和不定積分的固定格式,其中g(shù)(x)是被微函數(shù)的表達(dá)式或者被積函數(shù)的表達(dá)式,”g(x)”或者'g(x)'表示它是個(gè)表達(dá)式而不是數(shù)字,若不加引號將無法正常微積分。
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.")
注意
這個(gè)程序使用Python 3.x,若需要Python 2.x的程序,請自行修改。
以上這篇Python 函數(shù)繪圖及函數(shù)圖像微分與積分就是小編分享給大家的全部內(nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
python目標(biāo)檢測yolo1?yolo2?yolo3和SSD網(wǎng)絡(luò)結(jié)構(gòu)對比
這篇文章主要為大家介紹了python目標(biāo)檢測yolo1?yolo2?yolo3和SSD網(wǎng)絡(luò)結(jié)構(gòu)對比,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-05-05django 實(shí)現(xiàn)電子支付功能的示例代碼
這篇文章主要介紹了django 實(shí)現(xiàn)電子支付功能的示例代碼,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-07-07python爬蟲selenium和phantomJs使用方法解析
這篇文章主要介紹了python爬蟲selenium和phantomJs使用方法解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-08-08Python基礎(chǔ)之面向?qū)ο筮M(jìn)階詳解
這篇文章主要為大家詳細(xì)介紹了Python基礎(chǔ)之面向?qū)ο筮M(jìn)階,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助2022-02-02基于Keras 循環(huán)訓(xùn)練模型跑數(shù)據(jù)時(shí)內(nèi)存泄漏的解決方式
這篇文章主要介紹了基于Keras 循環(huán)訓(xùn)練模型跑數(shù)據(jù)時(shí)內(nèi)存泄漏的解決方式,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨想過來看看吧2020-06-06Python使用matplotlib填充圖形指定區(qū)域代碼示例
這篇文章主要介紹了Python使用matplotlib填充圖形指定區(qū)域代碼示例,具有一定借鑒價(jià)值,需要的朋友可以參考下2018-01-01python使用gdal對shp讀取,新建和更新的實(shí)例
這篇文章主要介紹了python使用gdal對shp讀取,新建和更新的實(shí)例,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-03-03