0

This is my code up until now (using the Anaconda Python 3.4 distribution, Spyder IDE):

import numpy as np
import pylab as pl
import cmath
from fractions import Fraction
import matplotlib.offsetbox as offsetbox 

#Gravity
g = 9.8

#Function to solve quadratic
def solver(k,b,m):
    #Solutions
    alpha = (-b/(2*m))
    sol1 = (alpha + (cmath.sqrt(((b/(2*m))**2)-(k/m))))
    sol2 = (alpha - (cmath.sqrt(((b/(2*m))**2)-(k/m))))
    #Only 2 decimal places
    alpha = "%.2f" % alpha.real
    alpha = float(alpha)
    sol1 = "%.2f" % sol1.real
    sol1 = float(sol1)
    sol2 = "%.2f" % sol2.real
    sol2 = float(sol1)
    #Particular solution
    yp = (g*m)/k
    yp = "%.2f" % yp
    yp = float(yp)
    #Calculating the discriminant
    if ((b/2*m)**2) > (k/m):
        print("y = C1*(e^%s*t) + C2*(e^%s*t) + %s"%(sol1, sol2, yp))
        t = np.linspace(0, 50, 10000)           
        yc = np.exp(sol1*t) + np.exp(sol2*t)
        y = yc + yp
        pl.plot(t,y, '-g', label="y = C1*(e^%s*t) + C2*(e^%s*t) + %s"%(sol1, sol2, yp), linewidth=2)
        a = pl.legend(loc='upper right',prop={'size':14})
        #Extra text
        txt=offsetbox.TextArea("Sobre amortiguado") 
        box = a._legend_box 
        box.get_children().append(txt) 
        box.set_figure(box.figure) 
        #Show plot
        pl.show()

    elif ((b/2*m)**2) == (k/m):
        print("y = C1*(e^%s*t) + C2*t*(e^%s*t) + %s"%(sol1, sol2, yp))
        t = np.linspace(0, 50, 10000)           
        yc = np.exp(sol1*t) + t*np.exp(sol2*t)
        y = yc + yp
        pl.plot(t,y, '-g', label="y = C1*(e^%s*t) + C2*t*(e^%s*t) + %s"%(sol1, sol2, yp), linewidth=2)
        a = pl.legend(loc='upper right',prop={'size':14})
        #Extra text
        txt=offsetbox.TextArea("Criticamente amortiguado") 
        box = a._legend_box 
        box.get_children().append(txt) 
        box.set_figure(box.figure) 
        #Show plot
        pl.show()

    elif ((b/2*m)**2) < (k/m):
        beta = (cmath.sqrt((k/m)-((b/(2*m))**2)))
        beta = "%.2f" % beta.real
        beta = float(beta)
        print("y = e^(%s*t)*[C1*cos(%s*t) + C2*sen(%s*t)] + %s"%(alpha, beta, beta, yp))
        t = np.linspace(0, 50, 10000)         
        yc = (np.exp(alpha*t))*(np.cos(beta*t) + np.sin(beta*t))
        y = yc + yp
        pl.plot(t,y, '-g', label="y = e^(%s*t)*[C1*cos(%s*t) + C2*sen(%s*t)] + %s"%(alpha, beta, beta, yp), linewidth=2)
        a = pl.legend(loc='upper right',prop={'size':14})
        #Extra text
        txt=offsetbox.TextArea("Sub amortiguado") 
        box = a._legend_box 
        box.get_children().append(txt) 
        box.set_figure(box.figure) 
        #Show plot
        pl.show()

#End function definition


#Execution
k = float(Fraction(input("k: ")))
b = float(Fraction(input("b: ")))
m = float(Fraction(input("m: ")))       
solver(k,b,m)

What I am trying to do is first ask the users for inputs, then plot the graph with the solution; but after that I would like the user to be able to change their inputs so the graph updates (without closing the plot window) and see the new solution for their new inputs. I tried putting everything in a while loop, but the pl.show() that brings up the graph, blocks further execution until the window is closed.

I have read something about using pl.draw() and pl.ion() but after trying for many hours couldn't get any of that to work, not sure what I am doing wrong

Thanks in advance for any help!

Edit: Have been working on this all day so I will probably not be checking answers right away, but I will after some sleep; hoping to find some good answers later!

siebz0r
  • 18,867
  • 14
  • 64
  • 107
SilverKnight
  • 41
  • 2
  • 6

1 Answers1

0

I think @Repiklis was right. To update the plot the most efficient way is to use set_ydata method for an axis object as explained in How to update a plot in matplotlib?. You'll also need pl.ion() at the beggining of your program (after the imports).

But the key is that you have to define a loop that allows the user to input more data. Something like:

while True:
    k = float(Fraction(input("k: ")))
    b = float(Fraction(input("b: ")))
    m = float(Fraction(input("m: ")))       
    solver(k,b,m)

A simpler way is to clear and plot the data each time (otherwise you'll have to manually reset the axis ranges).

Community
  • 1
  • 1
Ramon Crehuet
  • 3,679
  • 1
  • 22
  • 37