Define Components Algebraically (Behavioral Sources)

Like a programming language, the full power of the simulator can be unlocked once you learn how to define your own components with algebraic expressions. Scroll through the screenshots below to see how "Behavioral Sources" work.

For contrast, let's start with a non-behavioral source. Click and drag a Voltage Function Generator from the toolbox to your schematic, then press G and click to add a ground node, and press N and click to add a node name, double-clicking to name it "A":

Click Simulate at the bottom of the window, and click Time Domain to expand the settings. Enter "10m" for Stop Time and "1u" for Time Step. Click on the "A" node name label to add V(A) to the simulation Outputs list:

Click Run Time-Domain Simulation. A plot window appears:

This is just an ordinary sine wave, made from a function generator. But let's see another way to create a sine wave.

Click Build at the bottom of the window. Then, drag an ordinary Voltage Source to the schematic, and double-click to open its parameter window:

Normally the V parameter just holds a number, like 5 for a 5 volt DC supply. However, it can also hold complicated expressions. Type in "COS(2*PI*2000*T)" and close the parameter window. Then, press G and click to add a ground node, and N and click to add a node name label "B" at the top of V2:

Click Simulate at the bottom of the window to open the simulation settings panel, and click the "B" node name label to add V(B) to the Outputs list:

Click Run Time-Domain Simulation. A plot window appears:

We now have two different sinusoids, where V(B) is at twice the frequency and also starts at a different phase offset.

Click Build at the bottom of the window, and drag another Voltage Source to the schematic, and press G to give it its own ground, and press N and double-click to set a node name "C". Double-click V3 and set its voltage to "MAX(V(A), V(B)) + 0.1":

Click Simulate to open the simulation settings window, then click the "C" node name label to add V(C) to the Outputs list, and click Run Time Domain Simulation. A plot window appears:

With three overlapping sinusoids, it's hard to see what's going on. Click V(A) and V(B) in the plot legend to temporarily turn off the display of those signals:

We can see the signal V(C) doing what we asked for algebraically: taking the maximum value of the other two voltages, and adding a small offset. Now, V(C) is a voltage in our circuit just like any other, and you can connect components to it as you wish.

Click Build to return to build mode. Click and drag a Current Source from the toolbox to the schematic, and drag to wire it in parallel to V3 as shown:

Double-click I1 and set its current to "V()/100":

We've now defined I1 to act as a current source where the current is equal to its current voltage drop V() divided by the number 100. This is equivalent to a 100 ohm resistor!

Click Simulate and click + Add Expression and type to add "I(I1.nA)" to the simulation Outputs list:

Click Run Time-Domain Simulation. A plot window appears:

Unsurprisingly, the current through I1 looks just like the shape of V(C) that we saw earlier, but scaled down to the ~10mA range. Our algebraically-defined resistor works perfectly.

It's more interesting that we can use this capability to model arbitrary V-I relationships.

Click Build at the bottom of the window. Double-click I1 and set its current to "IF(V() < 0, -1 * SQRT(-V()), SQRT(V()))/100":

Press F5 to repeat the simulation. A plot window appears:

Notice that the shape of the current plot has changed significantly, in a nonlinear way. It appears almost "compressed" near the top and bottom.

Our expression has turned I1 into a nonlinear resistor, where the current is proportional to the square root of the voltage. This technique is powerful and can be used for modeling arbitrary linear or nonlinear devices within the simulator.

That's it! Now you know how to program the simulator, just like a programming language.

Click below to open the final circuit, or try it yourself from scratch (recommended).


« Return to Table of Contents

Search Documentation


About CircuitLab

CircuitLab is an in-browser schematic capture and circuit simulation software tool to help you rapidly design and analyze analog and digital electronics systems.