solve.py

#
# Solving equations with scipy : so easy ! 
# Vincent Legat - 2018
# Ecole Polytechnique de Louvain
#

from numpy import *
from scipy.optimize import fsolve
import matplotlib.pyplot as plt
import matplotlib
matplotlib.rcParams['toolbar'] = 'None'
plt.rcParams['figure.facecolor'] = 'silver'


f = lambda x : exp(x) - 2.0 - x

x1 = fsolve(f,+1.0)
x2 = fsolve(f,-1.0)
plt.figure("Solving equations : so easy :-)")
x = linspace(-2,2,200)
plt.plot(x,f(x),'-b')
plt.gca().axhline(y=0,color='k')
plt.plot([x1,x2],[0,0],'or',markersize=10)

x0 = -1
xsol = fsolve(f,x0)
plt.figure("Solving equations : soluce = %8.6f :-)" % xsol)
x = linspace(-2,2,200)
plt.plot(x,f(x),'-b')
plt.gca().axhline(y=0,color='k')
plt.plot(xsol,f(xsol),'or',markersize=10)
plt.plot(x0,0,'og',markersize=10)
print(xsol)


#ax.yaxis.grid(color='gray',linestyle='dashed')
#ax.xaxis.grid(color='gray',linestyle='dashed')

plt.show()