Sortie Print

Résolu
turcmax Messages postés 5 Statut Membre -  
Phil_1857 Messages postés 1883 Date d'inscription   Statut Membre Dernière intervention   -
Bonjour,
je ne comprend pas pourquoi rien ne se passe



Configuration: Windows / Chrome 81.0.4044.113

2 réponses

  1. turcmax Messages postés 5 Statut Membre
     
    mport numpy as nt
    import matplotlib.pyplot as plt
    class FoncOptim():
    #    
        def __init__(soi,h,a,b,N):
            soi.a=a
            soi.b=b
            soi.N=N
            soi.h=h
    #
        def h(soi,x):
            y=1.0+5.0*(x**4-x**2)*nt.exp(-x**2)
            return y
    #
        def Max_min(soi,A):
            
            #
               MV=A[0]; mv=A[0] 
               n=len(A) 
            #
               for i in nt.arange(1,n):
            #    
                   if (MV < A[i]):
                        MV=A[i]
            #      
                   if (mv > A[i]):
                        mv=A[i]
            #
               return(MV,mv)
    #
        def func_opt(soi,a,b,N): 
          x=nt.zeros(N+1) 
          y=nt.zeros(N+1)  
        # 
          dx=(b-a)/N
          for i in nt.arange(0,N+1):
                   x[i]=a+i*dx
                   y[i]=h(x[i])
                   plt.plot([x[i],x[i]],[0,y[i]],'r')
        #    
          MAX,min=Max_min(y)
        #
          plt.plot(x,y,'b')
          plt.xlabel('x')  
          plt.title('Fonction étudiée')
          plt.ylabel('y')
          plt.grid() 
        #  
        #
          XMAX=[]; xmin=[]
          for j in nt.arange(0,N+1):
        #    
             if (nt.absolute(y[j]-MAX) < 0.00001):
                    XMAX.append(x[j])
        #            
             if (nt.absolute(y[j]-min) < 0.00001):
                    xmin.append(x[j])           
        #            
          print("Le total des valeurs maximales est %3d" % len(XMAX))
        #    
          for val in XMAX:
              print("La valeur maximale de %f se situe à x=%f" % (MAX,val))
        #        
          print(" \nLe total des valeurs minimales est %3d" % len(xmin))      
        # 
          for val in xmin:
              print("La valeur minimale de %f se situe à x=%f" % (min,val))
        #        
        #      
          return (MAX,min)  
    opti=FoncOptim(h,-1,5,150)
    print('a=',opti.a)
    print('b=',opti.b)
    print('N=',opti.N)
    a= -1
    b= 5
    N= 150
    ti.
    print(opti.Max_min([2,4,7,9,10,5]))
    (10, 2)
    opti.func_opt(-1,5,150)
    ---------------------------------------------------------------------------
    NameError                                 Traceback (most recent call last)
    <ipython-input-471-1156f43b0e96> in <module>
    ----> 1 opti.func_opt(-1,5,150)
    
    <ipython-input-466-a900159e460a> in func_opt(soi, a, b, N)
         37                plt.plot([x[i],x[i]],[0,y[i]],'r')
         38     #
    ---> 39       MAX,min=Max_min(y)
         40     #
         41       plt.plot(x,y,'b')
    
    NameError: name 'Max_min' is not defined
    0
  2. Phil_1857 Messages postés 1883 Date d'inscription   Statut Membre Dernière intervention   169
     
    Bonjour turcmax,

    "name 'Max_min' is not defined"
    le message est clair : le nom Max_min n'est pas défini

    La fonction Max_min et la fonction func_opt sont dans la même classe
    Donc, il ne faudrait pas plutôt faire ça :
    MAX,min=soi.Max_min(y) ?

    Ceci dit, je vois que tu a francisé le mot "self" en "soi", ca marche mais c'est déconseillé
    La convention est d'utiliser self comme dans les autres langages orientés objet
    C'est une convention forte reconnue par tout le monde ...
    0