Saltar al contenido principal
LibreTexts Español

28.3: Trazado

  • Page ID
    129631
  • \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \) \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)\(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\) \(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\)\(\newcommand{\AA}{\unicode[.8,0]{x212B}}\)

    Varios módulos están disponibles en python para el trazado. Aquí mostraremos cómo usar el módulo pylab (que es equivalente al módulo matplotlib). Por ejemplo, podemos trazar fácilmente los datos en las dos matrices de la sección anterior para trazar la posición frente al tiempo para el objeto:

    código python\(\PageIndex{1}\)

    Trazado de dos matrices

    #import the pylab module
    import pylab as pl
    
    #define an array of values for the position of the object
    position = [0 ,1 ,4 ,9 ,16 ,25]
    #define an array of values for the corresponding times
    time = [0 ,1 ,2 ,3 ,4 ,5]
    
    #make the plot showing points and the line (.−)
    pl.plot(time, position, '.-')
    #add some labels:
    pl.xlabel("time") #label for x-axis
    pl.ylabel("position") #label for y-axis
    #show the plot
    pl.show()
    

    Salida

    clipboard_e6fdeed3c9af44df22b923078b882c3a2.png
    Figura A4.3.1: Usando dos matrices y trazándolas.

    Ejercicio\(\PageIndex{1}\)

    ¿Cómo modificarías el código Python anterior para mostrar solo los puntos, y no la línea?

    Responder

    Podemos usar Python para trazar cualquier función matemática que nos guste. Es importante darse cuenta de que las computadoras no tienen una representación de una función continua. Así, si quisiéramos trazar una función continua, primero necesitamos evaluar esa función en muchos puntos, y luego trazar esos puntos. El módulo numpy proporciona muchas características útiles para trabajar con matrices de números y aplicar funciones directamente a esas matrices.

    Supongamos que nos gustaría trazar la función\(f(x) = cos(x^{2})\) entre\(x = −3\) y\(x = 5\). Para hacer esto en Python, primero generaremos una matriz de muchos valores de\(x\) entre\(−3\) y\(5\) usando el paquete numpy y la función linspace (min, max, N) que genera puntos\(N\) linealmente espaciados entre min y max . Luego evaluaremos la función en todos esos puntos para crear una segunda matriz. Finalmente, trazaremos las dos matrices una contra la otra:

    código python\(\PageIndex{2}\)

    Trazar una función de 1 variable

    #import the pylab and numpy modules
    import pylab as pl
    import numpy as np
    
    #Use numpy to generate 1000 values of x between -3 and 5.
    #xvals is an array with 1000 values in it:
    xvals = np.linspace(-3,5,1000)
    
    #Now, evaluate the function for all of those values of x.
    #We use the numpy version of cos, since it allows us to take the cos
    #of all values in the array.
    #fvals will be an array with the 1000 corresponding cosines of the xvals squared
    fvals = np.cos(xvals**2)
    
    #make the plot showing only a line, and color it
    pl.plot(xvals, fvals, color='red')
    #show the plot
    pl.show()
    

    Salida

    clipboard_e73943421617dfd4ff5e17fdc4bc74816.png
    Figura A4.3.2: Trazar una función usando matrices.

    This page titled 28.3: Trazado is shared under a CC BY-SA license and was authored, remixed, and/or curated by Howard Martin revised by Alan Ng.