In [1]:
import numpy as np
import scipy as sp
import matplotlib.pyplot as plt
from IPython.display import display,Math
In [2]:
display(Math('ax^2 + bx + c = 0'))
display(Math('x = \\frac{-b \\pm \\sqrt {b^2 - 4ac}}{2a}'))
$\displaystyle ax^2 + bx + c = 0$
$\displaystyle x = \frac{-b \pm \sqrt {b^2 - 4ac}}{2a}$
In [3]:
a = 3
b = 7
c = 5

quadeqP = (-b + np.sqrt(b**2 - 4*a*c)) / (2*a)
quadeqN = (-b - np.sqrt(b**2 - 4*a*c)) / (2*a)

print(quadeqP,quadeqN)
nan nan
/anaconda3/lib/python3.6/site-packages/ipykernel_launcher.py:5: RuntimeWarning: invalid value encountered in sqrt
  """
/anaconda3/lib/python3.6/site-packages/ipykernel_launcher.py:6: RuntimeWarning: invalid value encountered in sqrt
  
In [4]:
quadeqP = (-b + sp.sqrt(b**2 - 4*a*c)) / (2*a)
quadeqN = (-b - sp.sqrt(b**2 - 4*a*c)) / (2*a)

print(quadeqP,quadeqN)
(-1.1666666666666665+0.5527707983925666j) (-1.1666666666666665-0.5527707983925666j)
In [5]:
def quadeq(a,b,c):
    
    out = sp.zeros(2)
    
    out[0] = (-b + sp.sqrt(b**2 - 4*a*c)) / (2*a)
    out[1] = (-b - sp.sqrt(b**2 - 4*a*c)) / (2*a)
    
    return out
    
sol = quadeq(2,7,5)
sol
Out[5]:
array([-1. , -2.5])
In [6]:
a = 1
b = range(-5,6)
c = range(-2,11)

M = np.zeros((len(b),len(c)))

for bi in range(0,len(b)):
    for ci in range(0,len(c)):
        M[bi,ci] = quadeq(a,b[bi],c[ci])[0]
        
plt.imshow(M,extent=[c[0],c[-1],b[0],b[-1]])
plt.xlabel('c')
plt.ylabel('b')
plt.title('a=' + str(a))
plt.colorbar()
plt.show()
/anaconda3/lib/python3.6/site-packages/ipykernel_launcher.py:5: ComplexWarning: Casting complex values to real discards the imaginary part
  """
/anaconda3/lib/python3.6/site-packages/ipykernel_launcher.py:6: ComplexWarning: Casting complex values to real discards the imaginary part