import numpy as np
import matplotlib.pyplot as plt
import time 
import math
from numpy import linalg
import scipy as sc
import scipy.sparse as sparse
import scipy.sparse.linalg
plt.style.use('ggplot')

def spectral(N,nplots):
    '''Algorithme de résolution par méthode spectrale de Fourier-Galerkin. N est la taille du vecteur 1D créé représentant la ligne '''
    compt=time.time()
    # GrilleNT
    N=2*int(N/2)
    N2=int(N/2)
    h = 2*math.pi/N; x = [h*i for i in range(1,N+1)]
    
    # Conditions initiales
    v = [math.sin(y) for y in x]
    alpha = 0.5
    dt = .001 #Pas temporel
    
    # (ik)^2 Vecteur
    I = complex(0,1)
    l1 = [I*i for i in range(0,N2)]
    l2 = [I*i for i in range(-N2+1,0)]
    l = l1+[0]+l2
    k = np.array(l)
    k2 = k**2;
    
    # Graphique
    tmax = 10.0; tplot = 0.1
    plotgap = int(round(tplot/dt))
    
    data = np.zeros((nplots+1,N))
    data[0,:] = v
    for i in range(nplots):
        v_hat = np.fft.fft(v) # passage dans l'espace spectral
        for n in range(plotgap):
            v_hat = v_hat / (1-dt*alpha*k2) # backward Euler timestepping
        v = np.fft.ifft(v_hat) # retour dans l'espace temporel
        data[i+1,:] = np.real(v) # enregistrement des données
    return(data)

def implicite(N,rg):
    '''Algorithme de résolution par méthode des éléments finis, en Euler implicite optimisé en matrices creuses'''
    t=time.time()
    h = 2*math.pi/N; x = [h*i for i in range(1,N+1)]
    h = 1/(N+1.0)
    k = h/2
    TFinal = 0.2
    NumOfTimeSteps = rg
    x = np.linspace(0,1,N+2)
    x = x[1:-1]
    
    # IC
    u = np.transpose(np.mat(np.sin(2*np.pi*x)))
    
    # Opérateur Laplacien
    data = np.ones((3, N))
    data[1] = -2*data[1]
    diags = [-1,0,1]
    L = sparse.spdiags(data,diags,N,N)/(h**2)
    
    # Matrice In
    I = sparse.identity(N)
    
    # Data mat des données
    data = []
    
    for i in range(NumOfTimeSteps):
        # Solver: (I - k/2*L) u_new = (I + k/2*L)*u_old
        A = (I -k/2*L)
        b = ( I + k/2*L )*u
        u = np.transpose(np.mat( sparse.linalg.spsolve(A,b)))
        data.append(u)
    return(np.squeeze(np.asarray(data)))

NX=2**9
pi=np.pi
K=1
L=0.8

def sol(x,t):
    return(np.sin(2*x*pi/NX)*np.exp(-K*t*(pi/2*L)**2))
    
def sol_grille(t):
    return np.array([sol(k,t) for k in range(NX)])
    
def sol_tps(t):
    '''NX la dim de la matrice'''
    dt=0.036
    data=np.zeros((t,NX))
    for i in range(t):
        data[i:]=sol_grille(dt*i)
    return data
    
def res(t):
    return linalg.norm(sol_grille(t)-spectral(NX,t))
    
def comp(time):
    err1,err2=[],[]
    for t in range(1,time):
        err1+=[res(t)]
        err2+=[linalg.norm(sol_grille(t)-implicite(NX,t))]
    X=np.arange(time-1)
    plt.clf()
    plt.plot(X,err1)
    plt.plot(X,err2)
    plt.grid()
    plt.show()
    
## Résidu (spectral)
def convergence(taille,n):
    temps=time.time()
    X=np.arange(n)
    Y=[]
    for i in range(n):
        b=max(spectral(2**taille,i)[-1])
        Y+=[b]
    plt.clf()
    plt.plot(X,Y)
    plt.grid()
    plt.show()
    return(X,Y,time.time()-temps)

## Résidu (Euler)
def convergence2(n):
    temps=time.time()
    X=np.arange(n-1)
    Y=[max(implicite(NX,1))]
    for i in range(2,n):    #1 sinon on a la liste vide au début
        b=max(implicite(NX,i)[-1])
        Y+=[b]
    plt.clf()
    plt.plot(X,Y)
    plt.grid()
    plt.title("Evolution de l'écart entre la méthode d'Euler et la solution exacte")
    plt.ylabel("Résidu")
    plt.xlabel("Temps (secondes)")
    plt.show()
    return(X,Y,time.time()-temps)

## Comparaison des figures de diffusion

    
X=sol_tps(100)
Y=spectral(NX,99)
Z=implicite(NX,100)
plt.clf()
plt.suptitle("Comparaison des figures de diffusion",fontsize=16)
plt.subplot(311)
plt.title("Solution exacte",fontsize=12)
plt.imshow(X)
plt.grid()
plt.subplot(312)
plt.title("Méthode spectrale",fontsize=12)
plt.imshow(Y)
plt.grid()
plt.subplot(313)
plt.title("Méthode d'Euler Implicite",fontsize=12)
plt.imshow(Z)
plt.grid()
plt.subplots_adjust(bottom=0.1, right=0.8, top=0.9)
cax = plt.axes([0.82, 0.1, 0.02, 0.8])  #3ème paramètre contrôle la largeur de la colorbar
plt.colorbar(cax=cax)
plt.show()



## Résultats (spectral)
#def exp(X):
#     Z=[]
#     for l in X:
#         Z+=[math.exp(-1/21*l)]
#     return Z

#EXP=exp(X)
plt.clf()
plt.cla()
plt.close()
plt.semilogx(X[:100],Y[:100],'g',label="Spectral")
plt.semilogx(X[:100],WU[:100],'r',label="Euler")
plt.legend()
plt.title("Evolution de l'écart (en norme) entre les méthodes numériques et la solution exacte")
plt.ylabel("Résidu (sans dimension)")
plt.xlabel("Temps (secondes)")
plt.grid(True, which="both")
plt.show()

## Matrices des résultats
X=[  0,   1,   2,   3,   4,   5,   6,   7,   8,   9,  10,  11,  12,
         13,  14,  15,  16,  17,  18,  19,  20,  21,  22,  23,  24,  25,
         26,  27,  28,  29,  30,  31,  32,  33,  34,  35,  36,  37,  38,
         39,  40,  41,  42,  43,  44,  45,  46,  47,  48,  49,  50,  51,
         52,  53,  54,  55,  56,  57,  58,  59,  60,  61,  62,  63,  64,
         65,  66,  67,  68,  69,  70,  71,  72,  73,  74,  75,  76,  77,
         78,  79,  80,  81,  82,  83,  84,  85,  86,  87,  88,  89,  90,
         91,  92,  93,  94,  95,  96,  97,  98,  99, 100, 101, 102, 103,
        104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116,
        117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127, 128, 129,
        130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142,
        143, 144, 145, 146, 147, 148, 149, 150, 151, 152, 153, 154, 155,
        156, 157, 158, 159, 160, 161, 162, 163, 164, 165, 166, 167, 168,
        169, 170, 171, 172, 173, 174, 175, 176, 177, 178, 179, 180, 181,
        182, 183, 184, 185, 186, 187, 188, 189, 190, 191, 192, 193, 194,
        195, 196, 197, 198, 199, 200, 201, 202, 203, 204, 205, 206, 207,
        208, 209, 210, 211, 212, 213, 214, 215, 216, 217, 218, 219, 220,
        221, 222, 223, 224, 225, 226, 227, 228, 229, 230, 231, 232, 233,
        234, 235, 236, 237, 238, 239, 240, 241, 242, 243, 244, 245, 246,
        247, 248, 249, 250, 251, 252, 253, 254, 255, 256, 257, 258, 259,
        260, 261, 262, 263, 264, 265, 266, 267, 268, 269, 270, 271, 272,
        273, 274, 275, 276, 277, 278, 279, 280, 281, 282, 283, 284, 285,
        286, 287, 288, 289, 290, 291, 292, 293, 294, 295, 296, 297, 298,
        299, 300, 301, 302, 303, 304, 305, 306, 307, 308, 309, 310, 311,
        312, 313, 314, 315, 316, 317, 318, 319, 320, 321, 322, 323, 324,
        325, 326, 327, 328, 329, 330, 331, 332, 333, 334, 335, 336, 337,
        338, 339, 340, 341, 342, 343, 344, 345, 346, 347, 348, 349, 350,
        351, 352, 353, 354, 355, 356, 357, 358, 359, 360, 361, 362, 363,
        364, 365, 366, 367, 368, 369, 370, 371, 372, 373, 374, 375, 376,
        377, 378, 379, 380, 381, 382, 383, 384, 385, 386, 387, 388, 389,
        390, 391, 392, 393, 394, 395, 396, 397, 398, 399]
Y=[1.0,
  0.95124131098081666,
  0.90486003171650231,
  0.86074024282414874,
  0.81877167699798925,
  0.77884944342152851,
  0.74087376561697371,
  0.70474973207678349,
  0.67038705905409801,
  0.63769986491919406,
  0.60660645551802328,
  0.57702911999639106,
  0.54889393657947361,
  0.52213058782127941,
  0.49667218486229781,
  0.47245510025611848,
  0.4494188089472031,
  0.42750573700237449,
  0.40666111771795854,
  0.38683285474295492,
  0.36797139187614003,
  0.35002958921169502,
  0.33296260532380939,
  0.31672778519580885,
  0.30128455361371126,
  0.28659431375777689,
  0.27262035073859475,
  0.25932773983663071,
  0.24668325921588857,
  0.23465530689354236,
  0.22321382175801902,
  0.2123302084381363,
  0.20197726583552283,
  0.1921291191417035,
  0.1827611551699436,
  0.17384996084022558,
  0.16537326466361982,
  0.15730988107979904,
  0.14963965750858443,
  0.14234342398318617,
  0.13540294523926413,
  0.1288008751400615,
  0.12252071332370845,
  0.11654676396434921,
  0.11086409654401926,
  0.10545850853723668,
  0.10031648991504261,
  0.095425189379778952,
  0.090772382246213618,
  0.086346439888739884,
  0.082136300678291196,
  0.078131442336332263,
  0.074321855636834772,
  0.070698019390509653,
  0.067250876648775562,
  0.063971812067990477,
  0.060852630377373609,
  0.057885535896803939,
  0.055063113053302845,
  0.052378307847508687,
  0.049824410223820947,
  0.047395037300153403,
  0.045084117415382582,
  0.042885874954621594,
  0.040794815914393587,
  0.038805714171628827,
  0.036913598422167043,
  0.035113739756121573,
  0.033401639839052297,
  0.031773019669409167,
  0.030223808884148055,
  0.028750135585790641,
  0.027348316665503712,
  0.026014848598012265,
  0.024746398685340617,
  0.023539796727497349,
  0.022392027099286518,
  0.021300221213443264,
  0.020261650351257172,
  0.019273718842764798,
  0.018333957579467222,
  0.017440017843359072,
  0.016589665436845705,
  0.015780775098878257,
  0.015011325193350364,
  0.014279392656481956,
  0.013583148190561726,
  0.01292085169203664,
  0.012290847902521631,
  0.011691562271860492,
  0.011121497022898419,
  0.010579227408131137,
  0.010063398148874844,
  0.0095727200480576202,
  0.0091059667681666796,
  0.0086619717662986262,
  0.0082396253786527259,
  0.0078378720471804289,
  0.0074557076814598033,
  0.0070921771492015642,
  0.0067463718891146857,
  0.0064174276401655732,
  0.0061045222815556228,
  0.0058068737780185738,
  0.0055237382253025121,
  0.0052544079909516081,
  0.0049982099457408858,
  0.0047545037813439151,
  0.0045226804100288315,
  0.0043021604423830809,
  0.0040923927392622897,
  0.0038928530343442344,
  0.0037030426238452551,
  0.0035224871201244021,
  0.0033507352660601735,
  0.0031873578072367328,
  0.003031946419120809,
  0.0028841126865480691,
  0.0027434871329683917,
  0.0026097182970238543,
  0.0024824718541515923,
  0.0023614297810161397,
  0.0022462895606829326,
  0.0021367634265465544,
  0.0020325776431240066,
  0.0019334718219155777,
  0.0018391982706234425,
  0.0017495213741014932,
  0.0016642170054892626,
  0.0015830719660581739,
  0.0015058834523701545,
  0.0014324585494169032,
  0.0013626137484730127,
  0.0012961744884579527,
  0.0012329747196606327,
  0.0011728564887361838,
  0.0011156695439377643,
  0.0010612709595967287,
  0.0010095247789126619,
  0.0009603016741604989,
  0.0009134786234655058,
  0.00086893860333827851,
  0.00082657029620134405,
  0.00078626781217636826,
  0.00074793042443666697,
  0.00071146231746357295,
  0.00067677234757749821,
  0.00064377381514518404,
  0.00061238424789382688,
  0.00058252519479052439,
  0.00055412202997189345,
  0.00052710376623381458,
  0.0005014028776151795,
  0.00047695513063221706,
  0.00045369942374161656,
  0.00043157763463121633,
  0.00041053447495659754,
  0.00039051735216053506,
  0.00037147623802994453,
  0.00035336354366182626,
  0.00033613400052570261,
  0.00031974454732529572,
  0.00030415422237668212,
  0.00028932406123394563,
  0.00027521699930647253,
  0.00026179777922449529,
  0.00024903286272137508,
  0.00023689034681238634,
  0.0002253398840605147,
  0.00021435260672998912,
  0.00020390105463799008,
  0.00019395910652421258,
  0.00018450191476675979,
  0.00017550584328120336,
  0.00016694840844760559,
  0.0001588082229178612,
  0.00015106494216291991,
  0.00014369921362629694,
  0.000136692628356791,
  0.00013002767499952742,
  0.00012368769603033792,
  0.00011765684612409551,
  0.0001119200525529527,
  0.00010646297751551253,
  0.00010127198230277731,
  9.6334093211319878e-05,
  9.1636969118484031e-05,
  8.716887063857533e-05,
  8.2918630782955567e-05,
  7.8875627050712928e-05,
  7.5029754880154075e-05,
  7.1371402394767048e-05,
  6.7891426380537544e-05,
  6.4581129434580076e-05,
  6.1432238227971737e-05,
  5.8436882828461682e-05,
  5.5587577031378247e-05,
  5.2877199649575371e-05,
  5.0298976715656445e-05,
  4.7846464551994585e-05,
  4.5513533666236483e-05,
  4.3294353432040317e-05,
  4.1183377516760801e-05,
  3.9175330019661416e-05,
  3.7265192286008869e-05,
  3.5448190364095327e-05,
  3.3719783073839564e-05,
  3.2075650657147878e-05,
  3.0511683981668019e-05,
  2.9023974270954267e-05,
  2.7608803335376018e-05,
  2.6262634279354635e-05,
  2.4982102661703017e-05,
  2.3764008086975728e-05,
  2.2605306206813511e-05,
  2.1503101111292045e-05,
  2.0454638091258506e-05,
  1.9457296753566876e-05,
  1.8508584472005732e-05,
  1.7606130157549906e-05,
  1.6747678332366681e-05,
  1.593108349276547e-05,
  1.5154304747003068e-05,
  1.4415400714541999e-05,
  1.3712524674014723e-05,
  1.3043919947766545e-05,
  1.2407915511442266e-05,
  1.1802921817643546e-05,
  1.1227426823219322e-05,
  1.0679992210260319e-05,
  1.0159249791352928e-05,
  9.6638980901081428e-06,
  9.192699088419467e-06,
  8.7444751323202944e-06,
  8.3181059887074929e-06,
  7.9125260455754856e-06,
  7.5267216487630759e-06,
  7.1597285685570814e-06,
  6.8106295898210419e-06,
  6.4785522196261019e-06,
  6.1626665066548138e-06,
  5.8621829669278922e-06,
  5.576350610669897e-06,
  5.3044550653823064e-06,
  5.0458167904330974e-06,
  4.7997893787005915e-06,
  4.5657579410269435e-06,
  4.3431375694435388e-06,
  4.1313718753275084e-06,
  3.9299315988358164e-06,
  3.7383132861415193e-06,
  3.556038031166261e-06,
  3.3826502786642327e-06,
  3.2177166856661872e-06,
  3.0608250384379512e-06,
  2.9115832222466227e-06,
  2.7696182413596266e-06,
  2.6345752868273137e-06,
  2.5061168497192742e-06,
  2.3839218775980749e-06,
  2.2676849721222425e-06,
  2.1571156257730572e-06,
  2.0519374957975666e-06,
  1.9518877135531706e-06,
  1.8567162275276664e-06,
  1.7661851783927729e-06,
  1.6800683045292277e-06,
  1.5981503765377009e-06,
  1.5202266593222081e-06,
  1.4461024004016446e-06,
  1.3755923431705667e-06,
  1.3085202638927428e-06,
  1.2447185312702957e-06,
  1.184027687487672e-06,
  1.1262960496833579e-06,
  1.0713793308533114e-06,
  1.019140279238653e-06,
  9.694483352963316e-07,
  9.2217930539545208e-07,
  8.7721505142374772e-07,
  8.3444319552843008e-07,
  7.9375683925348578e-07,
  7.5505429637147498e-07,
  7.1823883874209957e-07,
  6.8321845456237323e-07,
  6.4990561840419911e-07,
  6.1821707246460822e-07,
  5.8807361848195618e-07,
  5.5939991979800807e-07,
  5.3212431307122081e-07,
  5.0617862917063521e-07,
  4.8149802280274762e-07,
  4.5802081044555648e-07,
  4.3568831618472728e-07,
  4.144447250665845e-07,
  3.9423694360142171e-07,
  3.7501446706848661e-07,
  3.5672925329099947e-07,
  3.3933560256573802e-07,
  3.2279004344709776e-07,
  3.0705122410017216e-07,
  2.9207980895131186e-07,
  2.778383803778721e-07,
  2.6429134519143368e-07,
  2.5140484568078276e-07,
  2.3914667499231749e-07,
  2.2748619663639528e-07,
  2.163942679184443e-07,
  2.0584316710347489e-07,
  1.9580652413195269e-07,
  1.862592547138755e-07,
  1.7717749763633671e-07,
  1.6853855512788948e-07,
  1.6032083613066612e-07,
  1.5250380233847526e-07,
  1.4506791686601048e-07,
  1.379945954208799e-07,
  1.3126615985642509e-07,
  1.2486579398924302e-07,
  1.18777501570988e-07,
  1.1298606630941256e-07,
  1.0747701383873104e-07,
  1.0223657554425785e-07,
  9.7251654150909102e-08,
  9.2509790989563769e-08,
  8.7999134859473971e-08,
  8.370841240890367e-08,
  7.962689995996828e-08,
  7.5744396707258482e-08,
  7.2051199223263547e-08,
  6.8538077206877133e-08,
  6.519625041437416e-08,
  6.201736671520288e-08,
  5.8993481217747516e-08,
  5.6117036412892302e-08,
  5.33808432857579e-08,
  5.077806334840585e-08,
  4.8302191548604509e-08,
  4.5947040011941014e-08,
  4.3706722576646775e-08,
  4.1575640082484299e-08,
  3.9548466376928928e-08,
  3.7620135003670646e-08,
  3.5785826540166944e-08,
  3.4040956552600502e-08,
  3.238116413813671e-08,
  3.0802301025846158e-08,
  2.9300421209051642e-08,
  2.7871771083188372e-08,
  2.6512780064529332e-08,
  2.5220051666328927e-08,
  2.3990355010082639e-08,
  2.2820616750686212e-08,
  2.1707913395313526e-08,
  2.0649463996816064e-08,
  1.9642623203382479e-08,
  1.8684874647087731e-08,
  1.7773824654807946e-08,
  1.690719626578267e-08,
  1.6082823540873063e-08,
  1.5298646149293213e-08,
  1.4552704219285292e-08,
  1.3843133439868994e-08,
  1.3168160401423365e-08,
  1.2526098163455634e-08,
  1.1915342038479934e-08,
  1.1334365581468482e-08,
  1.0781716774851914e-08,
  1.0256014399533994e-08,
  9.7559445828508489e-09,
  9.2802575148472307e-09,
  8.8277643246628539e-09,
  8.3973341092219733e-09,
  7.9878911068002404e-09,
  7.5984120084046698e-09,
  7.2279234002472458e-09,
  6.8754993309201063e-09,
  6.5402589971921677e-09,
  6.2213645426431527e-09,
  5.9180189636334394e-09,
  5.6294641173760043e-09,
  5.3549788271322111e-09,
  5.0938770797957559e-09,
  4.8455063113600459e-09,
  4.6092457759839507e-09,
  4.3845049945797636e-09,
  4.1707222790459928e-09,
  3.9673633284566079e-09,
  3.7739198936982822e-09,
  3.5899085072181381e-09,
  3.4148692747073633e-09,
  3.2483647257007415e-09,
  3.0899787202194144e-09,
  2.9393154087243379e-09,
  2.7959982427810535e-09,
  2.6596690339631061e-09,
  2.5299870586421458e-09,
  2.4066282064272523e-09,
  2.2892841701252727e-09,
  2.1776616751975942e-09]

Z= [9.6224426675032202,
 9.2591836947011021,
 8.9096381921560948,
 8.5732884596031589,
 8.2496363405950159,
 7.9382024846993415,
 7.6385256375484838,
 7.3501619576909913,
 7.0726843592328503,
 6.8056818792944123,
 6.5487590693472546,
 6.3015354095281175,
 6.0636447450638942,
 5.8347347439715413,
 5.6144663752312649,
 5.402513406658497,
 5.1985619217325043,
 5.0023098546649143,
 4.8134665430201302,
 4.6317522972247085,
 4.4568979863285909,
 4.2886446394039606,
 4.1267430619922267,
 3.9709534670299731,
 3.8210451197087885,
 3.6767959957411587,
 3.5379924525279201,
 3.4044289127390606,
 3.2759075598401948,
 3.1522380451127963,
 3.0332372057352432,
 2.9187287935062365,
 2.8085432138095392,
 2.7025172744330122,
 2.6004939438699886,
 2.5023221187451168,
 2.4078564000201723,
 2.3169568776483582,
 2.229488923358153,
 2.1453229912598646,
 2.0643344259795167,
 1.9864032780357812,
 1.9114141261869704,
 1.8392559064843008,
 1.7698217477788454,
 1.703008813438162,
 1.6387181490384051,
 1.5768545358061741,
 1.5173263495931619,
 1.4600454251746524,
 1.404926925670954,
 1.3518892168982508,
 1.3008537464629679,
 1.2517449274204169,
 1.2044900263254501,
 1.159019055509398,
 1.1152646694236166,
 1.0731620648962445,
 1.0326488851543902,
 0.99366512746954161,
 0.95615305428955888,
 0.92005710772541915,
 0.88532382726637981,
 0.85190177060129157,
 0.81974143742915073,
 0.78879519614580951,
 0.75901721329841065,
 0.73036338570295967,
 0.70279127512457573,
 0.67626004542361118,
 0.65073040207462141,
 0.62616453396850502,
 0.60252605741177112,
 0.57977996223981365,
 0.55789255996455345,
 0.53683143387950316,
 0.51656539104847221,
 0.49706441610674357,
 0.47829962680630778,
 0.46024323123931848,
 0.44286848667642748,
 0.42614965995900567,
 0.4100619893866187,
 0.39458164804328749,
 0.37968570850824357,
 0.36535210889886666,
 0.35155962019558856,
 0.33828781480026093,
 0.32551703628155804,
 0.31322837026244077,
 0.30140361640673913,
 0.29002526146321339,
 0.2790764533272711,
 0.26854097608185995,
 0.25840322598062565,
 0.24864818833771085,
 0.23926141528999778,
 0.23022900439886829,
 0.22153757805974816,
 0.2131742636889962]
 
WU=[1.0,
 0.9259183694701123,
 0.89096381921560974,
 0.85732884596030856,
 0.82496363405949502,
 0.79382024846992805,
 0.76385256375484001,
 0.73501619576910093,
 0.70726843592328004,
 0.68056818792943852,
 0.6548759069347162,
 0.63015354095280807,
 0.60636447450638054,
 0.58347347439715103,
 0.56144663752311696,
 0.54025134066584235,
 0.51985619217324364,
 0.50023098546648748,
 0.4813466543020064,
 0.46317522972246422,
 0.44568979863285435,
 0.42886446394039263,
 0.41267430619921469,
 0.39709534670299118,
 0.38210451197087303,
 0.36767959957411173,
 0.353799245252788,
 0.34044289127390281,
 0.3275907559840151,
 0.31522380451127574,
 0.30332372057352064,
 0.29187287935061867,
 0.28085432138095101,
 0.27025172744329851,
 0.26004939438699703,
 0.25023221187450978,
 0.24078564000201588,
 0.23169568776483415,
 0.22294889233581389,
 0.21453229912598545,
 0.20643344259794824,
 0.19864032780357485,
 0.19114141261869319,
 0.18392559064842706,
 0.17698217477788114,
 0.17030088134381438,
 0.16387181490383909,
 0.15768545358061564,
 0.15173263495931494,
 0.14600454251746431,
 0.14049269256709318,
 0.13518892168982216,
 0.1300853746462943,
 0.12517449274204012,
 0.12044900263254317,
 0.11590190555093764,
 0.11152646694235936,
 0.10731620648962284,
 0.10326488851543758,
 0.099366512746953128,
 0.095615305428953148,
 0.092005710772539981,
 0.088532382726636299,
 0.085190177060127367,
 0.081974143742913314,
 0.078879519614579174,
 0.075901721329839494,
 0.073036338570294401,
 0.070279127512455991,
 0.067626004542360119,
 0.065073040207460589,
 0.062616453396849472,
 0.060252605741175802,
 0.057977996223980227,
 0.055789255996453777,
 0.053683143387948738,
 0.051656539104845606,
 0.049706441610672829,
 0.04782996268062889,
 0.046024323123929935,
 0.044286848667641265,
 0.042614965995898993,
 0.04100619893866015,
 0.039458164804327248,
 0.037968570850822586,
 0.036535210889885095,
 0.035155962019556883,
 0.033828781480024724,
 0.032551703628154322,
 0.031322837026242625,
 0.030140361640672214,
 0.029002526146320082,
 0.027907645332725602,
 0.026854097608184829,
 0.02584032259806154,
 0.024864818833770057,
 0.023926141528998777,
 0.023022900439885773,
 0.022153757805973829,
 0.021317426368898558] 

import numpy as np
import matplotlib.pyplot as plt
import time 
import math
from numpy import linalg
import scipy as sc
import scipy.sparse as sparse
import scipy.sparse.linalg
plt.style.use('ggplot')

def spectral(N,nplots):
    '''Algorithme de résolution par méthode spectrale de Fourier-Galerkin. N est la taille du vecteur 1D créé représentant la ligne '''
    compt=time.time()
    # GrilleNT
    N=2*int(N/2)
    N2=int(N/2)
    h = 2*math.pi/N; x = [h*i for i in range(1,N+1)]
    
    # Conditions initiales
    v = [math.sin(y) for y in x]
    alpha = 0.5
    dt = .001 #Pas temporel
    
    # (ik)^2 Vecteur
    I = complex(0,1)
    l1 = [I*i for i in range(0,N2)]
    l2 = [I*i for i in range(-N2+1,0)]
    l = l1+[0]+l2
    k = np.array(l)
    k2 = k**2;
    
    # Graphique
    tmax = 10.0; tplot = 0.1
    plotgap = int(round(tplot/dt))
    
    data = np.zeros((nplots+1,N))
    data[0,:] = v
    for i in range(nplots):
        v_hat = np.fft.fft(v) # passage dans l'espace spectral
        for n in range(plotgap):
            v_hat = v_hat / (1-dt*alpha*k2) # backward Euler timestepping
        v = np.fft.ifft(v_hat) # retour dans l'espace temporel
        data[i+1,:] = np.real(v) # enregistrement des données
    return(data)

def implicite(N,rg):
    '''Algorithme de résolution par méthode des éléments finis, en Euler implicite optimisé en matrices creuses'''
    t=time.time()
    h = 2*math.pi/N; x = [h*i for i in range(1,N+1)]
    h = 1/(N+1.0)
    k = h/2
    TFinal = 0.2
    NumOfTimeSteps = rg
    x = np.linspace(0,1,N+2)
    x = x[1:-1]
    
    # IC
    u = np.transpose(np.mat(np.sin(2*np.pi*x)))
    
    # Opérateur Laplacien
    data = np.ones((3, N))
    data[1] = -2*data[1]
    diags = [-1,0,1]
    L = sparse.spdiags(data,diags,N,N)/(h**2)
    
    # Matrice In
    I = sparse.identity(N)
    
    # Data mat des données
    data = []
    
    for i in range(NumOfTimeSteps):
        # Solver: (I - k/2*L) u_new = (I + k/2*L)*u_old
        A = (I -k/2*L)
        b = ( I + k/2*L )*u
        u = np.transpose(np.mat( sparse.linalg.spsolve(A,b)))
        data.append(u)
    return(np.squeeze(np.asarray(data)))

NX=2**9
pi=np.pi
K=1
L=0.8

def sol(x,t):
    return(np.sin(2*x*pi/NX)*np.exp(-K*t*(pi/2*L)**2))
    
def sol_grille(t):
    return np.array([sol(k,t) for k in range(NX)])
    
def sol_tps(t):
    '''NX la dim de la matrice'''
    dt=0.036
    data=np.zeros((t,NX))
    for i in range(t):
        data[i:]=sol_grille(dt*i)
    return data
    
def res(t):
    return linalg.norm(sol_grille(t)-spectral(NX,t))
    
def comp(time):
    err1,err2=[],[]
    for t in range(1,time):
        err1+=[res(t)]
        err2+=[linalg.norm(sol_grille(t)-implicite(NX,t))]
    X=np.arange(time-1)
    plt.clf()
    plt.plot(X,err1)
    plt.plot(X,err2)
    plt.grid()
    plt.show()
    
## Résidu (spectral)
def convergence(taille,n):
    temps=time.time()
    X=np.arange(n)
    Y=[]
    for i in range(n):
        b=max(spectral(2**taille,i)[-1])
        Y+=[b]
    plt.clf()
    plt.plot(X,Y)
    plt.grid()
    plt.show()
    return(X,Y,time.time()-temps)

## Résidu (Euler)
def convergence2(n):
    temps=time.time()
    X=np.arange(n-1)
    Y=[max(implicite(NX,1))]
    for i in range(2,n):    #1 sinon on a la liste vide au début
        b=max(implicite(NX,i)[-1])
        Y+=[b]
    plt.clf()
    plt.plot(X,Y)
    plt.grid()
    plt.title("Evolution de l'écart entre la méthode d'Euler et la solution exacte")
    plt.ylabel("Résidu")
    plt.xlabel("Temps (secondes)")
    plt.show()
    return(X,Y,time.time()-temps)

## Comparaison des figures de diffusion

    
X=sol_tps(100)
Y=spectral(NX,99)
Z=implicite(NX,100)
plt.clf()
plt.suptitle("Comparaison des figures de diffusion",fontsize=16)
plt.subplot(311)
plt.title("Solution exacte",fontsize=12)
plt.imshow(X)
plt.grid()
plt.subplot(312)
plt.title("Méthode spectrale",fontsize=12)
plt.imshow(Y)
plt.grid()
plt.subplot(313)
plt.title("Méthode d'Euler Implicite",fontsize=12)
plt.imshow(Z)
plt.grid()
plt.subplots_adjust(bottom=0.1, right=0.8, top=0.9)
cax = plt.axes([0.82, 0.1, 0.02, 0.8])  #3ème paramètre contrôle la largeur de la colorbar
plt.colorbar(cax=cax)
plt.show()



## Résultats (spectral)
#def exp(X):
#     Z=[]
#     for l in X:
#         Z+=[math.exp(-1/21*l)]
#     return Z

#EXP=exp(X)
plt.clf()
plt.cla()
plt.close()
plt.semilogx(X[:100],Y[:100],'g',label="Spectral")
plt.semilogx(X[:100],WU[:100],'r',label="Euler")
plt.legend()
plt.title("Evolution de l'écart (en norme) entre les méthodes numériques et la solution exacte")
plt.ylabel("Résidu (sans dimension)")
plt.xlabel("Temps (secondes)")
plt.grid(True, which="both")
plt.show()

## Matrices des résultats
X=[  0,   1,   2,   3,   4,   5,   6,   7,   8,   9,  10,  11,  12,
         13,  14,  15,  16,  17,  18,  19,  20,  21,  22,  23,  24,  25,
         26,  27,  28,  29,  30,  31,  32,  33,  34,  35,  36,  37,  38,
         39,  40,  41,  42,  43,  44,  45,  46,  47,  48,  49,  50,  51,
         52,  53,  54,  55,  56,  57,  58,  59,  60,  61,  62,  63,  64,
         65,  66,  67,  68,  69,  70,  71,  72,  73,  74,  75,  76,  77,
         78,  79,  80,  81,  82,  83,  84,  85,  86,  87,  88,  89,  90,
         91,  92,  93,  94,  95,  96,  97,  98,  99, 100, 101, 102, 103,
        104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116,
        117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127, 128, 129,
        130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142,
        143, 144, 145, 146, 147, 148, 149, 150, 151, 152, 153, 154, 155,
        156, 157, 158, 159, 160, 161, 162, 163, 164, 165, 166, 167, 168,
        169, 170, 171, 172, 173, 174, 175, 176, 177, 178, 179, 180, 181,
        182, 183, 184, 185, 186, 187, 188, 189, 190, 191, 192, 193, 194,
        195, 196, 197, 198, 199, 200, 201, 202, 203, 204, 205, 206, 207,
        208, 209, 210, 211, 212, 213, 214, 215, 216, 217, 218, 219, 220,
        221, 222, 223, 224, 225, 226, 227, 228, 229, 230, 231, 232, 233,
        234, 235, 236, 237, 238, 239, 240, 241, 242, 243, 244, 245, 246,
        247, 248, 249, 250, 251, 252, 253, 254, 255, 256, 257, 258, 259,
        260, 261, 262, 263, 264, 265, 266, 267, 268, 269, 270, 271, 272,
        273, 274, 275, 276, 277, 278, 279, 280, 281, 282, 283, 284, 285,
        286, 287, 288, 289, 290, 291, 292, 293, 294, 295, 296, 297, 298,
        299, 300, 301, 302, 303, 304, 305, 306, 307, 308, 309, 310, 311,
        312, 313, 314, 315, 316, 317, 318, 319, 320, 321, 322, 323, 324,
        325, 326, 327, 328, 329, 330, 331, 332, 333, 334, 335, 336, 337,
        338, 339, 340, 341, 342, 343, 344, 345, 346, 347, 348, 349, 350,
        351, 352, 353, 354, 355, 356, 357, 358, 359, 360, 361, 362, 363,
        364, 365, 366, 367, 368, 369, 370, 371, 372, 373, 374, 375, 376,
        377, 378, 379, 380, 381, 382, 383, 384, 385, 386, 387, 388, 389,
        390, 391, 392, 393, 394, 395, 396, 397, 398, 399]
Y=[1.0,
  0.95124131098081666,
  0.90486003171650231,
  0.86074024282414874,
  0.81877167699798925,
  0.77884944342152851,
  0.74087376561697371,
  0.70474973207678349,
  0.67038705905409801,
  0.63769986491919406,
  0.60660645551802328,
  0.57702911999639106,
  0.54889393657947361,
  0.52213058782127941,
  0.49667218486229781,
  0.47245510025611848,
  0.4494188089472031,
  0.42750573700237449,
  0.40666111771795854,
  0.38683285474295492,
  0.36797139187614003,
  0.35002958921169502,
  0.33296260532380939,
  0.31672778519580885,
  0.30128455361371126,
  0.28659431375777689,
  0.27262035073859475,
  0.25932773983663071,
  0.24668325921588857,
  0.23465530689354236,
  0.22321382175801902,
  0.2123302084381363,
  0.20197726583552283,
  0.1921291191417035,
  0.1827611551699436,
  0.17384996084022558,
  0.16537326466361982,
  0.15730988107979904,
  0.14963965750858443,
  0.14234342398318617,
  0.13540294523926413,
  0.1288008751400615,
  0.12252071332370845,
  0.11654676396434921,
  0.11086409654401926,
  0.10545850853723668,
  0.10031648991504261,
  0.095425189379778952,
  0.090772382246213618,
  0.086346439888739884,
  0.082136300678291196,
  0.078131442336332263,
  0.074321855636834772,
  0.070698019390509653,
  0.067250876648775562,
  0.063971812067990477,
  0.060852630377373609,
  0.057885535896803939,
  0.055063113053302845,
  0.052378307847508687,
  0.049824410223820947,
  0.047395037300153403,
  0.045084117415382582,
  0.042885874954621594,
  0.040794815914393587,
  0.038805714171628827,
  0.036913598422167043,
  0.035113739756121573,
  0.033401639839052297,
  0.031773019669409167,
  0.030223808884148055,
  0.028750135585790641,
  0.027348316665503712,
  0.026014848598012265,
  0.024746398685340617,
  0.023539796727497349,
  0.022392027099286518,
  0.021300221213443264,
  0.020261650351257172,
  0.019273718842764798,
  0.018333957579467222,
  0.017440017843359072,
  0.016589665436845705,
  0.015780775098878257,
  0.015011325193350364,
  0.014279392656481956,
  0.013583148190561726,
  0.01292085169203664,
  0.012290847902521631,
  0.011691562271860492,
  0.011121497022898419,
  0.010579227408131137,
  0.010063398148874844,
  0.0095727200480576202,
  0.0091059667681666796,
  0.0086619717662986262,
  0.0082396253786527259,
  0.0078378720471804289,
  0.0074557076814598033,
  0.0070921771492015642,
  0.0067463718891146857,
  0.0064174276401655732,
  0.0061045222815556228,
  0.0058068737780185738,
  0.0055237382253025121,
  0.0052544079909516081,
  0.0049982099457408858,
  0.0047545037813439151,
  0.0045226804100288315,
  0.0043021604423830809,
  0.0040923927392622897,
  0.0038928530343442344,
  0.0037030426238452551,
  0.0035224871201244021,
  0.0033507352660601735,
  0.0031873578072367328,
  0.003031946419120809,
  0.0028841126865480691,
  0.0027434871329683917,
  0.0026097182970238543,
  0.0024824718541515923,
  0.0023614297810161397,
  0.0022462895606829326,
  0.0021367634265465544,
  0.0020325776431240066,
  0.0019334718219155777,
  0.0018391982706234425,
  0.0017495213741014932,
  0.0016642170054892626,
  0.0015830719660581739,
  0.0015058834523701545,
  0.0014324585494169032,
  0.0013626137484730127,
  0.0012961744884579527,
  0.0012329747196606327,
  0.0011728564887361838,
  0.0011156695439377643,
  0.0010612709595967287,
  0.0010095247789126619,
  0.0009603016741604989,
  0.0009134786234655058,
  0.00086893860333827851,
  0.00082657029620134405,
  0.00078626781217636826,
  0.00074793042443666697,
  0.00071146231746357295,
  0.00067677234757749821,
  0.00064377381514518404,
  0.00061238424789382688,
  0.00058252519479052439,
  0.00055412202997189345,
  0.00052710376623381458,
  0.0005014028776151795,
  0.00047695513063221706,
  0.00045369942374161656,
  0.00043157763463121633,
  0.00041053447495659754,
  0.00039051735216053506,
  0.00037147623802994453,
  0.00035336354366182626,
  0.00033613400052570261,
  0.00031974454732529572,
  0.00030415422237668212,
  0.00028932406123394563,
  0.00027521699930647253,
  0.00026179777922449529,
  0.00024903286272137508,
  0.00023689034681238634,
  0.0002253398840605147,
  0.00021435260672998912,
  0.00020390105463799008,
  0.00019395910652421258,
  0.00018450191476675979,
  0.00017550584328120336,
  0.00016694840844760559,
  0.0001588082229178612,
  0.00015106494216291991,
  0.00014369921362629694,
  0.000136692628356791,
  0.00013002767499952742,
  0.00012368769603033792,
  0.00011765684612409551,
  0.0001119200525529527,
  0.00010646297751551253,
  0.00010127198230277731,
  9.6334093211319878e-05,
  9.1636969118484031e-05,
  8.716887063857533e-05,
  8.2918630782955567e-05,
  7.8875627050712928e-05,
  7.5029754880154075e-05,
  7.1371402394767048e-05,
  6.7891426380537544e-05,
  6.4581129434580076e-05,
  6.1432238227971737e-05,
  5.8436882828461682e-05,
  5.5587577031378247e-05,
  5.2877199649575371e-05,
  5.0298976715656445e-05,
  4.7846464551994585e-05,
  4.5513533666236483e-05,
  4.3294353432040317e-05,
  4.1183377516760801e-05,
  3.9175330019661416e-05,
  3.7265192286008869e-05,
  3.5448190364095327e-05,
  3.3719783073839564e-05,
  3.2075650657147878e-05,
  3.0511683981668019e-05,
  2.9023974270954267e-05,
  2.7608803335376018e-05,
  2.6262634279354635e-05,
  2.4982102661703017e-05,
  2.3764008086975728e-05,
  2.2605306206813511e-05,
  2.1503101111292045e-05,
  2.0454638091258506e-05,
  1.9457296753566876e-05,
  1.8508584472005732e-05,
  1.7606130157549906e-05,
  1.6747678332366681e-05,
  1.593108349276547e-05,
  1.5154304747003068e-05,
  1.4415400714541999e-05,
  1.3712524674014723e-05,
  1.3043919947766545e-05,
  1.2407915511442266e-05,
  1.1802921817643546e-05,
  1.1227426823219322e-05,
  1.0679992210260319e-05,
  1.0159249791352928e-05,
  9.6638980901081428e-06,
  9.192699088419467e-06,
  8.7444751323202944e-06,
  8.3181059887074929e-06,
  7.9125260455754856e-06,
  7.5267216487630759e-06,
  7.1597285685570814e-06,
  6.8106295898210419e-06,
  6.4785522196261019e-06,
  6.1626665066548138e-06,
  5.8621829669278922e-06,
  5.576350610669897e-06,
  5.3044550653823064e-06,
  5.0458167904330974e-06,
  4.7997893787005915e-06,
  4.5657579410269435e-06,
  4.3431375694435388e-06,
  4.1313718753275084e-06,
  3.9299315988358164e-06,
  3.7383132861415193e-06,
  3.556038031166261e-06,
  3.3826502786642327e-06,
  3.2177166856661872e-06,
  3.0608250384379512e-06,
  2.9115832222466227e-06,
  2.7696182413596266e-06,
  2.6345752868273137e-06,
  2.5061168497192742e-06,
  2.3839218775980749e-06,
  2.2676849721222425e-06,
  2.1571156257730572e-06,
  2.0519374957975666e-06,
  1.9518877135531706e-06,
  1.8567162275276664e-06,
  1.7661851783927729e-06,
  1.6800683045292277e-06,
  1.5981503765377009e-06,
  1.5202266593222081e-06,
  1.4461024004016446e-06,
  1.3755923431705667e-06,
  1.3085202638927428e-06,
  1.2447185312702957e-06,
  1.184027687487672e-06,
  1.1262960496833579e-06,
  1.0713793308533114e-06,
  1.019140279238653e-06,
  9.694483352963316e-07,
  9.2217930539545208e-07,
  8.7721505142374772e-07,
  8.3444319552843008e-07,
  7.9375683925348578e-07,
  7.5505429637147498e-07,
  7.1823883874209957e-07,
  6.8321845456237323e-07,
  6.4990561840419911e-07,
  6.1821707246460822e-07,
  5.8807361848195618e-07,
  5.5939991979800807e-07,
  5.3212431307122081e-07,
  5.0617862917063521e-07,
  4.8149802280274762e-07,
  4.5802081044555648e-07,
  4.3568831618472728e-07,
  4.144447250665845e-07,
  3.9423694360142171e-07,
  3.7501446706848661e-07,
  3.5672925329099947e-07,
  3.3933560256573802e-07,
  3.2279004344709776e-07,
  3.0705122410017216e-07,
  2.9207980895131186e-07,
  2.778383803778721e-07,
  2.6429134519143368e-07,
  2.5140484568078276e-07,
  2.3914667499231749e-07,
  2.2748619663639528e-07,
  2.163942679184443e-07,
  2.0584316710347489e-07,
  1.9580652413195269e-07,
  1.862592547138755e-07,
  1.7717749763633671e-07,
  1.6853855512788948e-07,
  1.6032083613066612e-07,
  1.5250380233847526e-07,
  1.4506791686601048e-07,
  1.379945954208799e-07,
  1.3126615985642509e-07,
  1.2486579398924302e-07,
  1.18777501570988e-07,
  1.1298606630941256e-07,
  1.0747701383873104e-07,
  1.0223657554425785e-07,
  9.7251654150909102e-08,
  9.2509790989563769e-08,
  8.7999134859473971e-08,
  8.370841240890367e-08,
  7.962689995996828e-08,
  7.5744396707258482e-08,
  7.2051199223263547e-08,
  6.8538077206877133e-08,
  6.519625041437416e-08,
  6.201736671520288e-08,
  5.8993481217747516e-08,
  5.6117036412892302e-08,
  5.33808432857579e-08,
  5.077806334840585e-08,
  4.8302191548604509e-08,
  4.5947040011941014e-08,
  4.3706722576646775e-08,
  4.1575640082484299e-08,
  3.9548466376928928e-08,
  3.7620135003670646e-08,
  3.5785826540166944e-08,
  3.4040956552600502e-08,
  3.238116413813671e-08,
  3.0802301025846158e-08,
  2.9300421209051642e-08,
  2.7871771083188372e-08,
  2.6512780064529332e-08,
  2.5220051666328927e-08,
  2.3990355010082639e-08,
  2.2820616750686212e-08,
  2.1707913395313526e-08,
  2.0649463996816064e-08,
  1.9642623203382479e-08,
  1.8684874647087731e-08,
  1.7773824654807946e-08,
  1.690719626578267e-08,
  1.6082823540873063e-08,
  1.5298646149293213e-08,
  1.4552704219285292e-08,
  1.3843133439868994e-08,
  1.3168160401423365e-08,
  1.2526098163455634e-08,
  1.1915342038479934e-08,
  1.1334365581468482e-08,
  1.0781716774851914e-08,
  1.0256014399533994e-08,
  9.7559445828508489e-09,
  9.2802575148472307e-09,
  8.8277643246628539e-09,
  8.3973341092219733e-09,
  7.9878911068002404e-09,
  7.5984120084046698e-09,
  7.2279234002472458e-09,
  6.8754993309201063e-09,
  6.5402589971921677e-09,
  6.2213645426431527e-09,
  5.9180189636334394e-09,
  5.6294641173760043e-09,
  5.3549788271322111e-09,
  5.0938770797957559e-09,
  4.8455063113600459e-09,
  4.6092457759839507e-09,
  4.3845049945797636e-09,
  4.1707222790459928e-09,
  3.9673633284566079e-09,
  3.7739198936982822e-09,
  3.5899085072181381e-09,
  3.4148692747073633e-09,
  3.2483647257007415e-09,
  3.0899787202194144e-09,
  2.9393154087243379e-09,
  2.7959982427810535e-09,
  2.6596690339631061e-09,
  2.5299870586421458e-09,
  2.4066282064272523e-09,
  2.2892841701252727e-09,
  2.1776616751975942e-09]

Z= [9.6224426675032202,
 9.2591836947011021,
 8.9096381921560948,
 8.5732884596031589,
 8.2496363405950159,
 7.9382024846993415,
 7.6385256375484838,
 7.3501619576909913,
 7.0726843592328503,
 6.8056818792944123,
 6.5487590693472546,
 6.3015354095281175,
 6.0636447450638942,
 5.8347347439715413,
 5.6144663752312649,
 5.402513406658497,
 5.1985619217325043,
 5.0023098546649143,
 4.8134665430201302,
 4.6317522972247085,
 4.4568979863285909,
 4.2886446394039606,
 4.1267430619922267,
 3.9709534670299731,
 3.8210451197087885,
 3.6767959957411587,
 3.5379924525279201,
 3.4044289127390606,
 3.2759075598401948,
 3.1522380451127963,
 3.0332372057352432,
 2.9187287935062365,
 2.8085432138095392,
 2.7025172744330122,
 2.6004939438699886,
 2.5023221187451168,
 2.4078564000201723,
 2.3169568776483582,
 2.229488923358153,
 2.1453229912598646,
 2.0643344259795167,
 1.9864032780357812,
 1.9114141261869704,
 1.8392559064843008,
 1.7698217477788454,
 1.703008813438162,
 1.6387181490384051,
 1.5768545358061741,
 1.5173263495931619,
 1.4600454251746524,
 1.404926925670954,
 1.3518892168982508,
 1.3008537464629679,
 1.2517449274204169,
 1.2044900263254501,
 1.159019055509398,
 1.1152646694236166,
 1.0731620648962445,
 1.0326488851543902,
 0.99366512746954161,
 0.95615305428955888,
 0.92005710772541915,
 0.88532382726637981,
 0.85190177060129157,
 0.81974143742915073,
 0.78879519614580951,
 0.75901721329841065,
 0.73036338570295967,
 0.70279127512457573,
 0.67626004542361118,
 0.65073040207462141,
 0.62616453396850502,
 0.60252605741177112,
 0.57977996223981365,
 0.55789255996455345,
 0.53683143387950316,
 0.51656539104847221,
 0.49706441610674357,
 0.47829962680630778,
 0.46024323123931848,
 0.44286848667642748,
 0.42614965995900567,
 0.4100619893866187,
 0.39458164804328749,
 0.37968570850824357,
 0.36535210889886666,
 0.35155962019558856,
 0.33828781480026093,
 0.32551703628155804,
 0.31322837026244077,
 0.30140361640673913,
 0.29002526146321339,
 0.2790764533272711,
 0.26854097608185995,
 0.25840322598062565,
 0.24864818833771085,
 0.23926141528999778,
 0.23022900439886829,
 0.22153757805974816,
 0.2131742636889962]
 
WU=[1.0,
 0.9259183694701123,
 0.89096381921560974,
 0.85732884596030856,
 0.82496363405949502,
 0.79382024846992805,
 0.76385256375484001,
 0.73501619576910093,
 0.70726843592328004,
 0.68056818792943852,
 0.6548759069347162,
 0.63015354095280807,
 0.60636447450638054,
 0.58347347439715103,
 0.56144663752311696,
 0.54025134066584235,
 0.51985619217324364,
 0.50023098546648748,
 0.4813466543020064,
 0.46317522972246422,
 0.44568979863285435,
 0.42886446394039263,
 0.41267430619921469,
 0.39709534670299118,
 0.38210451197087303,
 0.36767959957411173,
 0.353799245252788,
 0.34044289127390281,
 0.3275907559840151,
 0.31522380451127574,
 0.30332372057352064,
 0.29187287935061867,
 0.28085432138095101,
 0.27025172744329851,
 0.26004939438699703,
 0.25023221187450978,
 0.24078564000201588,
 0.23169568776483415,
 0.22294889233581389,
 0.21453229912598545,
 0.20643344259794824,
 0.19864032780357485,
 0.19114141261869319,
 0.18392559064842706,
 0.17698217477788114,
 0.17030088134381438,
 0.16387181490383909,
 0.15768545358061564,
 0.15173263495931494,
 0.14600454251746431,
 0.14049269256709318,
 0.13518892168982216,
 0.1300853746462943,
 0.12517449274204012,
 0.12044900263254317,
 0.11590190555093764,
 0.11152646694235936,
 0.10731620648962284,
 0.10326488851543758,
 0.099366512746953128,
 0.095615305428953148,
 0.092005710772539981,
 0.088532382726636299,
 0.085190177060127367,
 0.081974143742913314,
 0.078879519614579174,
 0.075901721329839494,
 0.073036338570294401,
 0.070279127512455991,
 0.067626004542360119,
 0.065073040207460589,
 0.062616453396849472,
 0.060252605741175802,
 0.057977996223980227,
 0.055789255996453777,
 0.053683143387948738,
 0.051656539104845606,
 0.049706441610672829,
 0.04782996268062889,
 0.046024323123929935,
 0.044286848667641265,
 0.042614965995898993,
 0.04100619893866015,
 0.039458164804327248,
 0.037968570850822586,
 0.036535210889885095,
 0.035155962019556883,
 0.033828781480024724,
 0.032551703628154322,
 0.031322837026242625,
 0.030140361640672214,
 0.029002526146320082,
 0.027907645332725602,
 0.026854097608184829,
 0.02584032259806154,
 0.024864818833770057,
 0.023926141528998777,
 0.023022900439885773,
 0.022153757805973829,
 0.021317426368898558] 

Algorithme de résolution par méthode spectrale de Fourier-Galerkin. N est la taille du vecteur 1D créé représentant la ligne # GrilleNT # Conditions initiales #Pas temporel # (ik)^2 Vecteur # Graphique # passage dans l'espace spectral # backward Euler timestepping # retour dans l'espace temporel # enregistrement des données Algorithme de résolution par méthode des éléments finis, en Euler implicite optimisé en matrices creuses # IC # Opérateur Laplacien # Matrice In # Data mat des données # Solver: (I - k/2*L) u_new = (I + k/2*L)*u_old NX la dim de la matrice ## Résidu (spectral) ## Résidu (Euler) #1 sinon on a la liste vide au début ## Comparaison des figures de diffusion #3ème paramètre contrôle la largeur de la colorbar ## Résultats (spectral) #def exp(X): #     Z=[] #     for l in X: #         Z+=[math.exp(-1/21*l)] #     return Z #EXP=exp(X) ## Matrices des résultats