0% found this document useful (0 votes)
272 views19 pages

Using Mathcad To Derive Circuit Equations and Optimize Circuit Behavior

The document describes how to use Mathcad to derive circuit equations and optimize circuit behavior. It provides examples of using Mathcad's symbolic math capabilities to rearrange circuit equations, such as solving for an unknown resistor value. Mathcad allows chained symbolic processing to solve multiple equations for unknowns and derive transfer functions. The document illustrates deriving the transfer function for an inverting amplifier circuit as an example of using Mathcad's symbolic math features to optimize circuit design.

Uploaded by

Pil42
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
272 views19 pages

Using Mathcad To Derive Circuit Equations and Optimize Circuit Behavior

The document describes how to use Mathcad to derive circuit equations and optimize circuit behavior. It provides examples of using Mathcad's symbolic math capabilities to rearrange circuit equations, such as solving for an unknown resistor value. Mathcad allows chained symbolic processing to solve multiple equations for unknowns and derive transfer functions. The document illustrates deriving the transfer function for an inverting amplifier circuit as an example of using Mathcad's symbolic math features to optimize circuit design.

Uploaded by

Pil42
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 19

Using Mathcad To

Derive Circuit Equations


and Optimize Circuit Behavior

By:

James C. (Jim) Bach


EE Analysis Engineer & Mathcad Instructor
Electrical Design & Analysis Group
Delphi Corporation
Kokomo, IN, USA
[email protected]

Using Mathcad To Derive Circuit Equations and Optimize Circuit Behavior

Introduction
For those of you who are not familiar with it, Mathcad (produced by Mathsoft, not to be
confused with Matlab or Mathematica) is a general purpose mathematical analysis tool with
WYSIWYG formula entry and which can operate in both numeric and symbolic modes;
symbolic math is performed by the underlying Maple engine, common to many of these
Math Processor applications.
Equations are entered in a natural, text book format
wherein integrals look like integrals, and summations look like summations; FORTRAN-like
coding syntaxes need not apply.
It is a Swiss-army knife of mathematical functions,
operators, GUI widgets, and graph types. It comprehends (and automatically converts
between) units, meaning that when you divide a Volts quantity by an Amps quantity,
your result is automatically an Ohms quantity. From the EEs point of view, Mathcad is a
tool that can be used to derive system- or circuit-level equations (remember solving N
equations with N unknowns?), optimize circuit component values, perform worst-case
analysis, process large vectors (matrices) of data, perform image-processing and signalprocessing tasks, curve-fit lab-collected measurements, and even create .AVI movies
(animations of data).
After some exploration and experimentation with Mathcad I can
guarantee you that you will start pushing-aside faithful old Excel. Speaking of which,
Mathcad allows you to embed Excel tables inside its documents, so that users can easily
enter data into variables, or display calculation results in tabular form; Mathcad can even
read/write data from/to Excel files in the native binary .XLS format. What could make the
transition from one tool to the other easier?
This article provides a brief tutorial on Mathcads Symbolics and Optimization features,
and then illustrates how to use both of them to perform a circuit design. For those of you
who might already use Mathcad some of this might be review; perhaps, though, you will
pick-up some new tidbits of knowledge. It is hoped that the reader will learn a faster and
easier (and less error-prone) method of deriving circuit equations and transfer functions, as
well as how to automatically determine optimal component values based on end-product
requirements. It is also hoped that this article will expose the reader to a new analytical
tool (Mathcad) that he wasnt already aware of, and possibly open the door to many new
opportunities for performing better design and analysis tasks in a faster and more efficient
manner.

James C. (Jim) Bach

Page 2 of 2

February 3, 2005

Using Mathcad To Derive Circuit Equations and Optimize Circuit Behavior

Symbolic Math In Mathcad


For the EE design engineer, one of Mathcads strong points is its Symbolic Math processor.
In its simplest form this feature allows you to convert or mutate an equation into another
form. For instance, say we take our familiar impedance of 2 paralleled resistors equation
and wish to rearrange the equation so we can solve for one of the resistor values (R1) if we
know the combined resistance (ZParallel) and one of the two resistor values (R2). Using the
two simplest forms of symbolic processing in Mathcad, which I call Static Symbolics and
Live Symbolics:
Static: Using the SymbolicsVariableSolve menu operation, with R1 selected:
ZParallel =

has solution(s)

1
1
+
R1 R2

ZParallel

R2
ZParallel + R2

Live: Using the Solve


operator
from the Symbolics Toolbar:
1
Cap
ZParallel =

1
1
1
+
R1 R2

solve , R1 ZParallel

R2
ZParallel R2

Or how about rearranging the familiar resonant frequency of an LC Tank circuit? Again,
using the simplest forms of symbolic processing in Mathcad we can easily rearrange the
equation to calculate the capacitance (C Cap) needed to make a given inductor (LCoil)
resonate at a certain frequency (F Res):
Static:
selected:

Using
FRes =

the

SymbolicsVariableSolve

has solution(s)

menu

operation,

with

C Cap

1
2

2 LCoil CCap

4 LCoil FRes

Live: Using the Solve operator from the Symbolics Toolbar:


FRes =

1
2 LCoil CCap

solve , CCap

1
2

4 LCoil FRes

How does one utilize these two simple forms of symbolics? Well, the Static symbolics
method uses the various operators located under the top-line pull-down menu titled
Symbolics. In general, you type-in an equation, select a term (variable) in the equation,
and then select the symbolic operation you wish to perform; some operations will operate
on the entire equation regardless of what is selected. For instance, creating the R1
derivation takes-place as:
Enter the equation:
ZParallel =

1
1
1
+
R1 R2

Select the R1 term (with blue L-shaped underline):

James C. (Jim) Bach

Page 3 of 3

February 3, 2005

Using Mathcad To Derive Circuit Equations and Optimize Circuit Behavior


Invoke the Solve operator from the top-line menu:

The result is inserted into the document:


has solution(s)

ZParallel

R2
ZParallel + R2

The Live symbolics method uses the various operators on the Symbolic toolbar. In
general, you place your insertion mark where (in your document) you want your symbolic
evaluation to take place, select the symbolic operator from the toolbar, and then fill-in the
placeholders (little black squares) with your equation and the term to be solved for. For
instance, creating the C cap derivation takes-place as:
Click the Solve icon on the toolbar:

The Solve operation is dropped-in, with placeholders ready and waiting:


solve ,

User fills-in the left-most placeholder (small square) with the original equation:
F Res =

1
2 L Coil C Cap

solve ,

User fills-in the right-most placeholder with the variable (term) to be solved-for:
F Res =

James C. (Jim) Bach

1
2 L Coil C Cap

solve , C Cap

Page 4 of 4

February 3, 2005

Using Mathcad To Derive Circuit Equations and Optimize Circuit Behavior


Mathcad automatically calculates the answer (right-side of symbol):
FRes =

1
2 LCoil CCap

solve , CCap

1
2

4 LCoil FRes

The more powerful Chained Live symbolic processor allows you to combine a set of
equations, solving for multiple variables contained in the equations; remember the not-sofun task of solving N equations with N unknowns? In addition, that symbolic result can be
assigned to a numeric function that can be used in subsequent design/analysis equations.
For example, you can write nodal equations for a circuit and have Mathcad derive symbolic
(algebraic) equations that represent the voltages at the nodes (or currents through the
elements), and then assign the equation representing the OUT node to a function that can
be used to plot the transfer function. A quick example is shown below.
Given a simple inverting amplifier using an op-amp:

Derive the nodal equations, presuming input currents are zero (ideal op-amp), but provide a
term for the input offset voltage (Vio):
Eqn1 := Vio

Vhi Vlo

Eqn2a := IHi

Eqn3a := IHi

Eqn3b := ILo

Input offset voltage (V across + and - pins)

Eqn2b := ILo

Vhi
Rbal
Vlo IN
Rin

Input currents (are zero for idealistic case)

Current through Rbal (from "+" pin)

Vlo OUT
Rfb

Current at Vlo node (from "-' pin)

Use the symbolics processor to calculate the equation for the output voltage (and a bunch of
intermediary terms that we dont care about):

OUT
Eqn1

Eqn2a
Vlo

( Rfb Vio + Rfb IN + Rin Vio) Vio 0 0 0


SysRes := Eqn2b solve , Vhi

Rin

Eqn3a
ILo
Eqn3b
I

Hi
Strip-out the one answer we care about (OUT), and collect on the Vio term to make it
neater looking:

0
( Rfb + Rin)
IN
Out := SysRes 0 collect , Vio
Vio Rfb
Rin
Rin

James C. (Jim) Bach

Page 5 of 5

February 3, 2005

Using Mathcad To Derive Circuit Equations and Optimize Circuit Behavior


Then assign the symbolic result to a function (VOut ) that can be evaluated numerically:
VOut ( IN , Rfb , Rin , Vio) := Out

( Rfb + Rin)
Rin

Vio Rfb

IN
Rin

The above is an example of what I call Chained Live Symbolics, because the equations are
tied-together or chained by virtue of assigning the symbolic equations (right-side of :=
operator) to variables (left-side of := operator), which are then used in subsequent
symbolic operations. For instance, the 5 basic circuit equations, assigned into variables
Eqn1, Eqn2a, Eqn2b, Eqn3a, and Eqn3b, are chained to (used by) the solve operation,
whose results are assigned into the variable SysRes (System Results), which is then
chained to (used by) the equation that pulls-out the OUT answer (and collects terms), and
assigns it to the variable Out, which is chained to (used by) the function declaration for
VOut . Notice that our final result does not contain a term for Rbal; because we had defined
our op-amps input currents to be zero, the voltage (Vhi) induced into Rbal from the +
inputs current is zero, thus the Rbal term drops-out of our transfer function.
The beauty of this system is that if you find an error in one of your fundamental equations,
or wish to change one or more equations because you made a topological change in the
circuit, all of the changes automatically ripple-down to the bottom-line answer.
For
instance, the example above ignored the op-amps input offset and bias currents. If we
later decided to add terms (Io and Ib, respectively) to account for these parasitics, all we
would need to do is modify equations Eqn2a and Eqn2b as follows:
Eqn2a := IHi

Ib +

1
2

Io

Eqn2b := ILo

Ib

1
2

Io

And then the new System Results pops-out automatically (and in a much more long and
messy form):
OUT
Eqn1

Eqn2a
Vlo
1 2 Rin Rfb Ib Rin RfbIo + 2 Rfb Vio 2RfbRbal Ib RfbRbal Io + 2 Rfb IN + 2 Rin Vio 2Rin Rbal Ib Rin Rbal Io Vio + Rbal Ib + 1 Rbal Io Rbal Ib + 1 Rbal Io Ib 1 Io Ib + 1 Io
SysRes:= Eqn2b solve , Vhi

Rin
2
2
2
2
2

Eqn3a
I Lo
Eqn3b
I
Hi

After collecting on terms Vio, Ib, and Io the VOut function declaration changes to:
V Out( IN , Rfb , Rin , Rbal , Vio , Ib , Io) =
: Out

1 2 Rfb 2 Rin

Vio +
2
Rin

1 2 Rin Rfb + 2 Rin Rbal + 2 Rfb Rbal

Ib +
2
Rin
1 Rin Rfb + Rin Rbal + Rfb Rbal

Io
2
Rin
Rfb

IN
Rin

All we had to do is tidy-up VOut s argument list by adding-in the two new terms, Io and Ib.
It was optional to collect on the newly-added terms in order to make the equation look
prettier, however, notice how it lends itself to allowing the designer to directly observe the
contributions of the op-amps parasitics in the overall transfer function. In fact, you can
James C. (Jim) Bach

Page 6 of 6

February 3, 2005

Using Mathcad To Derive Circuit Equations and Optimize Circuit Behavior


even use Mathcads derivative operator to directly find the individual contributions of
these error terms to the transfer function:
d
dVio
d
dIb
d
dIo

Out

1 2 Rfb + 2 Rin

2
Rin

Out

1 2 Rin Rfb 2 Rin Rbal 2 Rfb Rbal

2
Rin

Out

1 Rin Rfb Rin Rbal Rfb Rbal

2
Rin

And, of course, the gain of the circuit is simply the derivative of the transfer function with
respect to the input signal IN:
d
dIN

Out

Rfb
Rin

Notice that our final result (OUT) now contains terms involving Rbal; because we have
included terms (Ib and Io) which generate current (IHi) on the + input, the voltage (Vhi)
induced across Rbal is no longer zero, thus Rbal is needed in the transfer function.
So now we have a function that can calculate the output voltage (OUT) given all of the
circuit values and device parasitics. Using this function we can (amongst other things) plot
a graph showing the circuits transfer function. Say weve designed this circuit to have a
gain of 10, and we are using an op-amp with fairly large input currents:
Rfb := 10k

Rin := 1k

Rbal := 1k

V io := 5mV

Ib := 10A

Io := 0.1A

Transfer Function (Av=-10)

0.1

Output (Volts)

0.2

0.3

0.4

0.5

0.6

0.01

0.02
0.03
Input (Volts)

0.04

Ib positive
Ib negative
Ideal Behavior

James C. (Jim) Bach

Page 7 of 7

February 3, 2005

Using Mathcad To Derive Circuit Equations and Optimize Circuit Behavior

Optimization in Mathcad
Another one of Mathcad's strong points is that it has built-in "optimization" capability, which
can be used to adjust any number of system variables until a set of "goal" conditions have
been met, or met with minimal error. This allows the engineer to make some initial guesses
for component values, and then have Mathcad determine what the optimal values are in
order to meet the design constraints (say, to reduce the error of a current-sense amplifier,
obtain a desired frequency response in a multi-stage filter network, etc.). The designer can
constrain component values to particular ranges, to prevent the optimizer from finding
problematically too small or too large of values. The designer can also create his own
Error function, which can be used to control the weighting of trade-offs in the optimization
process.
First of all, you must know if the system you are trying to optimize has achievable goals
(constraints), or if you have some mutually exclusive goals that will make an exact solution
impossible. Mathcad provides two constructs for optimization:
GivenFind

Finds an exact solution (makes all constraints come true)

GivenMinerr

Finds the solution with minimal error for all constraints

If you know your system DOES have an exact solution, then use the GivenFind
construct.
Otherwise use the GivenMinerr construct, which will provide a solution
whether or not an exact solution exists. The following examples demonstrate how each of
these constructs work.
The first example of optimization illustrates use of the GivenFind construct to determine
the optimal values of two resistors in a divider such that both the target output voltage and
target Thevenin resistance are obtained. First we need to create functions for calculating
the output voltage (VOut ) and the Thevenin resistance (ZOut ), specify the supply voltage
(Vs), and establish the constraints on the system (GoalV and GoalZ):
VOut( VS , R1, R2) := V S

Z Out( R1 , R2) :=

R2
R1 + R2

1
1
1
+
R1
R2

Output Voltage
Functions describing
circuit behavior to be
optimized
Output Impedance

VSupply := 5V

Power Supply Voltage

GoalV := 1.5V

Voltage
Optimization Goals
Impedance

GoalZ := 10k

Initial guesses for resistors


Next we make our guesses for the values of R1 and R2:
R1 := 2GoalZ

R2 := R1

James C. (Jim) Bach

Page 8 of 8

February 3, 2005

Using Mathcad To Derive Circuit Equations and Optimize Circuit Behavior


Then we let Mathcad perform the optimization, telling it to force VOut to match GoalV and to
force ZOut to match GoalZ:
Given
V Out( V Supply , R1 , R2) = GoalV

R1_Found

:=
R2_Found

ZOut( R1 , R2) = GoalZ

Find( R1 , R2)

It is important to note that the = signs used to define the constraints of the GivenFind
block are the Boolean Equals sign, obtained by clicking the icon on the Boolean Toolbar
(or typing <ctrl>=); this is NOT the standard give me the answer equals sign obtained by
typing = on the keyboard.
The results that Mathcad yields are:
R1_Found = 33.3333 k
R2_Found = 14.2857 k

Calling our original functions with the newly found resistor values we can check compliance
with the initial design constraints:
V Out( V Supply , R1_Found , R2_Found) = 1.5000
ZOut( R1_Found , R2_Found) = 10.0000 k

Indeed, we see that our goals have been met.


We can generalize this optimization so that no matter how many different VOut and ZOut
combinations we need to create, we dont have to recreate the GivenFind block multiple
times. In this example well always default the resistors to 10k, and well replace the leftside of the := Find equation with the name of a function and a list of input arguments:
R1 := 10k

R2 := 10k

Given
V Out( V S , R1 , R2) = T V

ZOut( R1 , R2) = T Z

Optimize_Rs1 ( V S , T V , T Z ) := Find( R1 , R2)

What this allows us to do is pass-in any set of target VOut and ZOut , and get-back a set of R1
and R2. For example:
Optimize_Rs1 ( 5V , 3V , 1k ) =

1666.6655

2499.9986

Optimize_Rs1 ( 12V , 3V , 1k ) =

4000.0009

1333.3333

Optimize_Rs1 ( 12V , 5V , 10k ) =

24.0000

k
17.1429

The second example of optimization illustrates use of the GivenMinerr construct to


determine the optimal values of three resistors in a divider such that both of the target
output voltages are obtained. The list of constraints includes an inane requirement for the
top and bottom resistor values to be the same value; this precludes the GivenFind
construct from succeeding.
James C. (Jim) Bach

Page 9 of 9

February 3, 2005

Using Mathcad To Derive Circuit Equations and Optimize Circuit Behavior


First we need to create functions for calculating the output voltages (VOut1 and VOut2),
specify the supply voltage (Vs), and establish the constraints on the system (GoalV1 and
GoalV2):
V Out1(V S , R1, R2 , R3) := V S

V Out2(V S , R1, R2 , R3) := V S

V Supply := 5V

GoalV 1 := 4V
GoalV 2 := 1.5V

R2 + R3

Top "Tap"

R1 + R2 + R3

Functions describing
circuit behavior to be
optimized

R3
R1 + R2 + R3

Bottom "Tap"

Power Supply Voltage

Top "Tap"
Optimization Goals
Bottm "Tap"

Next we make our guesses for the values of R1, R2 and R3:
R1 := 1k

R2 := 1k

R3 := 1k

Then we let Mathcad perform the optimization, telling it to force VOut1 to match GoalV1, to
force VOut2 to match GoalV2, and to force R1 to match R3 (i.e. same -valued resistors):
Given
V Out1( V Supply , R1 , R2 , R3) = GoalV1
V Out2( V Supply , R1 , R2 , R3) = GoalV2
R1 = R3

R1_Found

R2_Found :=
R

3_Found

Minerr( R1 , R2 , R3)

Again, please note that the three constraints in the GivenMinerr block utilize the
Boolean Equals sign.
The results that Mathcad yields shows us that it complied with the constraint of R1 = R3 :
R1_Found = 0.2500 k

R2_Found = 0.5000 k

R3_Found = 0.2500 k

However, calling our original functions with the newly found resistor values shows us that
the voltage constraints were not quite met:
V Out1( V Supply , R1_Found , R2_Found , R3_Found) = 3.7500 V GoalV1 = 4.0000 V
V Out2( V Supply , R1_Found , R2_Found , R3_Found) = 1.2500 V GoalV2 = 1.5000 V

James C. (Jim) Bach

Page 10 of 10

February 3, 2005

Using Mathcad To Derive Circuit Equations and Optimize Circuit Behavior


Because the desired voltages across R1 and R3 are different (1V vs- 1.5V) there is NO way
same-valued resistors can be used and provide the desired output voltages. Notice that
both of the output voltages missed their targets by the same amount; VOut1 is 0.25V lower
than desired and VOut2 is 0.25V higher than desired. The optimizer did its best with
conflicting constraints; it split the difference.
The key to getting even this close to an optimized solution is the use of the
GivenMinerr construct. If we attempted this optimization using the GivenFind
construct, wed find-out the hard way that we had an impossible situation:
Given
V Out1( V Supply , R1 , R2 , R3) = GoalV1
V Out2( V Supply , R1 , R2 , R3) = GoalV2
R1 = R3

R1_Found

R2_Found :=
R

3_Found

Find( R1 , R2 , R3)

Whenever you attempt to perform an optimization using the GivenFind construct, and
you get this sort of error, try changing Find to Minerr and see what results you get;
perhaps there really was NOT an exact solution. In general the GivenMinerr construct
is the sure-bet; it will give an answer whether or not there is an exact solution.
Notice in the example below that if we unshackle the optimizer (remove R1=R3 constraint),
then the GivenFind has no problem in finding an exact solution:
R1_Found = 0.5950 k

R2_Found = 1.4876 k

R3_Found = 0.8926 k

V Out1( V Supply , R1_Found , R2_Found , R3_Found) = 4.0000 V GoalV1 = 4.0000 V


V Out2( V Supply , R1_Found , R2_Found , R3_Found) = 1.5000 V GoalV2 = 1.5000 V

James C. (Jim) Bach

Page 11 of 11

February 3, 2005

Using Mathcad To Derive Circuit Equations and Optimize Circuit Behavior

Putting it all together


Now that we know how to use the Symbolics processor to derive circuit equations and how
to use the Optimizer to obtain optimal component values to meet a set of goals (targets),
lets put the two techniques together to synthesize a useful design. The example provided
here is a real-world circuit, a microphone preamplifier with bandpass filtering. The circuit
topology is:
Cfb

Low-Pass
Filtering

IN
Input from
Lo-Z Source

High-Pass
Filtering

Cin

Rin

Rfb

Vin

Vcc

Vcc

Optional Low-Pass Filter


(extra hi-freqy roll-off)

vcc

Rbias1
U1

Vbias

Rout

Output to
Hi-Z Source

OUT

Vout
vee

Cout
Rbias2
Op-Amp Parameters:
Vio = Input Offset Voltage
Iio = Input Offset Current
Iib = Input Bias Current

The heart of this circuit topology is op-amp (U1), arranged in an inverting amplifier
configuration. This analysis shall take into account the input parasitics of the op-amp,
namely the input offset voltage (Vio), the input offset current (Iio), and the input bias
current (Iib). Because this design uses a single-supply op-amp, the non-inverting (+)
input of the op-amp is held at a pseudo-ground voltage of mid-supply, created by a simple
resistor divider network consisting of Rbias1 and Rbias2. Part of the optimization process
will be to choose divider resistor values that minimize output offset (i.e. shift from the
desired mid-supply value).
The frequency-selectivity of the circuit is controlled by the RC elements in both the input
and feedback legs. Input resistor Rin and feedback capacitor Cfb form a low-pass "Pole",
while input capacitor Cin and feedback resistor Rfb form a high-pass "Zero". Additional
low-pass filtering is provided by optional output elements Rout and Cout, forming another
"Pole"; these elements can be eliminated if the required transfer function does not need
them.
The first step in our design process is to derive the transfer function for this circuit. To do
this we will make use of Mathcads Symbolics processor, just as we did with the simpler
inverting amp lifier. We begin by writing equations for each of the circuits reactive elements
(capacitors):
Eqn 1a := XCin =

1
2i Frq Cin
1
2i Frq Cfb

Eqn 1b := XCfb =
Eqn 1c := XCout =

1
2i Frq Cout

Then we calculate the combined (complex) impedances of series/parallel RC branches:


Eqn2a := XIN = XCin + Rin
Eqn2b := XFB =

James C. (Jim) Bach

1
1
1
+
XCfb
Rfb
Page 12 of 12

February 3, 2005

Using Mathcad To Derive Circuit Equations and Optimize Circuit Behavior


Then we write equations that describe the op-amps input characteristic s:
Eqn 3a =
: Vio = Vbias Vin
1
Iio
2

Eqn 3b =
: IInv = Iib +

Eqn 3c =
: INonInv = Iib

Iio

Then we write our nodal equations:


IN Vin

Eqn 4a =
:

XIN

Eqn 4b =
:

Vin Vout

Vin Vout
XFB
Rout

Eqn 4d =
:

Vcc Vbias
Rbias1

Vout OUT

Vout OUT

Eqn 4c =
:

+ IInv

XFB

Rout
=

+ IOut

OUT
XCout
Vbias
Rbias2

+ INonInv

Lastly, we combine all of them in a Solve block, and let the Symbolics processor grindout an answer:

SysRes :=

Eqn1a
Eqn1b

Eqn1c
Eqn2a

Eqn2b
Eqn3a

Eqn3b
Eqn3c

Eqn4a
Eqn4b

Eqn4c
Eqn4d

OUT

Vout
Vbias
Vin

XIN
XFB

solve ,
XCin
X

Cfb
XCout
I

Out
IInv

INonInv

Because of the large number of terms and intermediary values, the results (contained in
SysRes) are too large to display directly. As you can see above, Mathcad doesnt even try
to display the results; however, the results ARE in the SysRes variable. In fact, even
stripping-apart SysRes to obtain the single result we care about (OUT), we get a long, ugly
mess of an equation that we cant fully reproduce here (but is visible in Mathcad by making
use of the horizontal scroll bar); the beginning of it looks like:

OUT := SysRes

)1

1 2 i Rbias2 Vcc + i Rfb Rbias2 Iio 4 i Frq Cin Rin Rbias1 Rbias2 Iio Rfb Cfb + 8

But, it doesnt really matter that we cannot easily READ the equation, since we are going to
assign it to functions that we can call numerically. The two functions we wish to create are
GAIN and OFFSET, as those are the two characteristics we will later optimize the circuit
around. We start-out by deriving equations for GAIN and OFFSET based on the full
James C. (Jim) Bach

Page 13 of 13

February 3, 2005

Using Mathcad To Derive Circuit Equations and Optimize Circuit Behavior


OUT equation; we do this by using judicious substitutions as shown below (again, the final
equations are too long to see here in their entirety, but are visible within Mathcad):
OFFSET := OUT

GAIN :=

OUT
IN

substitute , IN = 0V , Frq = 0Hz


Rbias2
1 2 i Rbias2 2 i Rbias1
i
Vcc
Vio +
collect , Vcc , Vio , Iib , Iio , Rfb
i Rbias1 i Rbias2
2
i Rbias1 i Rbias2

1 2
2

substitute , Vcc = 0V , Vio = 0V , Iio = 0A , Iib = 0A 1

( 4 Rfb Rbias2 + 4 Rfb Rbias1 ) Cin


collect , IN , Frq , , Cout , Cfb , Cin
2
( 8 Rout Rin

Notice that for deriving OFFSET we set the input signal (IN) to zero and we set the
frequency (Frq) to zero; this is a DC calculation with the input grounded. Similarly, when
deriving GAIN we set the power supply (Vcc) to zero, as we also do with all of the opamp input characteristics (Vio, Iio, and Iib); this is an AC calculation and DC terms need to
drop out. In both cases terms have been collected in order to obtain more readable
results; this allows observation of the contributions of key parameters of the calculation
(e.g. how Vcc and Vio affect the offset voltage of the circuit). Oddly enough, Mathcad
forces the derivation of GAIN to have IN as one of the collection terms even though
IN never actually appears in the resultant equation; for whatever reason, without that
collection term you will not receive a result.
Now that these two equations have been generated, we can assign them to functions that
can be called numerically (again, you cannot see the entire equation here):
Offset( Rfb , Rbias1, Rbias2 , Vcc , Vio , Iio , Iib ) := OFFSET i

Rbias2
1 2 i Rbias2 2 iRbias1
Vcc
Vio +
i Rbias1 i Rbias2
2
i Rbias1 i Rbias2

Gain( Frq , Rin , Cin , Rfb , Cfb, Rout , Cout , Rbias1 , Rbias2) := GAIN

1
( 4 Rfb Rbias2 + 4 Rfb Rbias1) Cin
2
( 8 Rout Rin

GaindB ( Frq , Rin , Cin , Rfb, Cfb, Rout , Cout , Rbias1 , Rbias2) := 20log( Gain( Frq , Rin , Cin , Rfb, Cfb, Rout , Cout , Rbias1, Rbias2

If we plug-in some initial values for the components (setting all resistors to 10k and all
capacitors to 0.01F), we can quickly determine the output offset voltage and plot the Bode
response of the circuit:
Rin := 10k

Rfb := Rin

Rout := Rin

Cin := 0.01F

Cfb := Cin

Cout := Cin

Vcc := 5V

Vio := 10mV

Iio := 1A

Rbias1 := Rin

Rbias2 := Rin

Iib := 0.1A

Offset ( Rfb , Rbias1 , Rbias2 , Vcc , Vio , Iio , Iib ) = 2.4980 V

James C. (Jim) Bach

Page 14 of 14

February 3, 2005

Using Mathcad To Derive Circuit Equations and Optimize Circuit Behavior


Frequency Response of Filter

10

20

Gain (dB)

30

40

50

60

70

80
0.01

0.1

1
Frequency (kHz)

10

100

Next, we wish to optimize the circuit so that it performs to some desired response. This
desired response (aka goal function) might be taken from a customer requirements
document, or from a specification created by the system designer. For this example, we are
going to fill a pair of vectors with Frequency/dB target points that we would like our filters
response to pass through. We can make use of an embedded Excel table to make the dataentry easy and professional looking:

Target Freqs

:=
Frequency
Target dBs
(Hz)

Gain
(dB)

10
32
100
320
1000
3300
10000
33000

-30
-10
0
0
0
0
-20
-50

As can be deduced by inspecting the table, we wish our filter to be flat from 100 to 3300
Hz, and have approximately 25dB/decade below and 45db/decade above that range.
Obviously, real world filters have slopes of 20 and 40 dB/decade, so, our optimized filter will
be close, but no gold cigar. There is NO way to exactly provide this shape, but, perhaps
we can create a filter that is good enough.
Before we can perform our optimization, we need to define an Error function, which the
optimizer will attempt to force to zero. For this example we will use a simple Sum of the
squared errors algorithm. This algorithm simplistically sums the square of the errors in
gain (dBs) at each of the target frequencies. The optimizer will call this function for each
permutation of component values that is attempted; when the optimizer finds a combination
that yields a minimal output from this function, it terminates and returns the optimal
values.
(

last TargetFreqs

E R R O RdBs( Rin, Cin, Rfb, Cfb, Rout , Cout, Rbias1, Rbias2) :=

= ORIGIN

(GaindB ( TargetFreqs n , Rin, Cin, Rfb, Cfb, Rout , Cout, Rbias1, Rbias2) TargetdBs n )2

Finally we use the GivenMinerr construct to optimize the circuit element values:
James C. (Jim) Bach

Page 15 of 15

February 3, 2005

Using Mathcad To Derive Circuit Equations and Optimize Circuit Behavior


Given
ERRORdBs( Rin , Cin , Rfb , Cfb , Rout , Cout , Rbias1 , Rbias2 ) = 0
Offset ( Rfb , Rbias1 , Rbias2 , Vcc , Vio , Iio , Iib ) = 2.5000V
1k Rin 100k

1k Rfb 100k

1k Rout 100k

100pF Cin 1F

100pF Cfb 1F

100pF Cout 1F

Rin
Cin

Rfb
Cfb

:=
Rout
Cout
Rbias1

Rbias2

Minerr( Rin , Cin , Rfb , Cfb , Rout , Cout , Rbias1 , Rbias2 )

James C. (Jim) Bach

Page 16 of 16

February 3, 2005

Using Mathcad To Derive Circuit Equations and Optimize Circuit Behavior


The component values that were chosen are:
Rbias1 = 9.9029k

Rbias2 = 9.9018 k

Rin = 8.6275k

Rfb = 13.5240 k

Rout = 26.9265 k

Cfb = 0.0054 F

Cout = 0.0027 F

Cfb = 5358.4219 pF

Cout = 2692.6482 pF

Cin = 0.0780F
4

Cin = 7.8032 10 pF

V Offset = 2.5000V
Gain 1kHz = 2.0310

We can plot our initial response and our optimized response as:
Optimized Design:

Initial Guess:
Frequency Response of Filter (Original)

20

20

Gain (dB)

Gain (dB)

Frequency Response of Filter (Optimized)

40

60
0.01

40

0.1

1
Frequency (kHz)

10

100

60
0.01

Actual Response
Target Response

0.1

1
Frequency (kHz)

10

100

Actual Response
Target Response

Note that we needed to specify several points within the passband in order to force
Mathcads optimizer to keep the mid-band gain down. Without these ext ra points the
skirts become better matched and the mid-band gain became too large, as shown below:
Frequency Response of Filter (Optimized)

Gain (dB)

20

40

60
0.01

0.1

1
Frequency (kHz)

10

100

Actual Response
Target Response

In fact, with two minor modifications we can provide additional constraints to make some
target points more important than others:
- Add a Weighting Factor column to the data-entry table (see following examples)
- Modify the Error function to multiply the squared error by the weight
(

last Target Freqs

ERRORdBs( Rin, Cin, Rfb, Cfb, Rout , Cout , Rbias1, Rbias2) :=

James C. (Jim) Bach

= ORIGIN

Weightsn (GaindB (TargetFreqs , Rin, Cin, Rfb, Cfb, Rout , Cout , Rbias1, Rbias2) TargetdBs )2
n
n

Page 17 of 17

February 3, 2005

Using Mathcad To Derive Circuit Equations and Optimize Circuit Behavior


Specifying that the two mid-band targets are 10X more important than the others yields the
following optimization results:
Frequency Response of Filter (Optimized)

:=

Frequency
(Hz)
10
32
100
320
1000
3300
10000
33000

Gain
(dB)
-30
-10
0
0
0
0
-20
-50

Weight
1
1
1
10
10
1
1
1

Gain (dB)

Target Freqs
Target dBs
Weights

20

40

60
0.01

0.1

1
Frequency (kHz)

10

100

Actual Response
Target Response

Specifying that the two corner frequency targets are 10X more important than the others
yields the following optimization results:
Frequency Response of Filter (Optimized)

:=

Frequency
(Hz)
10
32
100
320
1000
3300
10000
33000

Gain
(dB)
-30
-10
0
0
0
0
-20
-50

Weight
1
1
10
1
1
10
1
1

Gain (dB)

Target Freqs
Target dBs
Weights

20

40

60
0.01

0.1

1
Frequency (kHz)

10

100

Actual Response
Target Response

Conclusion
Mathcad is a very fast and powerful mathematical analysis package that can be used by
electrical designers to perform a variety of design and analysis tasks ranging from simple to
complex. The built-in symbolic processor allows us to easily construct complex transfer
functions from a simple collection of nodal equations. The built-in optimizer allows us to
easily arrive at optimal component values such that the circuit meets a given set of
constraints (targets). With these two features alone (and Mathcad has many more) EEs can
perform better, more accurate circuit designs than pen-and-paper me thods; in many
instances better, more efficient circuit designs than using circuit simulators (like SPICE).

James C. (Jim) Bach

Page 18 of 18

February 3, 2005

Using Mathcad To Derive Circuit Equations and Optimize Circuit Behavior

Biography:

Jim Bach received a BSEE (with a minor in Computer Science) from Marquette University in
1982. He began working for Delphi Electronic & Safety (DES) in 1986, when it was called
Delco Electronics. In his career at DES he has been a Systems Engineer, an Advanced
Development Engineer, an EE Simulation and Modeling Engineer, and now EE Analysis
Engineer and Mathcad Instructor.
His primary background is in Powertrain Electronics
(engine and/or transmission control modules), although hes assisted engineers in other
product lines. Primarily analog in nature, he enjoys working with circuits that interface
with sensors or control solenoids, as well as conditioning/filtering signals. Over the past few
years Jim has become DESs resident expert in utilizing Mathcad for performing design and
analysis tasks, to the point of having created an internal 6-day, 8-session Mathcad for
Engineers training class and hosting periodic Brown-Bag Lunch seminars. Jim enjoys
circuit design and analysis, and the analytical tool known as Mathcad; putting the two of
them together and teaching about it is both challenging and rewarding.

Abstract
One of Mathcads strong points is that it has a built-in symbolic processor, which can be
used to combine a collection of nodal (circuit) equations and synthesize a set of equations
(e.g. transfer functions) for the circuit. This provides a simple and automated method of
solving for N unknowns from N equations. The article explains how to use this feature to
create transfer functions for a simple circuit; later the article illustrates how to use this
feature to create the transfer function of a more complicated circuit (bandpass filter) and
create a numeric function that can be used for design optimization and analysis.
Another of Mathcad's strong points is that it has a built-in "optimizer" capability, which can
be used to adjust any number of system variables until a set of "goal" conditions have been
met, or met with minimal error. This allows the engineer to make some initial guesses for
component values, and then have Mathcad figure-out what the optimal values would be
(say, to obtain a desired frequency response in a multi-stage filter network). The designer
can constrain component values to particular ranges, to prevent the optimizer from finding
problematically too small or too large of values. The designer can also create his own
Error function, which can be used to control the weighting of trade-offs in the optimization
process. This article explains how to use the optimizer to determine component values of a
simple circuit in order to make it meet some criteria; later the article illustrates how to use
this feature to optimize a more complicated circuit (bandpass filter) so that it approximates
a desired Bode response.
James C. (Jim) Bach

Page 19 of 19

February 3, 2005

You might also like