Circuit Design using programming C++

Modelling and simulation of an electrical circuit with two voltage sources

Aniekan Umanah
5 min readMay 26, 2021

Hello! Today, we’ll look at how programming can be used to solve engineering problems. A simple electrical circuit with two voltage sources and three load components would be used to demonstrate this using this code.

Why write Programs?

Computer programmes facilitate the completion of complex or time-consuming tasks. They also aid in the reduction of errors. A cash register, for example, only requires the cashier to enter a set of values into the system, and it calculates whether or not change is required, as well as what denominations to be given (e.g., 1 pound coin, 3 20p coins, etc.). This expedites and simplifies payment for goods while eliminating human error from miscalculations.

Application

Some engineering problems can be quite complex and time-consuming to solve. Programming can assist in making these types of problems easier to solve and in reducing the time required to obtain results. As a result, better and more efficient products are produced. To see how we can do this, let’s take a look at the simple electrical circuit below.

Circuit diagram
Components of Rx

Procedure

Writing a computer program requires a methodical approach:

  • define the problem — what so we want to solve?
  • plan the solution — use algorithms.
  • code and document the program — write the program and explain its contents.
  • test and debug — test the program.

Problem — For our circuit, we want to find the unknown parameters, Rx, ix, iy, and Vz. To do this we would nee to establish an algorithm which would be able to solve for these parameters.

Solution — Can use mesh analysis to find currents:

Left hand loop:

Right hand loop:

Solve simultaneous equations using matrices:

Write down matrix solution, and divide their determinants:

Substituting i1 for ix, and i2 for iy,

a1 = Rx + Rz, a2 =Rz.

b1 = Rz, b2 = Ry + Rz.

c1 = V1, c2 = V2.

Write the code:

Explanation

We utilise functions to breakdown the program into small self-contained routines.

// function prototypes 
float currentIx(float,float,float,float,float);
float currentIy(float,float,float,float,float);

We inform the C++ compilier that we want to to take in five float input variable parameters and return a float output for both functions. Within these functions, arithmetic operations are performed in-line with the developed algorithm.

Every C/C++ program consists of at least one main functions. This function is always the first to execute in the program.

int main(void)
{
// declare variable
// enter values
// perform calculations / call functions
// display results
return 0;
}

We declare the variable that will be used to store the data that is fed into the system. The programme then prompts the user to enter values for the particular variable. The variables are used to perform calculations in accordance with the algorithm that was developed. The prototype functions are invoked to provide their outputs, which are then displayed.

Test and Debug — Build the program to check for errors and perform some test to verify the operation. To evaluate, we use the parameters specified in the circiut diagram.

Executed code

Verify:

Circuit simulation

The results match!

Discussion

As we could see, the programme could provide correct answers. The programme can also be adapted to fit the type of circuit you want to test. We can see from the derivations and calculations that obtaining this takes a long time and that errors can occur, so having to do this all the time would not be very efficient method. In contrast, the computer programme requires a set of input variable to produce the desired results.

See you later! 👋🏾

--

--

Aniekan Umanah
Aniekan Umanah

Written by Aniekan Umanah

Electronic Engineering | Computer Systems

Responses (2)