Skip to main content
Engineering LibreTexts

10.2: Assignment

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

    We shall now write a program which will make use of iteration to illustrate the Maximum Power Transfer Theorem. It is suggested that the program be saved as MaxPower.py. This program will feature both tabular and graphical output. The program should ask the user for a voltage source value along with its internal resistance. It will then create a table of results for a range of load resistances. The load resistance will range from one-tenth of the internal resistance to twice the internal resistance, incrementing by one-tenth of the internal resistance each time. For example, if the internal resistance is 200 ohms, the load will start at 20 and increase by 20 until it gets to 400. This will create ten load values less than the internal resistance and ten loads which are greater. The table should show four columns: The load resistance, load voltage, load current, and load power. It should also indicate the maximum power found. Once the table is complete, the program should draw a graph of the load power versus the load resistance. Here’s one possible pseudo-code:

    1. Give the user directions.

    2. Ask user for source voltage and resistance (E and Rsource).

    3. Initialize the maximum power (Pmax) to zero.

    4. Print out a heading for the table (Rload, Vload, I and Pload).

    5. Start a loop which initializes the load resistance (Rload) at one-tenth of the source resistance, increments the load resistance by one-tenth of the source resistance and finishes when the load reaches twice the source resistance.

    6. Compute the current (I), the load voltage (Vload) and load power (Pload) from:

    I = E/(Rsource+Rload)

    Vload = E * Rload/(Rsource+Rload)

    Pload = I * Vload

    7. Compare the load power just calculated to Pmax. If it’s larger, reset Pmax to this value.

    8. Print out Rload, Vload, I and Pload.

    9. End of loop (from step five).

    10. Print out Pmax.

    11. Determine the proper scaling factor for the graph.

    12. Print out a title for the graph.

    13. Start a loop which will initialize the load resistance (Rload) at one-tenth of the source resistance, increment the load resistance by one-tenth of the source resistance and finish when the load reaches twice the source resistance (same as step five).

    14. Compute the current (I), the load voltage (Vload) and load power (Pload) as in step six.

    15. Plot Pload using the scale factor from step 11.

    16. End of loop (from step 13).

    A number of steps should be very familiar by now such as those giving the user directions, obtaining user data, and basic computations. Let’s focus instead on the new items beginning with step three. We need to initialize Pmax to zero. The reason for this may not be obvious. This variable is going to hold the highest load power found so far. Each time through the loop the currently calculated power (Pload) will be compared to Pmax. If Pload turns out to be larger than Pmax then we have a new Pmax and we’ll reset Pmax to this new Pload. If the newly calculated Pload is not larger than Pmax, we do nothing. By the time the loop is finished, every Pload will have been examined and Pmax will indeed be the maximum Pload found. This process can be thought of as a software version of a ratchet.

    Pmax=0
    

    Step four is just a print out for the heading. It might be advisable to separate each column with a tab or two (\t).

    Step five starts the loop. We have a choice between a for and a while. First, the load resistance Rload needs to be initialized. This will be the first value used in the loop. The terminating value will be twice the source resistance. It’s possible to do this in one statement using for and range().

    for Rload in range(Rsource/10, 2*Rsource, Rsource/10):
    

    There is a practical problem here in that range() expects integers and we have floats. Although this issue might be resolved with the int() function, it won’t work in every case (for example, if Rsource is very small, the increment might be zero once it’s converted to an integer and this would prevent the loop from operating). In comparison, the while loop is always safe.

    To use the while loop, the load resistance needs to be initialized manually:

    Rload = 0.1*Rsource
    

    Once this is done the loop termination point may be set:

    while Rload <= 2.0*Rsource:
    

    And finally, the load resistance needs to be manually incremented at the end of the loop (i.e., after all of the computations and printouts):

         Rload = Rload + 0.1*Rsource
    

    A shortcut way of saying this is:

         Rload += 0.1*Rsource
    

    Remember, everything which is inside the loop (i.e., the statement block) must be indented one level. Failure to do this will mess up the definition of the loop. Following the while or for is step six which deals with the various computations for I, Vload and Pload, and then step seven which involves Pmax, as discussed earlier:

         if Pload > Pmax:
              Pmax = Pload
    

    At this point, the values may be printed out. This is shown below with a single tab for spacing. Some form of rounding may be appropriate here.

         print( Rload, "\t", Vload, "\t", I, "\t", Pload )
    

    Using the while loop, the next line would be the increment for Rload, which completes step nine. Step 10 is a simple print out of Pmax. Before continuing, note that the loop which starts at step 13 is basically the same as the loop we just examined. The differences are that we don’t need to deal with Pmax again and instead of printing out table values we’ll print out the graph points. Consequently, we won’t rehash this discussion but instead move directly to the issue of creating a graph.

    Our technique for creating a graph will rely on simple text output. While definitely a step below modern high resolution graphics, the graph will be perfectly serviceable, even if not a work of art. Indeed, this is the same method that was used with the early text-only versions of the SPICE circuit simulator. To create this graph, the image will be rotated 90 degrees clockwise so that the normal vertical axis (Pload) will move to horizontal and increase from left to right while the normal horizontal axis (Rload) will be located vertically along the left edge and increasing from top to bottom. The idea is to turn Pload into either a point or solid bar that is displaced from left to right for increasing values.

    One possibility is to turn Pload into an integer and use it to iterate a character such as an asterisk:

         print( int(Pload) * "*" )
    

    This will create a bar of asterisks Pload long. That is, if Pload is 34 we’ll have printed out 34 asterisks in a horizontal row. (Note: It would be more accurate to add 0.5 to Pload before turning it into an integer as the int() function will truncate, not round. Adding 0.5 will effectively round the result.) To create a single dot, simply iterate a blank space for one less and then add the asterisk:

         print( int(Pload-1) * " " + "*" )
    

    There are two problems with this, one minor and one major. The minor issue is that we might wish to include the Rload value (so we know which value produced the peak). This can be done with a small modification (shown using bar version):

         print( Rload, "\t\t", int(Pload) * "*" )
    

    The larger problem is that Pload might be so large that it flows off screen (actually, it will wrap down to the next line which can be very confusing). For that matter, it might be so small (e.g., milliwatts) that it will appear as only a single line at 0 (there is no way to print partial spaces). Fortunately, both these ills may be cured by scaling all of the values. For very large powers we’ll scale to make them smaller and for very small powers we’ll scale to make them larger. While we could ask the user for a scaling factor, it would be better if we compute an optimal one.

    Computing an optimal scaling factor turns out to be fairly easy. All we need to do divide the maximum graphing width by the maximum power (which we have already determined, namely Pmax). For example, if the width of the graphing area is 50 characters and Pmax is 0.1, then the scaling factor is 500. That is, every value of Pload will be multiplied by 500 so that the entire space will be filled. Conversely, if Pmax is 200 then the scaling factor will be 0.25 and every value will be reduced to one quarter of its size in order to fit the entire curve on the screen. 50 is a reasonable screen width for this exercise so the scaling factor is:

    sf = 50.0 / Pmax
    

    Note that this computation is performed before the second loop in step 11 prior to printing out a graph title and the start of the second loop. This single scaling factor is needed inside the loop for the plot so it doesn’t make sense to compute it inside the loop itself. When it comes time to print the graph inside the second loop (step 16), we modify the print statement above to include the scale factor (shown using bar version and without rounding):

         print( Rload, "\t\t", int(Pload*sf) * "*" )

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

    • Was this article helpful?