pythonscipy求解非線性方程的方法(fsolve/root)-創(chuàng)新互聯(lián)

使用scipy.optimize模塊的root和fsolve函數(shù)進(jìn)行數(shù)值求解線性及非線性方程,下面直接貼上代碼,代碼很簡單

創(chuàng)新互聯(lián)建站憑借在網(wǎng)站建設(shè)、網(wǎng)站推廣領(lǐng)域領(lǐng)先的技術(shù)能力和多年的行業(yè)經(jīng)驗(yàn),為客戶提供超值的營銷型網(wǎng)站建設(shè)服務(wù),我們始終認(rèn)為:好的營銷型網(wǎng)站就是好的業(yè)務(wù)員。我們已成功為企業(yè)單位、個人等客戶提供了網(wǎng)站設(shè)計、網(wǎng)站建設(shè)服務(wù),以良好的商業(yè)信譽(yù),完善的服務(wù)及深厚的技術(shù)力量處于同行領(lǐng)先地位。
from scipy.integrate import odeint
import numpy as np
import matplotlib.pyplot as plt
from scipy.optimize import root,fsolve
#plt.rc('text', usetex=True) #使用latex
## 使用scipy.optimize模塊的root和fsolve函數(shù)進(jìn)行數(shù)值求解方程

## 1、求解f(x)=2*sin(x)-x+1
rangex1 = np.linspace(-2,8)
rangey1_1,rangey1_2 = 2*np.sin(rangex1),rangex1-1
plt.figure(1)
plt.plot(rangex1,rangey1_1,'r',rangex1,rangey1_2,'b--')
plt.title('$2sin(x)$ and $x-1$')

def f1(x):
 return np.sin(x)*2-x+1

sol1_root = root(f1,[2])
sol1_fsolve = fsolve(f1,[2])
plt.scatter(sol1_fsolve,2*np.sin(sol1_fsolve),linewidths=9)
plt.show()

## 2、求解線性方程組{3X1+2X2=3;X1-2X2=5}
def f2(x):
 return np.array([3*x[0]+2*x[1]-3,x[0]-2*x[1]-5])

sol2_root = root(f2,[0,0])
sol2_fsolve = fsolve(f2,[0,0])
print(sol2_fsolve) # [2. -1.5]

a = np.array([[3,2],[1,-2]])
b = np.array([3,5])
x = np.linalg.solve(a,b)
print(x) # [2. -1.5]
## 3、求解非線性方程組
def f3(x):
 return np.array([2*x[0]**2+3*x[1]-3*x[2]**3-7,
     x[0]+4*x[1]**2+8*x[2]-10,
     x[0]-2*x[1]**3-2*x[2]**2+1])

sol3_root = root(f3,[0,0,0])
sol3_fsolve = fsolve(f3,[0,0,0])
print(sol3_fsolve)

## 4、非線性方程
def f4(x):
 return np.array(np.sin(2*x-np.pi)*np.exp(-x/5)-np.sin(x))
init_guess =np.array([[0],[3],[6],[9]])
sol4_root = root(f4,init_guess)
sol4_fsolve = fsolve(f4,init_guess)
print(sol4_fsolve)
t = np.linspace(-2,12,2000)
y1 = np.sin(2*t-np.pi)*np.exp(-t/5)
y2 = np.sin(t)
plt.figure(2)
a , = plt.plot(t,y1,label='$sin(2x-\pi)e^{-x/5}$')
b , = plt.plot(t,y2,label='$sin(x)$')
plt.scatter(sol4_fsolve,np.sin(sol4_fsolve),linewidths=8)
plt.title('$sin(2x-\pi)e^{-x/5}$ and $sin(x)$')
plt.legend()

分享名稱:pythonscipy求解非線性方程的方法(fsolve/root)-創(chuàng)新互聯(lián)
分享鏈接:http://bm7419.com/article14/godde.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站設(shè)計網(wǎng)站收錄、響應(yīng)式網(wǎng)站網(wǎng)站導(dǎo)航、企業(yè)網(wǎng)站制作外貿(mào)網(wǎng)站建設(shè)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

商城網(wǎng)站建設(shè)