Example #1
0
def exo5():
    valeur = np.array([0.5, 1., 1.5])
    F = np.array([])
    G = np.array([])
    n = 400
    t = np.linspace(-2 * pi, 2 * pi, n)

    for a in valeur:
        f = np.array([])
        g = np.array([])
        for i in range(0, n):
            f = np.append(f, t[i] - a * sin(t[i]))
            g = np.append(g, 1 - a * cos(t[i]))
        F = np.append(F, f)
        G = np.append(G, g)

    F = F.reshape((3, n))
    G = G.reshape((3, n))

    # Question a
    Graph2D(t, F, "t", "f", "f(t)", ["0.5", "1.", "1.5"]).show()
    Graph2D(t, G, "t", "g", "g(t)", ["0.5", "1.", "1.5"]).show()
    # Question b
    Graph2D(F, G, "f", "g", "question b", legend=["0.5", "1.", "1.5"]).show()
    # Question c
    for i in range(0, n):
        if (abs(G[2, i]) < 0.05):  # intersection with the x-axis
            print("abscissa :", F[2, i], "i :", i, "t :", t[i])
        if (abs(F[2, i]) < 1e-3):  # double point
            print("i :", i, "t :", t[i], "g :", G[2, i])
Example #2
0
def well():
    # potential well number
    nw = 4
    yd = 1
    yg = 1
    y0 = np.array([yg, yd])
    zd = 0
    zg = 0
    z0 = np.array([zg, zd])
    n = 300
    a = getA(nw, n)
    #testPotential(nw,n)

    global E
    E = -266.7709 / Eh
    E1 = E * Eh
    x, y1, z1 = tirEuler(n, a, y0, z0, yDer, zDer)
    y1 = y1 / np.linalg.norm(y1)

    E = -3.98506 / Eh
    E2 = E * Eh
    x, y2, z2 = tirEuler(n, a, y0, z0, yDer, zDer)
    y2 = y2 / np.linalg.norm(y2)

    legend = ["E = " + str(E1), "E = " + str(E2)]
    graph = Graph2D(x, [y1, y2], "x [a0]", "Psi(x)", "Potential well", legend)
    graph.show()
Example #3
0
def exo2():
    """
        "physicists" Hermite polynomials computation
    """
    # question b :
    print("H5(0.5) :", H(5, 0.5))

    min = -3
    max = 3
    N = 200
    x = np.linspace(min, max, N)

    #tracer
    y = [
        formHn(0, min, max, N),
        formHn(1, min, max, N),
        formHn(2, min, max, N),
        formHn(3, min, max, N),
        formHn(4, min, max, N),
        formHn(5, min, max, N)
    ]
    legend = ['H0', 'H1', 'H2', 'H3', 'H4', 'H5']
    graph = Graph2D(x, y, "x", "Hn(x)", "polynômes d'Hermite", legend)
    graph.ylimit(-50, 50)
    graph.show()
Example #4
0
def testPotential(nw, n):
    a = getA(nw, n)
    x = np.linspace(0, a, n + 1)
    v = [V(x[i], nw) for i in range(0, n + 1)]

    graph = Graph2D(x, [v], "x [a0]", "V(x)", "Kronig Penney potential")
    graph.show()
Example #5
0
def testVoltageStep(nw, n):
    a = getA(nw, n)
    x = np.linspace(0, a, n + 1)
    v = [V(x[i], nw) + V1(x[i], a) for i in range(0, n + 1)]

    graph = Graph2D(x, [v], "x [a0]", "V(x)", "Potential + voltage step")
    graph.show()
Example #6
0
def questionC2():
    T = 5000
    x, y, z = approx(T, I)

    ye = [exact(xi, I) for xi in x]
    legend = ["T = 0 N", "T = 5000 N"]
    graph = Graph2D(x, [ye, y], "x", "y", "Déformation poutre", legend)
    graph.show()
Example #7
0
def questionC1():
    x, y, z = approx(T, I)

    #Exact Solution
    ye = [exact(xi, I) for xi in x]
    legend = ["exacte", "approchée"]
    graph = Graph2D(x, [ye, y], "x", "y", "Déformation poutre", legend)
    graph.show()
Example #8
0
def testImpurity(nw, n):
    a = getA(nw, n)
    x = np.linspace(0, a, n + 1)
    iw = randint(1, nw)
    v = [V(x[i], nw) + impurity(x[i], iw) for i in range(0, n + 1)]
    #v = [impurity(x[i],iw) for i in range(0,n+1)]

    graph = Graph2D(x, [v], "x [a0]", "V(x)", "adding Impurity")
    graph.show()
Example #9
0
def testAmorphous(nw, n):
    a = getA(nw, n)
    x = np.linspace(0, a, n + 1)
    sizes = [1.5 * a0 * random() for i in range(0, nw)]
    #print("sizes : {}".format(sizes))
    v = [V(x[i], nw) + amorphous(x[i], nw, sizes) for i in range(0, n + 1)]

    graph = Graph2D(x, [v], "x [a0]", "V(x)", "amorphous solid")
    graph.show()
Example #10
0
def testTransport():
    x = np.linspace(0,4,81)
    c = 1
    Tfin = 2
    r = 0.9
    opt = "imp"
    U = transport1D(x,c,Tfin,r,opt="imp")
    
    graph = Graph2D(x,[U],"x","u","Transport equation {}".format(opt))
    graph.show()   
Example #11
0
def exo3():
    L = 2
    M = 4
    N = 3
    n = 200
    t = np.linspace(0, 2 * pi, n)
    x = np.array([])
    y = np.array([])

    for i in range(0, n):
        x = np.append(x, sin(L * t[i]) * cos(M * t[i]))
        y = np.append(y, sin(L * t[i]) * sin(N * t[i]))

    yt = Graph2D(t, [x], "t", "x")
    yx = Graph2D(x, [y], "x", "y")
    tx = Graph2D(x, [t], "x", "t")
    yt.show()
    yx.show()
    tx.show()
Example #12
0
def interpolExp():
    x0 = np.array([-1, 0.5, 1.5, 2.])
    f = np.exp(x0)
    x = np.linspace(-2.3, 50)
    poly = pDivise(x, x0, f)
    xExp = np.exp(x)
    graph = Graph2D(x, [poly, xExp], 'x', 'f(x)', 'Exp interpolation',
                    [r'$p_{0123}}$', 'exp'])
    graph.xlimit(-2, 3)
    graph.ylimit(-5, 11)
    graph.show()
Example #13
0
def testHeat():
    x = np.array([
        0, 0.025, 0.05, 0.125, 0.175, 0.2, 0.25, 0.3, 0.4, 0.425, 0.5, 0.575,
        0.6, 0.675, 0.725, 0.75, 0.8, 0.85, 0.95, 0.975, 1
    ])

    r = 0.09
    Tfin = 1
    U = heat1D(x, r, Tfin)
    graph = Graph2D(x, U, "x", "u", "1D Heat equation solution")
    graph.show()
Example #14
0
def exo1():
    a = 0.6
    b = 7.
    min = 0.
    max = 5.
    n = 150
    t = np.linspace(min, max, n)
    x = np.array([])
    y = np.array([])

    for i in range(0, n):
        x = np.append(x, exp(-a * t[i]) * cos(b * t[i]))
        y = np.append(y, exp(-a * t[i]) * sin(b * t[i]))

    yt = Graph2D(t, [x], "t", "x")
    yx = Graph2D(x, [y], "x", "y")
    tx = Graph2D(x, [t], "x", "t")
    yt.show()
    yx.show()
    tx.show()
Example #15
0
def questionE():
    # Effet des forces de compressions
    T = -5000
    b = 1e-1
    a = 5e-2
    I = b * a**3
    x, y, z = approx(T, I)

    ye = [exact(xi, I) for xi in x]
    legend = ["T = 0 N", "T = -5000 N"]
    graph = Graph2D(x, [ye, y], "x", "y", "Déformation poutre", legend)
    graph.show()
Example #16
0
def testPoisson():
    def f(x):
        return x * (1 - x)

    x = np.array([
        0, 0.025, 0.05, 0.125, 0.175, 0.2, 0.25, 0.3, 0.4, 0.425, 0.5, 0.575,
        0.6, 0.675, 0.725, 0.75, 0.8, 0.85, 0.95, 0.975, 1
    ])
    u = poisson1D(x, f)

    graph = Graph2D(x, [u], "x", "u", "1D Poisson equation solution")
    graph.show()
Example #17
0
def test():
    # Plot of a Bezier curve :
    xPoints = np.array([0, 2, 4, 6])
    yPoints = np.array([1, 4, 1, 4])
    u = np.linspace(0, 1, 50)
    x, y = curve(u, xPoints, yPoints)
    print("P0 : ({},{})".format(x[0], y[0]))
    print("P3 : ({},{})".format(x[-1], y[-1]))

    graph = Graph2D([xPoints, x], [yPoints, y], 'x', 'y', 'Bézier curve',
                    ['points', 'curve'])
    graph.show()
Example #18
0
def pinpointK(n,x0,s0,t0,k1,k2):
    global k
    k = k1; k1 = k
    r,s1,t1 = tirRK(n,1,s0,t0,sDer,tDer,x0)
    print("S(1) : {}".format(s1[-1]))
    
    k = k2; k2 = k
    r,s2,t2 = tirRK(n,1,s0,t0,sDer,tDer,x0)
    print("S(1) : {}".format(s2[-1]))
    
    legend = ["k = "+str(k1),"k = "+str(k2)]
    graph = Graph2D(r,[s1,s2],"rho","S(rho)","Méthode du tir",legend)
    graph.show()
Example #19
0
def exo7():
    """
        Plot the approximation versus the actual function
    """
    n = 100
    x = np.linspace(-pi, pi, n)
    approx = np.array([])
    TAN = np.tan(x)

    for i in range(0, n):
        approx = np.append(approx, tan(x[i]))

    Graph2D(x, [TAN, approx], 'x', 'tan(x)', 'exo7',
            ['tan', 'fraction']).show()
Example #20
0
def exo4():
    min = -2 * pi
    max = 2 * pi
    n = 400
    t = np.linspace(min, max, n)
    x = np.array([])
    y = np.array([])

    for i in range(0, n):
        x = np.append(x, cos(t[i]) * cos(t[i] / 2.))
        y = np.append(y, sin(t[i]) * cos(3. * t[i]))

    # period of x : 4*pi, period of y : pi
    a = Graph2D(t, [x, y], "t", "x", "question a", ["x", "y"])
    a.show()

    # plot parametric curve
    b = Graph2D(x, [y], "x", "y", "question b")
    b.show()

    for i in range(0, n):
        # loop on the intervals satisfying
        if (y[i] <= 1e-8 and abs(x[i]) <= 1 and abs(x[i]) >= 0.8):
            print("i :", i, "t :", t[i])
Example #21
0
def exo6():
    min = -pi
    max = 2 * pi
    n = 400
    t = np.linspace(min, max, n)

    # Question a
    terme0 = np.cos(t)  # 2*pi   periodic
    terme1 = -1 / 3. * np.cos(3 * t)  # 2*pi/3 periodic
    terme2 = 1 / 5. * np.cos(5 * t)  # 2*pi/5 periodic
    Graph2D(t, [terme0, terme1, terme2], "t", "f", "question a").show()

    # Question b
    def Sn(n, t):
        terme = np.cos(t)
        somme = terme
        for i in range(1, n + 1):
            terme = -1 / (2 * i + 1) * np.cos((2 * i + 1) * t)
            somme += terme
        return somme

    y = [Sn(3, t), Sn(4, t), Sn(6, t), Sn(10, t)]
    legend = ["s3", "s4", "s6", "s10"]
    Graph2D(t, y, "t", "Sn", "Partial sums", legend).show()
Example #22
0
def exo5():
    n = 200
    min = -10.
    max = 2.
    X = np.linspace(-10., 2., n)
    A = np.array([])
    B = np.array([])
    x = -10.
    pas = (max - min) / n

    for i in range(0, n):
        A = np.append(A, Ai(x))
        B = np.append(B, Bi(x))
        x += pas

    graph = Graph2D(X, [A, B], 'x', 'f(x)', "Airy Functions", ['Ai', 'Bi'])
    graph.ylimit(-.5, 1)
    graph.show()
Example #23
0
def questionD():
    x = np.linspace(0, L, 100)

    # a < b :
    b = 1e-1
    a = 5e-2
    I = b * a**3
    y1 = [exact(xi, I) for xi in x]

    # a > b :
    a = 1e-1
    b = 5e-2
    I = b * a**3
    y2 = [exact(xi, I) for xi in x]

    legend = ["a < b", "a > b"]
    graph = Graph2D(x, [y1, y2], "x", "y", "Déformation poutre", legend)
    graph.show()
Example #24
0
def exo1():
    
    def f(x):
        return x-0.2*sin(x)-0.5
    
    def derf(x):
        return 1-0.2*cos(x)
    
    def g(x):
        return 0.2*sin(x)+0.5
    
    tol = 1e-5
    n = 20
    a = 0
    b = 1
    # with the bissection method
    xa = bissection(a,b,f,tol)
    print("Bissection : x* = {}".format(xa))
    
    # with the regulaFalsi method
    xb = regulaFalsi(a,b,f,tol,n)
    print("Regula Falsi : x* = {}".format(xb))
    
    # with the NewtonRaphson method
    xc = newtonRaphson(b,tol,f,derf)
    print("Newton Raphson : x* = {}".format(xc))
    
    # with the fixed point method
    xd,x,y = iterations(g,0,tol,n)
    xf = np.linspace(a,b,50)
    yf = [g(x) for x in xf]
    print("Fixed Points : x* = {}".format(xd))
    legend = ['x','f','iteration']
    graph = Graph2D([xf,xf,x],[xf,yf,y],'x','f(x)','f(x)=x',legend)
    graph.show()
    
    # with the secant method
    xe = secant(a,b,f,tol,n)
    print("Secant : x* = {}".format(xe))
Example #25
0
def exo3():
    a = 0.5
    b = 1.5
    tol = 1e-1

    def f(x):
        return 1 / tan(x) - x

    def derf(x):
        return -1 / sin(x)**2 - 1

    x = np.linspace(a, b, 50)
    y = np.divide(1, np.tan(x))
    Graph2D(x, [x, y], 'x', 'f(x)', 'Fixed point ?',
            ['y = x', 'cotan(x)']).show()

    # question b
    xb = bissection(a, b, f, tol)
    print("Bissection : x* = {}".format(xb))

    # question c
    xc = newtonRaphson(1, tol, f, derf)
    print("Newton Raphson : x* = {}".format(xc))
Example #26
0
def exo4():
    """
        Plot the Bessel function J0(x)
    """
    # Question b :
    n = 1
    err = 1
    while (err > 1e-6):
        n += 1
        err = err * 10 / (n - 1)

    print("n : ", n)

    test = np.array([-0.1775967713, -0.2459357644, -0.0142244728])
    print("J0(5)-J0(5)test : ", J0(5., n) - test[0])
    print("J0(10)-J0(10)test : ", J0(10., n) - test[1])
    print("J0(15)-J0(15)test : ", J0(15., n) - test[2])

    x = np.linspace(0, 20, 200)
    bessel0 = np.array([])
    for i in range(0, 200):
        bessel0 = np.append(bessel0, J0(x[i], n))

    Graph2D(x, [bessel0], 'x', 'J0(x)', "Bessel function J0").show()
Example #27
0
def drawCircles():
    origin = 0.8 + 0.5j
    circle1 = circle(origin, 0.75)
    circle2 = circle(origin, 1)
    circle3 = circle(origin, 1.25)

    x = np.array([circle1.real, circle2.real, circle3.real])
    y = np.array([circle1.imag, circle2.imag, circle3.imag])
    #Graph2D(x,y,'x','y','Cercles',['r=0.75','r=1','r=1.25']).show()

    w1 = 1 / circle1
    w2 = 1 / circle2
    w3 = 1 / circle3

    X = np.array([w1.real, w2.real, w3.real])
    Y = np.array([w1.imag, w2.imag, w3.imag])
    #Graph2D(X,Y,'x','y','1/z',['r=0.75','r=1','r=1.25']).show()

    b = 1
    circle4 = circle(-0.25, 1.25)
    wJ = circle4 + b * b / circle4
    graph = Graph2D(wJ.real, [wJ.imag], 'x', 'y', 'Joukovsky')
    graph.setEqual()
    graph.show()
Example #28
0
    vx = [j for i,j,k,l,m,n in u]
    y = [k for i,j,k,l,m,n in u]
    vy = [l for i,j,k,l,m,n in u] 
    z = [m for i,j,k,l,m,n in u]
    vz = [n for i,j,k,l,m,n in u]
    
    return t,x,vx,y,vy,z,vz


def particuleEnergy(vx,vy,vz):
    E = []
    E = np.multiply(0.5*m,np.square(vx)+np.square(vy)+np.square(vz))
    return E


if __name__ == "__main__":
    print("T : ",T)
    print("a0 :",a0/1e-10)
    
    u0 = [5,1,0,0.1,0,-0.2]
    dt = 0.01
    n = 1000
    
    t,x,vx,y,vy,z,vz = integrationRK4(u0,dt,n)
    E = particuleEnergy(vx,vy,vz)
    
    Graph2D(t,[E],"timz(s)","Energy(J)","Energy's conservation").show()
    plot3DGraph(x,y,z,"x(Rt)","y(Rt)","z(Rt)","Particle's trajectory")
    
    
    
Example #29
0
def testEnergy(theta,vtheta):
    T = np.multiply(0.5*m*l**2,np.square(vtheta))
    V = m*g*l*(1-np.cos(theta))
    E = T + V
    
    return T,V,E


if __name__ == "__main__":
    # First resolution : question b
    y0 = np.radians([10.,0.])
    dt = 0.005
    n = 500
    #t,theta,vtheta = numpySoluce(y0,dt,n)
    t,theta,vtheta = testRK4(y0,dt,n)
    x = l*np.sin(theta)
    y = -l*np.cos(theta)
    Graph2D(theta,[vtheta],r'$\theta$ (rds)',r'$\dot{\theta}$ (rds/s)',
                  "Phase portrait").show()
    Graph2D(x,[y],"x (m)","y (m)","Trajectory in Oxy").show()
    
    # test of energy's conservation
    T,V,E = testEnergy(theta,vtheta)
    legend = ['V','T','E']
    Graph2D(t,[V,T,E],"t","Energy","Energy's conservation",legend).show()

    
    
    
    
Example #30
0
def exo2():
    t = np.array([0, 1, 2, 3, 4, 5, 10])
    N = np.array([1000, 370, 130, 50, 17, 8, 1])
    decroit = Graph2D(t, [N], "t", "N(t)")
    decroit.show()