Python for Numerical Methods -

Roots of Equations


Roots for Equations and Polynomials

We will use scipy.optimize.fsolve() to solve for roots of equations. We will use np.roots(coeff) to find the roots of polynomials. Both are in the python library. Suggested reading is Numerical Methods for Engineers by Chapra and Canale.

Roots of Equations Using Fsolve

The first example finds the depth of a channel given an equation using other flow parameters. The program is self explanatory. Import scipy.optimize, define the function, call the function and the initial guess using fsolve, and print the results. The output is shown in the comment. You can copy and paste the Python program into PyCharm Commmunity interface.

#Find root of equation with fsolve for a channel
import scipy.optimize
def fun(H):
   z = 0.471405*(20*H)**(5/3)/(20+2*H)**(2/3) - 5
   return z
#Intitial guess is 1
results = scipy.optimize.fsolve(fun,1)
print(results)

#Output [0.70229282]



The second example is similar to the first except we use a different function, and 0.2 for the initial guess. We then plot the function. Np.arange assigns X a value between -2 and 2, at .1 intervals. Y is the function value given a value of x. We then plot x,y and show the plot.

#Find roots of equations with fsolve
import scipy.optimize
import matplotlib.pyplot as plt
import numpy as np

def fun(x):
   z = x**2
   return z

results = scipy.optimize.fsolve(fun,0.2)
print(results)
x = np.arange(-2,2,.1)
y = fun(x)
plt.plot(x,y)
plt.show()

#Output [1.13621076e-84] which is about zero



Root Plot

The third example is similar to the first two. We are computing molal volume in the gas equation.

#Find root of equation with fsolve for gas eq.
import scipy.optimize
p = 1
T = 300
a = 3.592
b = .04267
R = .0821

def fun(v):
   z = (p + a/v**2)*(v - b) - R*T
   return z
#Initial guess is 10
#fun in the function to be evaluated
results = scipy.optimize.fsolve(fun,10)
print(results)

#Output [24.52647078]



The fourth example of finding roots using fsolve is below. This example involves an equation with a circuit from electrical engineering. We provide a program and plot.

#Find root of equation with fsolve for circuit
import scipy.optimize
import math
import matplotlib.pyplot as plt
import numpy as np

def fun(R):
   z = math.exp((-0.005*R))*math.cos(((2000 - 0.01*R**2)**.5)*0.05)-.01
   return z
# Plot the function
R = np.arange(-400,400,1)
y = np.exp((-0.005*R))*np.cos(((2000 - 0.01*R**2)**.5)*0.05)-.01
plt.plot(R,y)
plt.show()

#Intitial guess is 100
results = scipy.optimize.fsolve(fun,100)
print(results)

#Output [328.15142909]



Root Plot Four



Roots of Polynomials Using np.roots(coeff)

In this section we provide two examples of using the numpy library to solve the roots for polynomials.

The example polynomial is given below. Import numpy, enter the coefficients, and print the output.

#Find the roots of the polynomial x**3 + 3*x**2 + 2*x +1
# import numpy library
import numpy as np

# Enter the coefficients of the poly
# in the array coeff
coeff = [1, 3, 2, 1]
print(np.roots(coeff))

#Output: [-2.32471796+0.j -0.33764102+0.56227951j -0.33764102-0.56227951j]
# The output is one real number and two complex numbers.




The second example consists of a polynomial to the fourth power. First we must expand the polynomial so that all powers of x are present. We then solve as above. The polynomial is plotted from x=0 to s=2.

#Find the roots of the vib. polynomial a**4 -1.9404*a**2 +.75
#In polynomial form: a**4 + 0*a**3 -1.9404*a**2 + 0*a +.75
import matplotlib.pyplot as plt
import numpy as np

# Enter the coefficients of the poly
# in the array coeff
coeff = [1, 0, -1.9404, 0, 0.75]
print(np.roots(coeff))

#Output: [-1.1864084 -0.72995556 1.1864084 0.72995556]
#Two positive roots

a = np.arange(0,2,.1)
y = a**4 -1.9404*a**2 +.75
plt.grid()
plt.plot(a,y)
plt.show()



PolyPlot5

That is it. We have examined equations for roots.





This website consists of example problems from numerical methods for engineers. The first examples apply to roots, plotting roots, maximums, mininums, and optimization problems. You have enough examples so that you become familiar with the syntax used in Python. The examples have been tested and the output of the programs are listed in the comments for each. All programs were run on Python using the PyCharm Community interface. They were run on an older laptop with 8GB of RAM. If you have never used Python, I recommend the manual Introduction to Engineering Python by Steve Larsen. It is availble on Amazon.







Introduction to Engineering Python: For First Year Engineering Students