Root Finding in Python

Published: 12 January 2021
on channel: HagesLab
13,909
168

Learn how to numerically find roots of complex equations in python.

Script can be found here: https://www.hageslab.com/Resources.ht...

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]


On this page of the site you can watch the video online Root Finding in Python with a duration of hours minute second in good quality, which was uploaded by the user HagesLab 12 January 2021, share the link with friends and acquaintances, this video has already been watched 13,909 times on youtube and it was liked by 168 viewers. Enjoy your viewing!