python实现之多元函数作图
生活随笔
收集整理的這篇文章主要介紹了
python实现之多元函数作图
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
多元函數的本質是一種關系,是兩個集合間一種確定的對應關系。多元函數是后續人工智能的基礎,先可視化呈現,后續再學習一下求導。
設D為一個非空的n 元有序數組的集合, f為某一確定的對應規則。若對于每一個有序數組 ( x1,x2,…,xn)∈D,通過對應規則f,都有唯一確定的實數y與之對應,則稱對應規則f為定義在D上的n元函數。
記為y=f(x1,x2,…,xn) 其中 ( x1,x2,…,xn)∈D。變量x1,x2,…,xn稱為自變量,y稱為因變量。
當n=1時,為一元函數,記為y=f(x),x∈D,當n=2時,為二元函數,記為z=f(x,y),(x,y)∈D。二元及以上的函數統稱為多元函數。?
#!/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圖 # 創建一個圖像窗口 fig = plt.figure() # 在圖像窗口添加3d坐標軸 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) # 創建x-y平面網絡 x, y = np.meshgrid(x, y) # 定義函數z=x^2+y^2 z = x * x + y * y # 將函數顯示為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) # 設置圖像為三維格式 x = np.arange(-10, 10, 0.1) # x的范圍 y = np.arange(-10, 10, 0.1) # y的范圍 x, y = np.meshgrid(x, y) # 繪制網格 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) # 設置圖像為三維格式 x = np.arange(-10, 10, 0.1) # x的范圍 y = np.arange(-10, 10, 0.1) # y的范圍 x, y = np.meshgrid(x, y) # 繪制網格 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) # 設置圖像為三維格式 x = np.arange(-10, 10, 0.1) # x的范圍 y = np.arange(-10, 10, 0.1) # y的范圍 x, y = np.meshgrid(x, y) # 繪制網格 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) # 設置圖像為三維格式 x = np.arange(-10, 10, 0.1) # x的范圍 y = np.arange(-10, 10, 0.1) # y的范圍 x, y = np.meshgrid(x, y) # 繪制網格 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) # 設置圖像為三維格式 x = np.arange(-10, 10, 0.1) # x的范圍 y = np.arange(-10, 10, 0.1) # y的范圍 x, y = np.meshgrid(x, y) # 繪制網格 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會對區間進行填充 ax.contourf(x, y, z, zdir="z", offset=-2, cmap='rainbow') fig.colorbar(surf, shrink=0.5, aspect=5) plt.show()原創不易,轉載請注明!請多多關注,謝謝!
總結
以上是生活随笔為你收集整理的python实现之多元函数作图的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 694. Number of Disti
- 下一篇: python 隐函数作图(原创简单方法)