Back to Browse

Root Finding in Python

13.9K views
Jan 12, 2021
17:59

Learn how to numerically find roots of complex equations in python. Script can be found here: https://www.hageslab.com/Resources.html#PythonAnchor Here we are using "Spyder" IDE with the numpy, scipy, and matplotlib libraries Script: import numpy as np import matplotlib.pyplot as plt from scipy.optimize import fsolve # Arbitrary Example def fxn1(x): return x**2+3*x - np.exp(x**(1/2)) xlist = np.linspace(0,1,num=1000) plt.figure(num=1,dpi=120) plt.plot(xlist,fxn1(xlist),label="Fxn1") plt.plot(xlist,xlist*0,"--",label="y=0") plt.legend() root = fsolve(fxn1,0.5) def fxn2(Ts, Tinf, Ti, A, Rtot, h, eps, sigma=5.67e-8): # SI units return (Tinf - Ti)/(A*Rtot) - (h+eps*sigma*(Ts+Tinf)*(Ts**2+Tinf**2))*(Tinf-Ts) Tlist = np.linspace(283.15,308.15,num=1000) plt.figure(num=2,dpi=120) plt.plot(Tlist,fxn2(Tlist,283.15,308.15,1.8,0.25,2,.95),label="Fxn2") # SI units plt.plot(Tlist,Tlist*0,"--",label="y=0") plt.legend() Ts = fsolve(fxn2,290,args=(283.15,308.15,1.8,.25,2,0.95)) # [K] Ts_C = Tsf-273.15 # [C]

Download

1 formats

Video Formats

360pmp428.8 MB

Right-click 'Download' and select 'Save Link As' if the file opens in a new tab.

Root Finding in Python | NatokHD