原文地址: Ohm's Law
C++基礎編程,使用Ohm's Law解決電路的計算問題。node
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.)git
The analysis is very simple, and begins with KVL and Ohm's Law,express
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.app
We can also take advantage of some notational simplifications that are easy to implement as computer instructions.less
Begin by noting that all resistor voltages and currents are related through Ohm's Law,ide
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,oop
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,ui
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,this
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:
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.
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.
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.
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.
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.
Write messages to the output file to report the values that were read from the file.
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.
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.
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.
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,轉載請註明出處)