def plotError(): print('Plotting error..') #func = lambda x: 1.0 / (x + 1.0) func = sinSqr remez = remezFit('Sin', func, func, (epsilon, 1.0), 4) # remez = remezFit('Rcp', func, func, (1.0, 2.0), 3) print('err:', remez.maxError) est = remezToPoly(remez) err = lambda x: (func(x) - est(x)) / func(x) mp.plot([err], [0.0, 1.0])
def main(): parser = argparse.ArgumentParser(description='Design Serial Diode') parser.add_argument('Vs', type=float, help='Voltage supply') parser.add_argument('Id', type=float, help='Desired current over the diode in Amps') parser.add_argument( 'Is', type=float, nargs='?', default=1e-12, help='Diode Saturation current in Amps (default = 1e-12)') parser.add_argument('N', type=float, nargs='?', default=1, help='Emission Coefficient (default = 1)') parser.add_argument('--Vt', type=float, default=0.026, help='Thermal Voltage in Volts (default = 0.026)') parser.add_argument('-g', '--graph', action='store_true', help='Draw a graph') parser.add_argument('-v', '--verbose', action='store_true', help='Print debug') args = parser.parse_args() Vs = args.Vs Id = args.Id Is = args.Is N = args.N Vt = args.Vt nVt = N * Vt if args.verbose: logging.basicConfig(format='%(levelname)s|%(message)s', level=logging.INFO) logging.info(f'Vs: {Vs}, Id: {Id}, Is: {Is}, N: {N}, Vt: {Vt}') Vd = fmul(log(fadd(fdiv(Id, Is), mpf(1))), nVt) VR = fsub(Vs, Vd) IR = Id R = fdiv(VR, IR) print("VR: {}, IR: {}, R: {}".format(VR, IR, R)) print("Vd: {}, Id: {}, Rd: {}".format(Vd, Id, fdiv(Vd, Id))) if args.graph: plot([ lambda x: fsub(Vs, fmul(x, R)), lambda x: fmul(nVt, log(fadd(fdiv(x, Is), 1))) ], [0, fdiv(Vs, R)], [0, Vs])
def main(): parser = argparse.ArgumentParser( description='Analyse charge % vs cycles of tau') parser.add_argument( 'value', type=float, help= 'Value of cycles or Vratio (the value for which we are not solving)') parser.add_argument( '-c', '--cycles', action='store_true', help='Solve for cycles (if the flag is not set, solve for Vratio)') parser.add_argument('-g', '--graph', action='store_true', help='Draw a graph') parser.add_argument('-v', '--verbose', action='store_true', help='Print debug') args = parser.parse_args() value = args.value cycles = args.cycles if args.verbose: logging.basicConfig(format='%(levelname)s|%(message)s', level=logging.INFO) logging.info(f'value: {value}, cycles: {cycles}') if cycles: Vratio = value cycles = fsub(0, log(fsub(1, Vratio))) print("cycles: {}".format(cycles)) else: cycles = value Vratio = fsub(1, exp(fsub(0, cycles))) print("Vratio: {}".format(Vratio)) if args.graph: plot([lambda x: fsub(1, exp(fsub(0, x)))], [0, cycles], [0, 1])
def main(argv): if len(argv) != 4: print "Analyse Serial Diode" print "Usage: asd <Vcc> <R> <Is> <nVt>" else: Vcc = mpf(argv[0]) R = mpf(argv[1]) Is = mpf(argv[2]) nVt = mpf(argv[3]) x = fdiv(fmul(fmul(Is, R), fsub(exp(fdiv(Vcc, nVt)), mpf(1))), nVt) w = lambertw(x) Id = fdiv(fmul(w, nVt), R) Vd = fmul(log(fadd(fdiv(Id, Is), mpf(1))), nVt) VR = fsub(Vcc, Vd) IR = fdiv(VR, R) print "VR: {}, IR: {}".format(VR, IR) print "Vd: {}, Id: {}".format(Vd, Id) plot([ lambda x: fsub(Vcc, fmul(x, R)), lambda x: fmul(nVt, log(fadd(fdiv(x, Is), 1))) ], [0, fdiv(Vcc, R)], [0, Vcc])
def plot_in_terminal(expr, *args, prec=None, logname=None, file=None, **kwargs): """ Run mpmath.plot() but show in terminal if possible """ from mpmath import plot if logname: os.makedirs('plots', exist_ok=True) file = 'plots/%s.png' % logname if prec: mpmath.mp.dps = prec if isinstance(expr, (list, tuple)): f = [lambdify(t, i, 'mpmath') for i in expr] else: f = lambdify(t, expr, 'mpmath') try: if hasattr(sys.stdout, 'isatty') and sys.stdout.isatty(): from iterm2_tools.images import display_image_bytes else: raise ImportError except ImportError: plot(f, *args, file=file, **kwargs) else: # mpmath.plot ignores the axes argument if file is given, so let # file=False, disable this. if 'axes' in kwargs: file=False if file is not False: from io import BytesIO b = BytesIO() else: b = None plot(f, *args, **kwargs, file=b) if file: with open(file, 'wb') as f: f.write(b.getvalue()) if b: print(display_image_bytes(b.getvalue()))
def main(): parser = argparse.ArgumentParser(description='Analyse Parallel Diode') parser.add_argument('Vs', type=float, help='Voltage supply') parser.add_argument('R1', type=float, help='Resistor 1 value in Ohms') parser.add_argument('R2', type=float, help='Resistor 2 value in Ohms') parser.add_argument('Is', type=float, nargs='?', default=1e-12, help='Diode Saturation current in Amps (default = 1e-12)') parser.add_argument('N', type=float, nargs='?', default=1, help='Emission Coefficient (default = 1)') parser.add_argument('--Vt', type=float, default=0.026, help='Thermal Voltage in Volts (default = 0.026)') parser.add_argument('-g', '--graph', action='store_true', help='Draw a graph') parser.add_argument('-v', '--verbose', action='store_true', help='Print debug') args = parser.parse_args() Vs = args.Vs R1 = args.R1 R2 = args.R2 Is = args.Is N = args.N Vt = args.Vt nVt = N*Vt if args.verbose: logging.basicConfig(format='%(levelname)s|%(message)s', level=logging.INFO) logging.info(f'Vs: {Vs}, R1: {R1}, R2: {R2}, Is: {Is}, N: {N}, Vt: {Vt}') x = fdiv(fmul(fmul(fmul(R1,R2),Is),exp(fdiv(fmul(R2,fadd(Vs,fmul(Is,R1))),(fmul(nVt,fadd(R1,R2)))))),fmul(nVt,fadd(R1,R2))) w = lambertw(x) Id = fsub(fdiv(fmul(fmul(nVt,w),fadd(R1,R2)),fmul(R1,R2)),Is) Vd = fmul(log(fadd(fdiv(Id,Is),mpf(1))),nVt) Rd = fdiv(Vd,Id) Rd2 = fdiv(fmul(Rd,R2),fadd(Rd,R2)) VR2 = Vd IR2 = fdiv(VR2,R2) VR1 = fsub(Vs,Vd) IR1 = fdiv(VR1,R1) print("VR1: {}, IR1: {}".format(VR1, IR1)) print("VR2: {}, IR2: {}".format(VR2, IR2)) print("Vd: {}, Id: {}".format(Vd, Id)) if args.graph: plot([lambda x: fsub(Vs,fmul(fadd(x,fdiv(fmul(nVt,log(fadd(fdiv(x,Is),mpf(1)))),R2)),R1)), lambda x: fmul(nVt,log(fadd(fdiv(x,Is),mpf(1))))], [0, fdiv(Vs,fadd(R1,Rd2))], [0, Vs])
def main(argv): if len(argv) != 5: print "Design Parallel Diode" print "Usage: apd <Vcc> <Id> <R2Mul> <Is> <nVt>" else: Vcc = mpf(argv[0]) Id = mpf(argv[1]) R2Mul = mpf(argv[2]) Is = mpf(argv[3]) nVt = mpf(argv[4]) Vd = fmul(log(fadd(fdiv(Id,Is),mpf(1))),nVt) Rd = fdiv(Vd,Id) VR2 = Vd IR2 = fmul(Id,R2Mul) R2 = fdiv(VR2,IR2) Rd2 = fdiv(fmul(Rd,R2),fadd(Rd,R2)) VR1 = fsub(Vcc,Vd) IR1 = fadd(Id,IR2) R1 = fdiv(VR1,IR1) print "VR1: {}, IR1: {}, R1: {}".format(VR1, IR1, R1) print "VR2: {}, IR2: {}, R2: {}".format(VR2, IR2, R2) print "Vd: {}, Id: {}, Rd: {}".format(Vd, Id, Rd) plot([lambda x: fsub(Vcc,fmul(fadd(x,fdiv(fmul(nVt,log(fadd(fdiv(x,Is),mpf(1)))),R2)),R1)), lambda x: fmul(nVt,log(fadd(fdiv(x,Is),mpf(1))))], [0, fdiv(Vcc,fadd(R1,Rd2))], [0, Vcc])
def main(argv): if len(argv) != 5: print "Analyse Parallel Diode" print "Usage: apd <Vcc> <R1> <R2> <Is> <nVt>" else: Vcc = mpf(argv[0]) R1 = mpf(argv[1]) R2 = mpf(argv[2]) Is = mpf(argv[3]) nVt = mpf(argv[4]) x = fdiv( fmul( fmul(fmul(R1, R2), Is), exp( fdiv(fmul(R2, fadd(Vcc, fmul(Is, R1))), (fmul(nVt, fadd(R1, R2)))))), fmul(nVt, fadd(R1, R2))) w = lambertw(x) Id = fsub(fdiv(fmul(fmul(nVt, w), fadd(R1, R2)), fmul(R1, R2)), Is) Vd = fmul(log(fadd(fdiv(Id, Is), mpf(1))), nVt) Rd = fdiv(Vd, Id) Rd2 = fdiv(fmul(Rd, R2), fadd(Rd, R2)) VR2 = Vd IR2 = fdiv(VR2, R2) VR1 = fsub(Vcc, Vd) IR1 = fdiv(VR1, R1) print "VR1: {}, IR1: {}".format(VR1, IR1) print "VR2: {}, IR2: {}".format(VR2, IR2) print "Vd: {}, Id: {}".format(Vd, Id) plot([ lambda x: fsub( Vcc, fmul( fadd(x, fdiv(fmul(nVt, log(fadd(fdiv(x, Is), mpf(1)))), R2) ), R1)), lambda x: fmul(nVt, log(fadd(fdiv(x, Is), mpf(1)))) ], [0, fdiv(Vcc, fadd(R1, Rd2))], [0, Vcc])
#!/usr/bin/python # import mpmath module import mpmath as mp # trick to show & save with the same function: # if file is None, it shows the plot, else it saves to the filename for file in [None, '7900_09_08.png']: # plot a sine between -6 and 6 mp.plot(mp.sin, [-6, 6], file=file) # same trick as above for file in [None, '7900_09_09.png']: # plot square root (to show complex plotting) # and a custom function (made with lambda expression) mp.plot([mp.sqrt, lambda x: -0.1 * x**3 + x - 0.5], [-3, 3], file=file)
import numpy as np import matplotlib.pyplot as plt from mpmath import j from mpmath import plot plot(lambda n: 3.0 / (2 * np.pi) - 1.0 / (2 * np.pi * (j * n)**2), [-10, 10]) plot(lambda n: 3.0 / (2 * np.pi) - 1.0 / (2 * np.pi * (j * 2 * n)**2), [-10, 10]) plot(lambda n: 3.0 / (2 * np.pi) - 1.0 / (2 * np.pi * (j * 4 * n)**2), [-10, 10]) plot(lambda n: 3.0 / (2 * np.pi) - 1.0 / (2 * np.pi * (j * (n / 2.0))**2), [-10, 10]) plot(lambda n: 3.0 / (2 * np.pi) - 1.0 / (2 * np.pi * (j * (n / 4.0))**2), [-10, 10])
# [x0, x1] - function implements on the interval # q - Polinomials maximum degree for choosing Galerkin method basis # 0th degree basis function is fixed to the initial value # ode - ordinary differential equation # x - function argument # u0 - initial value # Assume that the solution can be approximated as a linear combination of the basis functions def galerkin(ode, x, x0, x1, u0, q): basis = [x**k for k in range(q+1)] # Coefficients for the basis monomials xi = [Symbol("xi_%i" % k) for k in range(q+1)] # Solution function ansatz u = u0 + sum(xi[k]*basis[k] for k in range(1,q+1)) # Form system of linear equations equations = [integrate(ode(u)*basis[k], (x, x0, x1)) \ for k in range(1,q+1)] coeffs = solve(equations, xi[1:]) return u.subs(coeffs) if __name__ == "__main__": x = Symbol('x') ode = lambda u: u.diff(x) - u for q in range(1,6): pprint(galerkin(ode, x, 0, 1, 1, q)) u1 = Lambda(x, galerkin(ode, x, 0, 1, 1, 1)) u2 = Lambda(x, galerkin(ode, x, 0, 1, 1, 2)) u3 = Lambda(x, galerkin(ode, x, 0, 1, 1, 3)) plot([exp, u1, u2, u3], [0, 2])
#coding=utf-8 import math import pandas from mpmath import arange, plot print(math.factorial(4)) plot(arange(5))
import numpy as np import matplotlib.pyplot as plt from mpmath import j from mpmath import plot plot(lambda n: 3.0/(2*np.pi) - 1.0/(2*np.pi*(j*n)**2), [-10, 10]) plot(lambda n: 3.0/(2*np.pi) - 1.0/(2*np.pi*(j*2*n)**2), [-10, 10]) plot(lambda n: 3.0/(2*np.pi) - 1.0/(2*np.pi*(j*4*n)**2), [-10, 10]) plot(lambda n: 3.0/(2*np.pi) - 1.0/(2*np.pi*(j*(n/2.0))**2), [-10, 10]) plot(lambda n: 3.0/(2*np.pi) - 1.0/(2*np.pi*(j*(n/4.0))**2), [-10, 10])
# mp.plot([lambda t: S_double_hol_conn(x1, x2, t, s, b),lambda t: S_double_hol_disconn(x1, x2, t, s, b)],[0,maxtime],points=plotpoints) """ plot of SINGLE SPLITTING QUENCH """ # s,b=3.37,50 a = cutoff(s, b) # print(a) # maxtime = 200 # plotpoints = 1000 # mp.plot(lambda x: cutoff(x,50)/50,[0.9,10],points=100) # x1, x2 = 50, 150 # mp.plot(lambda t: S_single_dirac(x1, x2, t, a),[0,maxtime],points=plotpoints) # x1, x2 = -20, 50 # mp.plot(lambda t: S_single_dirac(x1, x2, t, a),[0,maxtime],points=plotpoints) # x1, x2 = 50, 150 # mp.plot([lambda t: S_single_hol_conn(x1, x2, t, a),lambda t: S_single_hol_disconn(x1, x2, t, a)],[0,maxtime],points=plotpoints) # x1, x2 = -20, 50 # mp.plot([lambda t: S_single_hol_conn(x1, x2, t, a),lambda t: S_single_hol_disconn(x1, x2, t, a)],[0,maxtime],points=plotpoints) """ difference """ x1 = 30 x2 = 40 # mp.plot( [lambda t: S_double_dirac(x1, x2, t, s, b)/(S_single_dirac(x1-b, x2-b, t, a)+S_single_dirac(x1+b, x2+b, t, a))], [0,400],points=200 ) mp.plot([ lambda t: S_double_hol(x1, x2, t, s, b) / (S_single_hol( x1 - b, x2 - b, t, a) + S_single_hol(x1 + b, x2 + b, t, a)) ], [0, 100], points=100)
from mpmath import zeta, plot, gamma, sin, pi # real-plot.py zeichnet zwei reelle Graphen der Riemannschen # Zetafunktion. Der Graph wird erst im Bereich mit x zwischen -4 und # 6 betrachtet, dann mit x zwischen -49 und 51. plot(zeta, [-4, 6], ylim=[-20, 20], points=1000, file="real-plot-1.pdf", singularities=[1]) plot(zeta, [-49, 51], ylim=[-20, 20], points=10000, file="real-plot-2.pdf", singularities=[1])
#!/usr/bin/python # import mpmath module import mpmath as mp # trick to show & save with the same function: # if file is None, it shows the plot, else it saves to the filename for file in [None, '7900_09_08.png']: # plot a sine between -6 and 6 mp.plot(mp.sin, [-6, 6], file=file) # same trick as above for file in [None, '7900_09_09.png']: # plot square root (to show complex plotting) # and a custom function (made with lambda expression) mp.plot([mp.sqrt, lambda x: -0.1*x**3 + x-0.5], [-3, 3], file=file)