python实现之多元函数作图
生活随笔
收集整理的這篇文章主要介紹了
python实现之多元函数作图
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
多元函數(shù)的本質(zhì)是一種關(guān)系,是兩個(gè)集合間一種確定的對應(yīng)關(guān)系。多元函數(shù)是后續(xù)人工智能的基礎(chǔ),先可視化呈現(xiàn),后續(xù)再學(xué)習(xí)一下求導(dǎo)。
設(shè)D為一個(gè)非空的n 元有序數(shù)組的集合, f為某一確定的對應(yīng)規(guī)則。若對于每一個(gè)有序數(shù)組 ( x1,x2,…,xn)∈D,通過對應(yīng)規(guī)則f,都有唯一確定的實(shí)數(shù)y與之對應(yīng),則稱對應(yīng)規(guī)則f為定義在D上的n元函數(shù)。
記為y=f(x1,x2,…,xn) 其中 ( x1,x2,…,xn)∈D。變量x1,x2,…,xn稱為自變量,y稱為因變量。
當(dāng)n=1時(shí),為一元函數(shù),記為y=f(x),x∈D,當(dāng)n=2時(shí),為二元函數(shù),記為z=f(x,y),(x,y)∈D。二元及以上的函數(shù)統(tǒng)稱為多元函數(shù)。?
#!/usr/bin/env python # -*- coding: UTF-8 -*- # _ooOoo_ # o8888888o # 88" . "88 # ( | - _ - | ) # O\ = /O # ____/`---'\____ # .' \\| |// `. # / \\|||:|||// \ # / _|||||-:- |||||- \ # | | \\\ - /// | | # | \_| ''\---/'' | _/ | # \ .-\__ `-` ___/-. / # ___`. .' /--.--\ `. . __ # ."" '< `.___\_<|>_/___.' >'"". # | | : `- \`.;`\ _ /`;.`/ - ` : | | # \ \ `-. \_ __\ /__ _/ .-` / / # ==`-.____`-.___\_____/___.-`____.-'== # `=---=' ''' @Project :pythonalgorithms @File :multivariatefunc.py @Author :不勝人生一場醉@Date :2021/8/6 23:59 ''' import numpy as np # mpl_toolkits是matplotlib官方的工具包 mplot3d是用來畫三維圖像的工具包 from mpl_toolkits.mplot3d import Axes3D from matplotlib import pyplot as plt from matplotlib.ticker import LinearLocator, FormatStrFormatter# 繪制z=x^2+y^2的3D圖 # 創(chuàng)建一個(gè)圖像窗口 fig = plt.figure() # 在圖像窗口添加3d坐標(biāo)軸 ax = Axes3D(fig) # 使用np.arange定義 x:范圍(-10,10);間距為0.1 x = np.arange(-10, 10, 0.1) # 使用np.arange定義 y:范圍(-10,10);間距為0.1 y = np.arange(-10, 10, 0.1) # 創(chuàng)建x-y平面網(wǎng)絡(luò) x, y = np.meshgrid(x, y) # 定義函數(shù)z=x^2+y^2 z = x * x + y * y # 將函數(shù)顯示為3d rstride 和 cstride 代表 row(行)和column(列)的跨度 cmap為色圖分類 ax.plot_surface(x, y, z, rstride=1, cstride=1, cmap='rainbow') plt.show()# 繪制z=sin(sqrt(x^2+y^2))的3D圖 fig = plt.figure() ax = Axes3D(fig) # 設(shè)置圖像為三維格式 x = np.arange(-10, 10, 0.1) # x的范圍 y = np.arange(-10, 10, 0.1) # y的范圍 x, y = np.meshgrid(x, y) # 繪制網(wǎng)格 z = np.sin(np.sqrt(x*x+y*y)) ax.plot_surface(x, y, z, rstride=1, cstride=1, cmap='rainbow') plt.show()# 繪制z=sin(x)^2+sin(y)^2的3D圖 fig = plt.figure() ax = Axes3D(fig) # 設(shè)置圖像為三維格式 x = np.arange(-10, 10, 0.1) # x的范圍 y = np.arange(-10, 10, 0.1) # y的范圍 x, y = np.meshgrid(x, y) # 繪制網(wǎng)格 z = np.sin(x) * np.sin(y) ax.plot_surface(x, y, z, rstride=1, cstride=1, cmap='rainbow') plt.show()# 繪制z=x*y的3D圖 fig = plt.figure() ax = Axes3D(fig) # 設(shè)置圖像為三維格式 x = np.arange(-10, 10, 0.1) # x的范圍 y = np.arange(-10, 10, 0.1) # y的范圍 x, y = np.meshgrid(x, y) # 繪制網(wǎng)格 z = x * y ax.plot_surface(x, y, z, rstride=1, cstride=1, cmap='rainbow') plt.show()# 繪制z=sin(x)*sin(y)/x/y的3D圖 fig = plt.figure() ax = Axes3D(fig) # 設(shè)置圖像為三維格式 x = np.arange(-10, 10, 0.1) # x的范圍 y = np.arange(-10, 10, 0.1) # y的范圍 x, y = np.meshgrid(x, y) # 繪制網(wǎng)格 z = np.sin(x) * np.sin(y) / (x * y) ax.plot_surface(x, y, z, rstride=1, cstride=1, cmap='rainbow') plt.show() # 繪制z=sin(x)*sin(y)+cos(x)*cos(y)的3D圖 fig = plt.figure() ax = Axes3D(fig) # 設(shè)置圖像為三維格式 x = np.arange(-10, 10, 0.1) # x的范圍 y = np.arange(-10, 10, 0.1) # y的范圍 x, y = np.meshgrid(x, y) # 繪制網(wǎng)格 z = np.sin(x) * np.sin(y) + np.cos(x) * np.cos(y) ax.plot_surface(x, y, z, rstride=1, cstride=1, cmap='rainbow') plt.show()# 繪制z=-(x*y)/e^(x^2+y^2)的3D圖 fig = plt.figure() ax = Axes3D(fig) x = np.arange(-2, 2, 0.01) y = np.arange(-2, 2, 0.01) x, y = np.meshgrid(x, y) r = - x * y z = r / np.e ** (x ** 2 + y ** 2) surf = ax.plot_surface(x, y, z, rstride=1, cstride=1, cmap='rainbow', linewidth=0, antialiased=False) ax.set_zlim(-1.01, 1.01) ax.zaxis.set_major_locator(LinearLocator(10)) ax.zaxis.set_major_formatter(FormatStrFormatter('%.02f')) fig.colorbar(surf, shrink=0.5, aspect=5) plt.show()# 繪制z=-(x*y)/e^(x^2+y^2)的3D圖 fig = plt.figure() ax = Axes3D(fig) x = np.arange(-2, 2, 0.01) y = np.arange(-2, 2, 0.01) x, y = np.meshgrid(x, y) r = - x * y z = r / np.e ** (x ** 2 + y ** 2) surf = ax.plot_surface(x, y, z, rstride=1, cstride=1, cmap='rainbow', linewidth=0, antialiased=False) ax.set_zlim(-1.01, 1.01) ax.zaxis.set_major_locator(LinearLocator(10)) ax.zaxis.set_major_formatter(FormatStrFormatter('%.02f')) # 在xy 平面添加等高線, contourf會(huì)對區(qū)間進(jìn)行填充 ax.contourf(x, y, z, zdir="z", offset=-2, cmap='rainbow') fig.colorbar(surf, shrink=0.5, aspect=5) plt.show()原創(chuàng)不易,轉(zhuǎn)載請注明!請多多關(guān)注,謝謝!
總結(jié)
以上是生活随笔為你收集整理的python实现之多元函数作图的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 694. Number of Disti
- 下一篇: 考研数据结构之队列(3.3)——练习题之