Defining a function in terms of a solution returned by Solve - Mathematica Stack Exchange most recent 30 from mathematica.stackexchange.com 2019-08-18T03:41:59Z https://mathematica.stackexchange.com/feeds/question/84988 http://www.creativecommons.org/licenses/by-sa/3.0/rdf https://mathematica.stackexchange.com/q/84988 0 Defining a function in terms of a solution returned by Solve user129412 https://mathematica.stackexchange.com/users/24936 2015-06-02T11:49:45Z 2015-06-02T13:37:32Z <p>The title is a little ambiguous, but I didn't know how else to put it. What I'm trying to do is solve a system of equations</p> <pre><code>system1 = {-I*ω*a1 == -I*ω1*a1 - I*J12*a2 - I*J13*a3 - κ1[ω]/2*a1 - γ1[ω]/2*a1 + Sqrt[κ1[ω]]*ain, -I*ω*a2 == -I*ω2*a2 - I*J12*a1 - I*J23*a3 - γ2/2*a2, -I*ω*a3 == -I*ω3*a3 - I*J13*a1 - I*J23*a2 - γ3/2*a3}; </code></pre> <p>Which I then solve for a1, a2 and a3:</p> <pre><code>s = Solve[system1, {a1, a2, a3}]; </code></pre> <p>And then, finally, I am interested in a function $R(\omega)$ which is given by</p> <pre><code>I*Sqrt[κ1]*(a1 /. s)/ain - 1 </code></pre> <p>If I simply use all the above and evaluate that last term, it works. I get a (rather ugly) expression of the type</p> <p>$\left\{-1+\frac{2 i \text{$\kappa $1} \left(4 \text{J23}^2+(\text{$\gamma $2}-2 i (\omega -\text{$\omega $2})) (\text{$\gamma $3}-2 i (\omega -\text{$\omega $3}))\right)}{4 \text{J12}^2 (\text{$\gamma $3}-2 i (\omega -\text{$\omega $3}))-16 i \text{J12} \text{J13} \text{J23}+4 \text{J13}^2 (\text{$\gamma $2}-2 i (\omega -\text{$\omega $2}))+(\text{$\gamma $1}+\text{$\kappa $1}-2 i \omega +2 i \text{$\omega $1}) \left(4 \text{J23}^2+(\text{$\gamma $2}-2 i \omega +2 i \text{$\omega $2}) (\text{$\gamma $3}-2 i \omega +2 i \text{$\omega $3})\right)}\right\}$</p> <p>(I apologize for the format, I don't know how to give variables and parameters subscripts in mathematica so for example $J_{12}$ comes out as $J12$)</p> <p>However, I want to define this as a function, and I am not sure if </p> <pre><code>R[ω_] := I*Sqrt[κ1]*(a1 /. s)/ain - 1 </code></pre> <p>does the trick. </p> <p>Now, the real problem comes from the fact that I don't want just this analytic expression, I want to plot $R(\omega)$. The real and imaginary parts, as well as the argument. For this I need values for my constants of course, which is fine. But the problems start appearing when I consider for example $\kappa_1$, which is not a constant but a function. To be clear, every term in these equations, apart from $a_1$, $a_2$, $a_3$,$\kappa_1$ and $\gamma_1$, are constants. These other five depend on the only variable in the equations, $\omega$. </p> <p>The main issue is that I don't know how I should do this. I can define for example $\kappa_1$ the way I want to without any problems using</p> <pre><code>κ1[ω_] := 1/(ω*Zc*(C1 + CJ1 + CJ3)); </code></pre> <p>This gives me a function $\kappa_1$ that I can evaluate. But how do I put this into my system of equations, system1? Do I have to replace <code>κ1 with κ1[ω]?</code></p> <p>If I do that, then I run into trouble when solving the system and defining an equation. If anyone coudld help, I'd be very grateful. For completeness, below is the full list of parameters:</p> <pre><code>C1 = 190*10^-15; C2 = 240*10^-15; C3 = 270*10^-15; L1 = 1.74*10^ -9; L2 = L1; L3 = L1; CJ1 = 30*10^-15; CJ2 = CJ1; CJ3 = CJ1; R1 = 2*10^5; R2 = R1; R3 = R1; Zc = 50; Cmatrix = {{C1 + Cκ, CJ1, CJ3}, {CJ1, C2, CJ2}, {CJ3, CJ2, C3}}; Lmatrix = {{L1, 0, 0}, {0, L1, 0}, {0, 0, L1}}; Linv = Inverse[Lmatrix]; Cinv = Inverse[Cmatrix]; ω1 = Sqrt[Cinv[[1, 1]]*Linv[[1, 1]]]; ω2 = Sqrt[Cinv[[2, 2]]*Linv[[2, 2]]]; ω3 = Sqrt[Cinv[[3, 3]]*Linv[[3, 3]]]; J12 = 0.5*Cinv[[1, 2]]/(Sqrt[Cinv[[1, 1]]]*Cinv[[2, 2]])*Sqrt[ω1*ω2]; J13 = 0.5*Cinv[[1, 3]]/(Sqrt[Cinv[[1, 1]]]*Cinv[[3, 3]])*Sqrt[ω1*ω3]; J23 = 0.5*Cinv[[2, 3]]/(Sqrt[Cinv[[2, 2]]]*Cinv[[3, 3]])*Sqrt[ω3*ω2]; Cκ = 80*10^-15; γ2 = 1/(R2*(C2 + CJ1 + CJ2)); γ3 = 1/(R3*(C3 + CJ2 + CJ3)); κtilde = 1/(Zc*Cκ); BigK[ω_] := 1 + (κtilde/ω)^2; Cκtilde[ω_] := Cκ*(1 - 1/BigK[ω]); κ1[ω_] := 1/(BigK[ω]*Zc*(C1 + CJ1 + Cκtilde[ω] + CJ3)); γ1[ω_] := 1/(R1*(C1 + CJ1 + CJ3 + Cκtilde[ω])); </code></pre> https://mathematica.stackexchange.com/questions/84988/-/84996#84996 2 Answer by m_goldberg for Defining a function in terms of a solution returned by Solve m_goldberg https://mathematica.stackexchange.com/users/3066 2015-06-02T13:22:05Z 2015-06-02T13:37:32Z <p>Using your definitions of <code>system1</code> and <code>s</code>, when I write </p> <pre><code>R[ω_] = Simplify[I*Sqrt[κ1[ω]]*(a1 /. s)/ain - 1][] </code></pre> <p>I get</p> <blockquote> <pre><code>R[ω_] = -1 + (J13*(4*J23^2 + (γ2 - (2*I)*(ω - ω2))*(γ3 - (2*I)*(ω - ω3)))*κ1[ω])/ (4*((J12*J23 + J13*((I/2)*γ2 + ω - ω2))* (-J13^2 + ((I*γ3 + 2*ω - 2*ω3)*(2*ω - 2*ω1 + I*γ1[ω] + I*κ1[ω]))/4) - ((2*J13*J23 + J12*(I*γ3 + 2*ω - 2*ω3))*(2*J12*J13 + 2*J23*ω - 2*J23*ω1 + I*J23*γ1[ω] + I*J23*κ1[ω]))/4)) </code></pre> </blockquote> <p>With this definition of <code>R</code>, <code>Re[R[ω]]</code> ,<code>Im[R[ω]]</code>, and <code>Arg[R[ω]]</code> all seem eminently plot-able as functions of <code>ω</code>. If this doesn't work for you, you need to tell us what goes wrong.</p> <p>Please note the use of <code>Set</code> ( = ). You don't want to use <code>SetDelayed</code> for defining <code>R</code>. In this case, the righthand side of the definition needs to be evaluated when the definition is evaluated.</p>