python to solve quadratic equation code example
Example 1: quadratic equation solver python
import cmath
a = 1
b = 5
c = 6
d = (b**2) - (4*a*c)
sol1 = (-b-cmath.sqrt(d))/(2*a)
sol2 = (-b+cmath.sqrt(d))/(2*a)
print('The solution are {0} and {1}'.format(sol1,sol2))
Example 2: quadratic equation plot in python
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(0,1,10)
eta1=0.05
eta2=0.1
lmd1= 2.8
y = 0.5*(-(1/3)*eta1*x**2 + np.sqrt( ((1/3)*eta1*x**2)**2-4*(3*x**2*2.8**2-1))
fig = plt.figure()
ax = fig.add_subplot(1, 1, 1)
ax.spines['left'].set_position('center')
ax.spines['bottom'].set_position('zero')
ax.spines['right'].set_color('none')
ax.spines['top'].set_color('none')
ax.xaxis.set_ticks_position('bottom')
ax.yaxis.set_ticks_position('left')
plt.plot(x,y, 'r')
plt.show()