Skip to main content
Engineering LibreTexts

4.1: Introduction

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

    In this exercise we will be taking a look at using loops, or iteration. We shall also investigate a few functions in the standard library. Computers are of course ideal for repetitive calculations such as those needed to create data tables.

    If a specific number of iterations are required, usually the for() loop construct is the best choice. For situations where the precise number of iterations is not known, or where the termination condition can be expressed in an easier fashion, the while() loop construct is preferred. This exercise shall focus on using the while() construct, although it is recommended that upon completion you attempt to alter the code to utilize the for() construct.

    This program will be used to compute a table of values for a series RC circuit. It will compute and display the values that might be used for Bode plot. This plot shows the gain or attenuation of a network with respect to frequency. In this example we shall use the voltage across the capacitor as the output quantity. The gain is normally displayed in decibel form (or dB for short) rather than as an ordinary factor. This program won’t draw the curve, but rather list the values so that you could draw the curve yourself.

    First, the program will need to obtain the resistor and capacitor values from the user. From these it will compute and display the critical frequency, \(f_c\). The user will then be asked to specify a range of frequencies over which to compute the gain values. This will consist of a start frequency, an end frequency and the number of frequency points to calculate per decade (a decade being a factor of 10). Normally the frequencies would not be equally spaced in hertz, but rather, equally spaced by a factor. In other words, the ratio of any two adjacent frequencies would be the same rather than the number of hertz between them. For example, if we started at 100 Hz, finished at 1000 Hz and only wanted 2 points per decade, we wouldn’t use 100, 550, and 1000 (1000 starts a new decade, so only count the 100 and 550 as the “two points per decade”). 550 is equidistant between 100 and 1000, but the ratios are way off: It’s 5.5 times larger than the starting point but not even a factor of 2 smaller than the ending point. In fact, we want the sequence 100, 316, 1000 because the ratio of 100 to 316 is 3.16 and the ratio of 316 to 1000 is also 3.16. How do we know to use this ratio? Simple! Think of the specification “two points per decade”. You want a number that when multiplied by itself yields 10, i.e. the square root of 10 (3.16). If you wanted 5 points per decade you’d use the 5th root of 10 and so on. Here’s a pseudo-code to start with:

    1. Give user appropriate directions

    2. Obtain resistance value in ohms and capacitance value in farads from user.

    3. Compute and print critical frequency.

    4. Obtain start and stop frequencies (in Hz) along with number of points per decade from user.

    5. Print out table heading consisting of Frequency (Hz) and Gain (dB).

    6. Determine frequency factor based on points per decade.

    7. Initialize frequency to start value.

    8. Start a loop that continues as long as frequency is less than or equal to the stop frequency.

    9. Compute gain in dB.

    10. Print out frequency and dB gain.

    11. Multiply frequency by frequency factor to obtain next frequency of interest.

    12. End of loop, and of program.

    The only thing that might remain a bit fuzzy is the computation of gain and its conversion to dB. The conversion to dB is defined by the following equation:

    \[dB = 20 * \log_{10} ( gain ) \nonumber \]

    The gain is basically a voltage divider between the resistor and the capacitive reactance, so let’s refine step 9:

    9a. Determine capacitive reactance Xc.

    9b. Calculate gain based on the vector voltage divider: gain = −\(j\)Xc/(R−\(j\)Xc).

    Reminder: The magnitude of this is Xc/\(\sqrt{(R^2 + Xc^2)}\).

    9c. Calculate dB based on \(dB = 20 * \log_{10} ( gain )\)

    The math library (math.h) will come in handy for common log, roots, and so forth. The functions of interest are pow(), log10(), and sqrt().

    This page titled 4.1: Introduction 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.