欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Python數(shù)值方法及數(shù)據(jù)可視化

 更新時間:2022年09月01日 10:24:37   作者:Chandler_river  
這篇文章主要介紹了Python數(shù)值方法及數(shù)據(jù)可視化,文章圍繞主題展開詳細的內(nèi)容介紹,具有一定的參考價值,需要的小伙伴可以參考一下
  • 隨機數(shù)和蒙特卡洛模擬
  • 求解單一變量非線性方程
  • 求解線性系統(tǒng)方程
  • 函數(shù)的數(shù)學積分
  • 常微分方程的數(shù)值解

等勢線繪圖和曲線:

等勢線 

import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
 
x_vals = np.linspace(-5,5,20)
y_vals = np.linspace(0,10,20)
 
X,Y = np.meshgrid(x_vals,y_vals)
Z = X**2 * Y**0.5
line_count = 15
 
ax = Axes3D(plt.figure())
ax.plot_surface(X,Y,Z,rstride=1,cstride=1)
plt.show()

非線性方程的數(shù)學解:

  • 一般實函數(shù)  Scipy.optimize
  • fsolve函數(shù)求零點(限定只給實數(shù)解)
import scipy.optimize as so
from scipy.optimize import fsolve
 
f = lambda x:x**2-1
fsolve(f,0.5)
fsolve(f,-0.5)
fsolve(f,[-0.5,0.5])
 
 
>>>fsolve(f,-0.5,full_output=True)
>>>(array([-1.]), {'nfev': 9, 'fjac': array([[-1.]]), 'r': array([1.99999875]), 'qtf': array([3.82396337e-10]), 'fvec': array([4.4408921e-16])}, 1, 'The solution converged.')
 
>>>help(fsolve)
>>>Help on function fsolve in module scipy.optimize._minpack_py:
 
fsolve(func, x0, args=(), fprime=None, full_output=0, col_deriv=0, xtol=1.49012e-08, maxfev=0, band=None, epsfcn=None, factor=100, diag=None)
    Find the roots of a function.

    Return the roots of the (non-linear) equations defined by
    ``func(x) = 0`` given a starting estimate.

    Parameters
    ----------
    func : callable ``f(x, *args)``
        A function that takes at least one (possibly vector) argument,
        and returns a value of the same length.
    x0 : ndarray
        The starting estimate for the roots of ``func(x) = 0``.
    args : tuple, optional
        Any extra arguments to `func`.
    fprime : callable ``f(x, *args)``, optional
        A function to compute the Jacobian of `func` with derivatives
        across the rows. By default, the Jacobian will be estimated.
    full_output : bool, optional
        If True, return optional outputs.
    col_deriv : bool, optional
        Specify whether the Jacobian function computes derivatives down
        the columns (faster, because there is no transpose operation).
    xtol : float, optional
        The calculation will terminate if the relative error between two
        consecutive iterates is at most `xtol`.
    maxfev : int, optional
        The maximum number of calls to the function. If zero, then
        ``100*(N+1)`` is the maximum where N is the number of elements
        in `x0`.
    band : tuple, optional
        If set to a two-sequence containing the number of sub- and
        super-diagonals within the band of the Jacobi matrix, the
        Jacobi matrix is considered banded (only for ``fprime=None``).
    epsfcn : float, optional
        A suitable step length for the forward-difference
        approximation of the Jacobian (for ``fprime=None``). If
        `epsfcn` is less than the machine precision, it is assumed
        that the relative errors in the functions are of the order of
        the machine precision.
    factor : float, optional
        A parameter determining the initial step bound
        (``factor * || diag * x||``). Should be in the interval
        ``(0.1, 100)``.
    diag : sequence, optional
        N positive entries that serve as a scale factors for the
        variables.

    Returns
    -------
    x : ndarray
        The solution (or the result of the last iteration for
        an unsuccessful call).
    infodict : dict
        A dictionary of optional outputs with the keys:

        ``nfev``
            number of function calls
        ``njev``
            number of Jacobian calls
        ``fvec``
            function evaluated at the output
        ``fjac``
            the orthogonal matrix, q, produced by the QR
            factorization of the final approximate Jacobian
            matrix, stored column wise
        ``r``
            upper triangular matrix produced by QR factorization
            of the same matrix
        ``qtf``
            the vector ``(transpose(q) * fvec)``

    ier : int
        An integer flag.  Set to 1 if a solution was found, otherwise refer
        to `mesg` for more information.
    mesg : str
        If no solution is found, `mesg` details the cause of failure.

    See Also
    --------
    root : Interface to root finding algorithms for multivariate
           functions. See the ``method=='hybr'`` in particular.

    Notes
    -----
    ``fsolve`` is a wrapper around MINPACK's hybrd and hybrj algorithms.

    Examples
    --------
    Find a solution to the system of equations:
    ``x0*cos(x1) = 4,  x1*x0 - x1 = 5``.

    >>> from scipy.optimize import fsolve
    >>> def func(x):
    ...     return [x[0] * np.cos(x[1]) - 4,
    ...             x[1] * x[0] - x[1] - 5]
    >>> root = fsolve(func, [1, 1])
    >>> root
    array([6.50409711, 0.90841421])
    >>> np.isclose(func(root), [0.0, 0.0])  # func(root) should be almost 0.0.
    array([ True,  True])

關鍵字參數(shù):  full_output=True 

多項式的復數(shù)根 :np.roots([最高位系數(shù),次高位系數(shù),... ... x項系數(shù),常數(shù)項])

>>>f = lambda x:x**4 + x -1
>>>np.roots([1,0,0,1,-1])
>>>array([-1.22074408+0.j        ,  0.24812606+1.03398206j,
        0.24812606-1.03398206j,  0.72449196+0.j        ])
  • 求解線性等式   scipy.linalg
  • 利用dir()獲取常用函數(shù)

import numpy as np
import scipy.linalg as sla
from scipy.linalg import inv
a = np.array([-1,5])
c = np.array([[1,3],[3,4]])
x = np.dot(inv(c),a)
 
>>>x
>>>array([ 3.8, -1.6])

數(shù)值積分  scipy.integrate 

  •  利用dir()獲取你需要的信息
  • 對自定義函數(shù)做積分

import scipy.integrate as si
from scipy.integrate import quad
import numpy as np
import matplotlib.pyplot as plt
f = lambda x:x**1.05*0.001
 
interval = 100
xmax = np.linspace(1,5,interval)
integral,error = np.zeros(xmax.size),np.zeros(xmax.size)
for i in range(interval):
    integral[i],error[i] = quad(f,0,xmax[i])
plt.plot(xmax,integral,label="integral")
plt.plot(xmax,error,label="error")
plt.show()

對震蕩函數(shù)做積分

quad 函數(shù)允許 調(diào)整他使用的網(wǎng)格

>>>(-0.4677718053224297, 2.5318630220102742e-05)
>>>quad(np.cos,-1,1,limit=100)
>>>(1.6829419696157932, 1.8684409237754643e-14)
>>>quad(np.cos,-1,1,limit=1000)
>>>(1.6829419696157932, 1.8684409237754643e-14)
>>>quad(np.cos,-1,1,limit=10)
>>>(1.6829419696157932, 1.8684409237754643e-14)

微分方程的數(shù)值解 參見  Python數(shù)值求解微分方程方法(歐拉法,隱式歐拉)

向量場與流線圖:

vector(x,y) = (y,-x)  

import numpy as np
import matplotlib.pyplot as plt
coords = np.linspace(-1,1,30)
X,Y = np.meshgrid(coords,coords)
Vx,Vy = Y,-X
 
plt.quiver(X,Y,Vx,Vy)
plt.show()
------------------
import numpy as np
import matplotlib.pyplot as plt
 
coords = np.linspace(-2,2,10)
X,Y = np.meshgrid(coords,coords)
Z = np.exp(np.exp(X+Y))
 
ds = 4/6
dX,dY = np.gradient(Z,ds)
plt.contourf(X,Y,Z,25)
plt.quiver(X,Y,dX.transpose(),dY.transpose(),scale=25)
plt.show()

到此這篇關于Python數(shù)值方法及數(shù)據(jù)可視化的文章就介紹到這了,更多相關Python數(shù)值 視化內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

  • pytorch中圖像的數(shù)據(jù)格式實例

    pytorch中圖像的數(shù)據(jù)格式實例

    今天小編就為大家分享一篇pytorch中圖像的數(shù)據(jù)格式實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-02-02
  • Python爬取股票交易數(shù)據(jù)并可視化展示

    Python爬取股票交易數(shù)據(jù)并可視化展示

    拋開炒股技術不說,?那么多股票數(shù)據(jù)是不是非常難找,找到之后是不是看著密密麻麻的數(shù)據(jù)是不是頭都大了?今天帶大家爬取雪球平臺的股票數(shù)據(jù)并將其可視化
    2021-12-12
  • Python extract及contains方法代碼實例

    Python extract及contains方法代碼實例

    這篇文章主要介紹了Python extract及contains方法代碼實例,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2020-09-09
  • python爬蟲之urllib庫常用方法用法總結(jié)大全

    python爬蟲之urllib庫常用方法用法總結(jié)大全

    urllib是python自帶的請求庫,各種功能相比較之下也是比較完備的,下面這篇文章主要給大家介紹了關于python爬蟲之urllib庫常用方法用法的相關資料,文中通過示例代碼介紹的非常詳細,需要的朋友可以參考下
    2018-11-11
  • 教你使用Python獲取QQ音樂某個歌手的歌單

    教你使用Python獲取QQ音樂某個歌手的歌單

    這篇文章主要介紹了Python獲取QQ音樂某個歌手的歌單,從qq音樂中獲取某個你喜歡的歌手的清單,涉及到的庫有requests、json,本文結(jié)合示例代碼給大家介紹的非常詳細,需要的朋友可以參考下
    2022-04-04
  • 利用Python yagmail三行代碼實現(xiàn)發(fā)送郵件

    利用Python yagmail三行代碼實現(xiàn)發(fā)送郵件

    這篇文章主要給大家介紹了關于利用Python yagmail三行代碼實現(xiàn)發(fā)送郵件的相關資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2018-05-05
  • Python3.6簡單的操作Mysql數(shù)據(jù)庫的三個實例

    Python3.6簡單的操作Mysql數(shù)據(jù)庫的三個實例

    今天小編就為大家分享一篇關于Python3.6簡單的操作Mysql數(shù)據(jù)庫的三個實例,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧
    2018-10-10
  • python 爬蟲基本使用——統(tǒng)計杭電oj題目正確率并排序

    python 爬蟲基本使用——統(tǒng)計杭電oj題目正確率并排序

    這篇文章主要介紹了python 爬蟲基本的基本使用,主要利用了Urllib和BeautifulSoup4這兩個庫,配以簡單的實例幫助大家理解,感興趣的朋友可以了解下
    2020-10-10
  • 淺談Python中eval的強大與危害

    淺談Python中eval的強大與危害

    這篇文章主要介紹了Python中eval的強大與危害,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2019-03-03
  • python異常處理之try finally不報錯的原因

    python異常處理之try finally不報錯的原因

    這篇文章主要介紹了python異常處理之try finally不報錯的原因,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2020-05-05

最新評論