Message Boards Message Boards

1
|
12002 Views
|
5 Replies
|
8 Total Likes
View groups...
Share
Share this post:

A question about using Wolfram Mathematica with Circuit Analysis

I am trying to use Wolfram Mathematica to solve for the thevenin impedance of my circuit. Here is my code:

Z1 = 1/(IwC1)

Z2 = 1/(IwC2)

Z3 = 1/(IwC3)

Z4 = ((R1 + Z1)* Z2/(R1 + Z1 + Z2) )

Z5 = Z4 + R2

Zth = Simplify[Z5*Z3/(Z5 + Z3)]

amp = Abs[Zth]

phase = Arg[Zth]
POSTED BY: Ammar Hazim
5 Replies

Hi, You can perform quite general nodal analysis on arbitrary networks of circuit components using gEDAmath, which is a Mathematica notebook that (optionally) couples to the gEDA open-source Electronic Design Automation toolkit:

For small networks, no need for the gEDA overhead; defining it within the notebook is sufficient.

POSTED BY: Frank Iannarilli
Posted 11 years ago

Hi Ammar,

Mathematica is a great tool for EE. There is the approach you are taking, but there is also the possibility of using the constructs available in Mathematica's signal processing and control systems formulations. These allow working in the s-domain, and with abstract transfer function models. (For example, compensation of a feedback amplifier for stability and optimal frequency and transient response is a control system problem.)

In working in the frequency domain with complex impedances, one aspect of Mathematica gets in the way. We generally want it assumed that all variables are real -- that complex numbers are always written with I as explicit. Many of Mathematica's function assume any variable can be anything. (Although some assume real, like ComplexExpand, and others can have the domain specified.)

In the code below, I follow your approach, (and that of Dr Reiss). I use Mathematica's functions to create complex impedances from components values, and I also use functions to form parallel and series circuit impedances. I then take these and apply the method to your circuit, which I reverse engineered from the math. (I hope correctly.)

You can follow this in the code below, but I also attach the notebook, since it has cell separation and can be immediately executed. I hope you enjoy using Mathematica on circuits. I have worked as an engineer for may years and am always dismayed to see engineers using "that other tool." It's like watching someone using a typewriter. (See http://en.wikipedia.org/wiki/Typewriter)

Kind regards,

David

(* impedance of a capacitor *)
zc[c_] := 1/(I w c);

(* impedance of an inductor *)
zl[l_] := 2 I w l;

(* impedance of parallel components *)
par[za_, zb_] := za zb/(za + zb)

(* and series components *)
ser[za_, zb_] := za + zb

(* the circuit impedance *)
zth = par[z3,
  ser[r2,
   par[ser[r1, z1], z2]
   ]
  ]

(* with capacitar impedances *)
zth1 = zth /. {z1 -> zc[c1], z2 -> zc[c2], z3 -> zc[c3]}

(* a simpler view, but still not as a + b I *)
zth2 = ComplexExpand[
   zth /. {z1 -> zc[c1], z2 -> zc[c2], z3 -> zc[c3]}] // FullSimplify

(* as a + b I *)
zth3 = ComplexExpand[zth2, TargetFunctions -> {Re, Im}]

(* We need some specialized functions which assume all variables are \
real *)

conj[z_] := z /. I -> -I

re[z_] := (z + conj[z])/2 // FullSimplify

im[z_] := (z - conj[z])/2/I // FullSimplify

arg[z_] := ArcTan[re[z], im[z]] // FullSimplify

abs[z_] := Sqrt[re[z]^2 + im[z]^2] // FullSimplify

(* the real part *)
re[zth3]

(* the imaginary part *)
im[zth3]

(* modulus *)
abs[zth3]

(* argument *)
arg[zth3]

pf = 1. 10^-12; meg = 1. 10^6;

values = {r1 -> .1 meg, r2 -> 0.1 meg, c1 -> 100 pf, c2 -> 50 pf, 
   c3 -> 50 pf};

absz = abs[zth3] /. values

Plot[absz, {w, 1, 1000000}]
Attachments:
POSTED BY: David Keith

That was useful! Thanks!

POSTED BY: Ammar Hazim

Do your Abs and Arg functions not return an output or do they not return the result you are expecting?

A couple of pointers. It may be a result of the formatting of your post (it is best to use the code formatting tools above the posting area to show code) or you may be actually missing some spaces. So the following will be interpreted by Mathematica as a single parameter, not a product of I, w, and C3:

IwC3

This should be written with spaces to imply multiplication as

I w C3

With that said, here are your input expressions:

Z1 = 1/(I w C1); 

Z2 = 1/(I w C2) ;

Z3 = 1/(I w C3) ;

Z4 = ((R1 + Z1)* Z2/(R1 + Z1 + Z2) ) ;

Z5 = Z4 + R2 ;

Zth = Simplify[Z5*Z3/(Z5 + Z3)] ;

Where I put semicolons at the end of each to suppress the output. To get a result from evaluating Abs[Zth] that actually gives you the absolute value, you need to process the result along with telling Mathematica that w, C1, C2, C3, R1, and R2 are real. You can do this with ComplexExpand which assumes that all variables are real unless you tell it otherwise:

ComplexExpand[Abs[Zth]]

Which gives

$ \frac{\sqrt{\left(1-\text{C1} \text{C2} \text{R1} \text{R2} w^2\right)^2+(\text{C1} w (-\text{R1}-\text{R2})-\text{C2} \text{R2} w)^2}}{\sqrt{w^2} \sqrt{\left(\text{C1} \left(\text{C2} \text{C3} \text{R1} \text{R2} w^2-1\right)-\text{C2}-\text{C3}\right)^2+(\text{C1} (-\text{C2} \text{R1} w-\text{C3} w (\text{R1}+\text{R2}))-\text{C2} \text{C3} \text{R2} w)^2}} $

as the result. To compute Arg[Zth] you can again use ComplexExpand, but you need to tell it what target functions to do the expansion with respect to as in (Also using FullSimplify with assumptions that the parameters are Real to simplify things a bit)

FullSimplify[ ComplexExpand[ Arg[Zth], TargetFunctions -> {Re, Im}], {{w, C1, C2, C3, R1, R2} \[Element]  Reals}]

Yielding

$\tan ^{-1}\left(\frac{\text{C1}^2 \left(\text{C2}^2 \text{R1}^2 \text{R2} w^2+\text{R1}+\text{R2}\right)+2 \text{C1} \text{C2} \text{R2}+\text{C2}^2 \text{R2}}{\text{C1}^2 \text{C2}^2 \text{C3}^2 \text{R1}^2 \text{R2}^2 w^4+w^2 \left(\text{C1}^2 \text{R1}^2 (\text{C2}+\text{C3})^2+2 \text{C1}^2 \text{C3}^2 \text{R1} \text{R2}+\text{C3}^2 \text{R2}^2 (\text{C1}+\text{C2})^2\right)+(\text{C1}+\text{C2}+\text{C3})^2},-\frac{\text{C1}^2 \text{C2}^2 \text{C3} \text{R1}^2 \text{R2}^2 w^4+w^2 \left(\text{C1}^2 \text{R1}^2 (\text{C2}+\text{C3})+2 \text{C1}^2 \text{C3} \text{R1} \text{R2}+\text{C3} \text{R2}^2 (\text{C1}+\text{C2})^2\right)+\text{C1}+\text{C2}+\text{C3}}{\text{C1}^2 \text{C2}^2 \text{C3}^2 \text{R1}^2 \text{R2}^2 w^5+w^3 \left(\text{C1}^2 \text{R1}^2 (\text{C2}+\text{C3})^2+2 \text{C1}^2 \text{C3}^2 \text{R1} \text{R2}+\text{C3}^2 \text{R2}^2 (\text{C1}+\text{C2})^2\right)+w (\text{C1}+\text{C2}+\text{C3})^2}\right) $

And here is an alternative approach with a different TargetFunctions option:

FullSimplify[
 ComplexExpand[ Arg[Zth], 
  TargetFunctions -> Conjugate], {{w, C1, C2, C3, R1, R2} \[Element] 
   Reals}]

giving

$-\frac{1}{2} i \left(\log \left(\text{C1}^2 \text{C2}^2 \text{C3}^2 \text{R1}^2 \text{R2}^2 w^4+w^2 \left(\text{C1}^2 \text{R1}^2 (\text{C2}+\text{C3})^2+2 \text{C1}^2 \text{C3}^2 \text{R1} \text{R2}+\text{C3}^2 \text{R2}^2 (\text{C1}+\text{C2})^2\right)+(\text{C1}+\text{C2}+\text{C3})^2\right)-\log \left(\frac{w^2 \left(\text{C1}^2 \left(\text{C2}^2 \text{R1}^2 \text{R2}^2 w^2+(\text{R1}+\text{R2})^2\right)+2 \text{C1} \text{C2} \text{R2}^2+\text{C2}^2 \text{R2}^2\right)+1}{w^2}\right)+2 \log \left(\frac{-i \text{C1} \text{C2} \text{R1} \text{R2} w^2-w (\text{C1} (\text{R1}+\text{R2})+\text{C2} \text{R2})+i}{\text{C1} \text{C2} \text{C3} \text{R1} \text{R2} w^3-i w^2 (\text{C1} \text{R1} (\text{C2}+\text{C3})+\text{C3} \text{R2} (\text{C1}+\text{C2}))-w (\text{C1}+\text{C2}+\text{C3})}\right)\right)$

So this should give you a sense of approaches you might take...

POSTED BY: David Reiss

Abs and Arg were returning the input expression without evaluating. Thanks a lot for your help!

POSTED BY: Ammar Hazim
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract