Skip to main content
Engineering LibreTexts

9.2: Assignment

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

    Let’s consider making a miniature Monte Carlo simulator for a two resistor voltage divider. It is suggested that you save this as Monte.py. This circuit will consist of a voltage source E and two resistors in series, first R1 and then R2. For simplicity, we shall assume that the voltage source is perfectly stable. The two resistors, however, will have a stated tolerance. We would like our program to simulate the action of picking two resistors from bins, that is, randomizing their values, and then determine the voltage across R2. The pseudo code would look something like this:

    1. Don’t forget to import the random module!

    2. Give the user directions.

    3. Ask user for voltage source value.

    4. Ask user for nominal value and percent tolerance of resistor one.

    5. Ask user for nominal value and percent tolerance of resistor two.

    6. Produce randomized value for R1 and R2 (R1rand and R2rand).

    7. Determine the voltage across R2 using the voltage divider rule: VR2 = E * R2rand / (R1rand + R2rand).

    8. Print out the randomized resistor values and resulting R2 voltage (R1rand, R2rand and VR2).

    By now, steps two, three, seven and eight should be obvious. Steps four and five may be handled with a set of input() statements, for example:

    R1 = float(input(“Enter the resistance of R1 in ohms: ”))
    Tol1 = float(input(“Enter the percent tolerance of R1: ”))
    Tol1 = Tol1/100.0 # Turn this from a percentage into a factor
    

    The generation of the randomized resistor values in step five takes a little thought. There are a few different approaches. For example, a randomized percentage could be generated and then applied to the nominal value. Conversely, the maximum and minimums could be calculated and the randomized resistor determined using the scale and offset technique shown earlier. This builds on the prior programming assignment which dealt with resistor tolerance.

    R1max = R1 + R1 * Tol1
    R1min = R1 - R1 * Tol1
    R1rand = R1min + (R1max - R1min) * random.random()
    

    Compare the third line above to the line of code computing x two pages back. The offset value is the lower bound and the scale factor is the difference between the bounds. Because random.random() can be anywhere between 0 and 1, the result can be anywhere between R1min and R1max (just substitute 0 and 1 for random.random() and simplify the equation to prove it to yourself).

    Another approach is to simply adjust the tolerance itself. Think of it in terms of the resistor having anywhere from −100% to +100% of the stated tolerance. Because we’re computing the resistors with factors instead of percentages, all we need is a random float between −1 and +1. Then we multiply this by the stated tolerance to obtain the actual randomized tolerance which can then be used to find the resistor value.

    r = 1.0 – 2.0 * random.random()
    Tol1rand = Tol1 * r
    R1rand = R1 + R1 * Tol1rand
    

    Whichever method is chosen, R1rand represents a randomized version of R1, that is, R1 with tolerance applied. The same approach may be applied to R2 to generate R2rand. These values are then used to compute the voltage as indicated in step seven of the pseudo code.


    This page titled 9.2: Assignment is shared under a not declared license and was authored, remixed, and/or curated by James M. Fiore.

    • Was this article helpful?