I want to find the two roots of a quadratic equation ax^2 bx c =0 and plot them against coefficient c while keeping a as a changeable parameter. To change a and see what happens to the plots with varying the parameter, I would like to create a Silder for a from Python's Matplotlib module.
I have the following, however, it doesn't seem to be working.
# Solve the quadratic equation ax**2 bx c = 0
from matplotlib.widgets import Slider # import the Slider widget
import numpy as np
import matplotlib.pyplot as plt
import cmath
a_min = -2
a_max = 2
a_init = -2
fig = plt.figure(figsize=(8,3))
# Slider layout
slider_ax = plt.axes([0.1, 0.05, 0.8, 0.05])
b = 10
def sol1(a,b,c):
d = (b**2) - (4*a*c) # Discriminant
return (-b-np.sqrt(d))/(2*a)
def sol2(a,b,c):
d = (b**2) - (4*a*c) # Discriminant
return (-b np.sqrt(d))/(2*a)
for c in np.linspace(-2, 2, 11):
print('c=',c,' sol1=',sol1(a_init,b,c),' sol2=',sol2(a_init,b,c))
# Plot with initial parameter value
#plt.axes(func_ax)
plt.xlabel('$c$')
plt.title('Roots of $ax^2 bx c = 0$')
plot1, = plt.plot(c, sol1(a_init,b,c), 'r')
plot2, = plt.plot(c, sol2(a_init,b,c), 'b')
# Create a slider
a_slider = Slider(slider_ax, # the axes object containing the slider
'$a$', # the name of the slider parameter
a_min, # minimal value of the parameter
a_max, # maximal value of the parameter
valinit=a_init # initial value of the parameter
)
# Update function
def update(a):
plot1.set_ydata(sol1(a,b,c))
plot2.set_ydata(sol2(a,b,c))
fig.canvas.draw_idle() # redraw the plot
# Execute when parameter gets updated
a_slider.on_changed(update)
plt.show()
Any help?
CodePudding user response:
I made a couple of modifications to your code to make the slider work:
- First,
plot1andplot2are defined in a loop in your code. This means that they are deleted and created again at each iteration. Instead, it makes more sense to compute all the variables you want to plot and then plot them outside the loop. This is performed in theupdate_solsfunction. - Second, I changed the
updatefunction of your slider. The variable updated by the slider should be accessed by usinga_slider.val(see an example
