Ohm's Law

原文地址: Ohm's Law

Introduction

C++基础编程,使用Ohm's Law解决电路的计算问题。

Ohm's Law

Programming concepts:

  • Looping instructions: for, while, do/while
  • Reading and writing text files

ECE concepts:

  • Analysis of resistive circuits: KVL, KCL, Ohm's Law
  • The voltage divider
  • The Wheatstone bridge

Background

Voltage divider and Wheatstone bridge circuits
The voltage divider, shown in Figure 1, should be familiar to you from the study of resistive circuits. To analyze this or any other circuit, we must determine all of the unknown voltages and currents (in this case, I, V1 and V2) from the given information (in this case, the source voltage Vs and the resistances R1 and R2.)

The analysis is very simple, and begins with KVL and Ohm's Law,

By now, it should be easy for you to visualize how to write a C++ program to implement these equations, and compute {I, V1, V2} for any values of {Vs, R1, R2}.

The Wheatstone bridge circuit shown in Figure 2 is considerably more complex. Nevertheless, we can perform the analysis armed only with KVL, KCL, Ohm's Law, and elementary algebra.

We can also take advantage of some notational simplifications that are easy to implement as computer instructions.

Begin by noting that all resistor voltages and currents are related through Ohm's Law,

Therefore, we can solve for the voltages and find the currents directly. By KVL, we can also express two of the voltages in terms of the others,

At this point, we need only solve for {V1, V2, V3} to determine the remaining voltages and all of the currents. Next, if we apply KCL at the nodes at either end of the current source,

We now have two linearly independent equations relating the unknown voltages {V1, V2, V3}. We can obtain a third equation by applying KCL at the node at the top of R2 and R3,

We have analyzed the circuit to obtain three linearly independent equations in the three unknown voltages, of the general form

This is not a typical form of the Wheatstone bridge. In the original application for this circuit, a voltmeter is used in place of the current source, and R5 is an unknown resistance one wishes to measure, usually by varying R4 until the voltmeter reads zero.

To solve any system of equations like this, we may substitute the expression for V1 from the last equation into the other two, yielding a+e

Now we have a pair of equations in two unknowns of the general form yw

These results provide us with an algorithm for solving the Wheatstone bridge circuit:

  • Use the given values for {Vs, Is, R1, R2, R3, R4, R5} to compute {a, b, c, d, e, f}.
  • Use {a, b, c, d, e, f} to compute {w, x, y, z}.
  • Use equations (4) and (5) to compute V3 and V2.
  • Use equation (3) to compute V1.
  • Use equations (1) and (2) to compute V4 and V5.

Instructions

Develop software according to the following specifications and submit whatever portion you have completed to the class repository before midnight tonight. For all in-class assignments, you must include line-by-line comments to explain what your does and why!

You must also choose meaningful names for all variables so that it will be easy for a reader to understand your code.

  1. Write a computer program that will display the following introductory message to standard output:

ECE 0301: Circuit Solver for Voltage Divider and Wheatstone bridge example circuits.
Don't forget to include comments! Type them in your code as you complete each item.

  1. Create a text file with the word Divider on the first line, and save it to a file named "divider_wheatstone_circuits.txt" in the directory where your program is stored. For the remainder of this assignment, all input will be read from this file.

Declare an ifstream object, use it to open the input file, and read the first line with the >> operator. If the first line is anything other than Divider or Wheatstone, the program should exit with the error message (printed to standard output)
ERROR! Invalid header. and a return value of -1.
Test your program by changing the first line of the text file, and make sure that the program exits under the proper conditions.

  1. If the first line of the file is Divider, the program should read three more lines from the file, which hold values for the voltage source Vs and the resistors R1 and R2. Store Vs in a double variable, and store R1 and R2 in int variables.

Define an ofstream object, and use it to open a text file for output named "divider_wheatstone_solutions.txt". Write messages to the output file to report the values that were read from the file.
Type the following into the input file and use it to test your program.

  1. Modify your program so that, if the first line of the file is Divider, it will solve the voltage divider circuit by computing the loop current I and the resistor voltages V1 and V2, and write messages to the output file reporting the computed values.

Test your program using the input file from step 3, and make sure the computed values are correct. The required format for the output file is shown below for this case.
Modify the text file by changing the component values, and test again. The loop current and resistor voltage will be reported with more digits if the answers are not integers or terminating decimals. Can your program solve any voltage divider problem?
When you are certain your program is correct, save it in a file named: ece0301_ICA04_step04.cpp
Submit this file to the class repository.

  1. If the first line of the file is Wheatstone, the program should read seven more lines from the file, which hold values for the voltage source Vs, the current source Is, and the resistors R1 through R5. Store Vs and Is in double variables, and store the resistances in int variables.

Write messages to the output file to report the values that were read from the file.

  1. Modify your program so that, if the first line of the file is Wheatstone, it will solve the

Wheatstone bridge circuit by computing the resistor voltages V1 through V5, and resistor currents I1 through I5, and write messages to the output file reporting the computed values.
Test your program using the input file from step 5, and verify that the computed values are correct. The required output file format is shown below for this case.

  1. The method we derived for solving the Wheatstone bridge circuit will not work in all cases.

Your program should produce a divide-by-zero error if any of the following conditions are true (go back and look at the equations to see why).
Furthermore, if either of the quantities in the right column are very small but not zero, then round-off error in representing them could occur when they are used as the denominator of a division operation. Round-off error for double-precision floating-point calculations is on the order of 1015 , and we will require that all denominators be at least 100 times larger (in magnitude) than this value.
Modify your program so that, if it is directed to solve the Wheatstone bridge circuit, and any one of the following conditions are true
ERROR! Unstable floating-point division. to standard output, and exit with a return value of -1.
Modify your text file to try each of the four cases that produce errors, and confirm that your program generates errors as expected.
When you are certain your program is correct, save it in a file named: ece0301_ICA04_step07.cpp
Submit this file to the class repository.

  1. Modify your program so that it will read an additional line from the input file containing an integer n. This will be line 5 of the file for a voltage divider circuit and line 9 of the file for a

Wheatstone bridge circuit. Add nested loops to your program so that it solves the circuit repeatedly for each resistor value taking the values {R, 2R, , nR}.
When you are certain your program is correct, save it in a file named: ece0301_ICA04_step08.cpp
Submit this file to the class repository.

  1. Modify your program so that it continues to read lines from the file until the end of the file is reached. After it has read all of the lines in the file for a given circuit, and solved the circuit and written the corresponding messages to the output file, the program should read the next line of the file. This line should contain either Divider or Wheatstone, just as on line 1, and your program should proceed to take the appropriate action, either reading more lines and solving the circuit, or exiting with return value -1.

For this case, the required output file format is summarized below. Note that you must increment the number in the circuit title for each new circuit.
When you are certain your program is correct, save it in a file named: ece0301_ICA04_step09.cpp
Submit this file to the class repository.
Don't forget to include comments! You will lose credit if you leave them out, even if your code functions as directed!

(本文出自csprojectedu.com,转载请注明出处)

你可能感兴趣的:(电路)