# Magnitude is returning a complex number if I precede it with Complex Expand?

I define a function called VoutCoin:

VoutCoin[Mut_, \[Omega]_, Lc_, Rc_] =
ComplexExpand[Abs[I1coin* (j \[Omega] L1 + (j \[Omega] Mut)^2/(j \[Omega] Lc + Rc))]] /. values


I have the suffixes and the values defined:

M = 10^6;
k = 10^3;
m = 10^-3;
u = 10^-6;
n = 10^-9;
pf = 10^-12;

values = {
R1 -> 180,
C1 -> 9.4 n,
L1 -> 1.7 m,
Vin -> LaplaceTransform[Cos[2*\[Pi]*138 k*t - \[Pi]], t, s]
};

j = I;
s = j*\[Omega];


if I invoke the function as defined above with the following arguments, I get:

In[1030]:= VoutCoin[.5, 2*\[Pi]*10 k, 10 u, .1]

Out[1030]= {0. + 8.40133*10^-8 I}


if I remove the ComplexExpand preceding the Abs in the function, I get:

VoutCoin[Mut_, \[Omega]_, Lc_, Rc_] = Abs[I1coin* (j \[Omega] L1 + (j \[Omega] Mut)^2/(j \[Omega] Lc + Rc))] /. values // Simplify

In[1095]:= VoutCoin[.5, 2*\[Pi]*10 k, 10 u, .1]

Out[1095]= {8.40133*10^-8}


What is going on? Why would ComplexExpand[Abs[]] return a complex numnber?

In case anyone wants plugs this into Mathematica, I'm including I1coin below

In[1092]:= I1coin = I1 /. sol1

Out[1092]= {(Vin (Rc + I Lc \[Omega]))/(Mut^2 \[Omega]^2 - (-R1 + I/(C1 \[Omega]) - I L1 \[Omega]) (Rc +
I Lc \[Omega]))}


Thank you in advance for your help....

--------Edit Per Request_----------------------

M = 10^6;
k = 10^3;
m = 10^-3;
u = 10^-6;
n = 10^-9;
pf = 10^-12;

values = {
R1 -> 180,
C1 -> 9.4 n,
L1 -> 1.7 m,
Vin -> LaplaceTransform[Cos[2*\[Pi]*138 k*t - \[Pi]], t, s]
};

j = I;

s = j*\[Omega];

eq1 = Vin - I1 (R1 + 1/(j \[Omega] C1) + j \[Omega] L1) -j \[Omega] Mut I2 == 0;
eq2 = I2 (j \[Omega] Lc + Rc) + j \[Omega] Mut I1 == 0;

sol1 = Solve[eq1 && eq2, {I1, I2}]

I1coin = I1 /. sol1

VoutCoin[Mut_, \[Omega]_, Lc_, Rc_] = ComplexExpand[Abs[I1coin* (j \[Omega] L1 + (j \[Omega] Mut)^2/(j \[Omega] Lc + Rc))]] /. values // Simplify

In[1159]:= VoutCoin[.5, 2*\[Pi]*10 k, 10 u, .1]

Out[1159]= {0. + 8.40133*10^-8 I}


Question is about result out[1159] --- I don't understand why Mathematica returns a complex number for a magnitude. It seems to happen when I precede Abs with ComplexExpand

• I seem to get results in terms of s when I run your code. Are you sure you included all definitions? – MarcoB Apr 24 '18 at 16:25
• maybe one more.....thank you.... – jrive Apr 24 '18 at 16:56
• CopmlexExpand assumes all variables are real. If they're not you'll get unexpected errors. check out ComplexExpand[Abs[t]] /. t -> I – yohbs Apr 24 '18 at 17:11
• @yohbs I do not understand your point. Your replacement rule is not injecting the value I inside the ComplexExpand; it is simply changing it in the result of the evaluation. For injection, you could try: With[{t = I}, ComplexExpand[Abs[t]]], which returns $1$, as I would expect. – MarcoB Apr 24 '18 at 17:35
• You define VoutCoin with = and not := which means that the value is precacluated assuming I1coin is real. then you probably inject a non-real number. sol1 is not defined in your question so it's hard to tell. It would be easier if you provided one contiguous block of code with all definitions that produces the error. – yohbs Apr 24 '18 at 17:54

## 1 Answer

Consider your values rule:

Vin /. values


-((I ω)/(76176000000 π^2 - ω^2))

So, values is replacing Vin with a pure imaginary number. Now, your VoutCoin function is basically:

f[ω_] = ComplexExpand[Sqrt[Vin^2]] /. values


Sqrt[-(ω^2/(76176000000 π^2 - ω^2)^2)]

Here, ComplexExpand[Sqrt[Vin^2]] evaluates to just Sqrt[Vin^2], and then you replace Vin with a pure imaginary number, hence you get the square root of a manifestly negative number. So, naturally:

f[1.]

(* 0. + 1.33009*10^-12 I *)


is imaginary.

If you move the closing bracket of the ComplexExpand so that it include the values replacement, then you will get a real answer:

VoutCoin[Mut_,ω_,Lc_,Rc_] = ComplexExpand[
Abs[I1coin*(j ω L1+(j ω Mut)^2/(j ω Lc+Rc))] /.values
] //Simplify;

VoutCoin[.5,2*π*10 k,10 u,.1]


{8.40133*10^-8}

• thank you for taking the time to explain and demo this to me. much appreciated – jrive Apr 25 '18 at 14:51