Saltar al contenido principal
LibreTexts Español

4.2: El Programa

  • Page ID
    83265
  • \( \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}}\)

    Primero, observe el uso de #define para los prototipos pi y de funciones. Las funciones, aunque pequeñas, hacen que la sección principal sea mucho más legible.

    #include <stdio.h>
    #include <math.h>
    
    #define M_PI 3.141592653
    
    void give_directions( void );
    double find_fc( double res, double cap );
    double find_xc( double freq, double cap );
    double find_dB( double gain );
    int main( void )
    {
           double r, c, xc, gain, dB, steps;
           double fc, f, fstart, fstop, ffactor;
          
           give_directions();
    
           printf("Enter the resistance in ohms:");
           scanf("%lf", &r);
           printf("Enter the capacitance in farads:");
           scanf("%lf", &c);
    
           fc = find_fc( r, c );
    
           printf("\nThe critical frequency is %lf hertz.\n\n", fc);
    
           printf("Enter the start frequency in hertz:");
           scanf("%lf", &fstart);
           printf("Enter the stop frequency in hertz:");
           scanf("%lf", &fstop);
           printf("Enter the number of steps per decade to display:");
           scanf("%lf", &steps);
    
           printf("Frequency (Hz)\t\t\tGain (dB)\n"); /* \t is a tab */
    
           ffactor = pow( 10.0, 1.0/steps );
    
           f = fstart;
    
           while( f <= fstop )
           {
                 xc = find_xc( f, c );
                 gain = xc/sqrt(r*r + xc*xc); /* could use pow() for square here,
                                                 but mult by self executes faster */
    
                 dB = find_dB( gain );
                 printf("%10.1lf\t\t%10.1lf\n", f, dB ); /* %10.1lf is 10 spaces
                                                       with 1 digit after decimal */
    
                 f *= ffactor; /* shortcut for f=f*ffactor; */
           }
    }
    
    void give_directions( void )
    {
           printf("Bode Table Generator\n\n");
           printf("This program will display dB gains for a simple RC circuit\n");
    }
    
    double find_fc( double res, double cap )
    {
           return( 1.0/(2.0*M_PI*res*cap) );
    }
    
    double find_xc( double freq, double cap )
    {
           return( 1.0/(2.0*M_PI*freq*cap) );
    }
    
    double find_dB( double gain )
    {
           return( 20.0 * log10( gain ) );
    }
    

    Entra a este programa y pruébalo usando R = 1 k\(\Omega\), C = 100 nF, frecuencia de inicio = 100 Hz, frecuencia de parada = 20 kHz, puntos por década=8. Considera qué podría salir mal con este programa y cómo podrías eludir esos problemas. Por ejemplo, considere lo que podría suceder si el usuario ingresó 0 para el valor de resistencia, o una frecuencia de parada que fuera menor que la frecuencia de inicio.


    This page titled 4.2: El Programa is shared under a CC BY-NC-SA 4.0 license and was authored, remixed, and/or curated by James M. Fiore via source content that was edited to the style and standards of the LibreTexts platform; a detailed edit history is available upon request.