2
$\begingroup$

I'm new with Mathematica and I have a problem with that, It would be great if you could help me with that. I try to draw a maximum slope of a plot in the same diagram using resource function tangent line, but it seams that it doesn't work for complex function.

ClearAll;
$Assumptions = 
  n ∈ Reals && z ∈ Reals && znew ∈ Reals;

z = Sqrt[2 epsilon] E^(n);
ztwotransitions = Sqrt[2 epsilontwotransitions] E^(n);
znew = Sqrt[2 epsilonnew] E^(n);
epsilon = 
  aminus1^2/(
   18  MPL^2  H0^4) (1 - (aminus1 - aplus)/
       aminus1 E^(-3 (n - n0new)))^2;
epsilontwotransitions = 
  aminus1^2/(
   18  MPL^2  H0^4) (1 - (aminus1 - aplus)/aminus1 E^(-3 (n - n0)))^2;
epsilonnew = 
  astar^2/(18 MPL^2  H0^4) (1 - (astar - aminus2)/
       astar E^(-3 (n - n1)))^2;
modefristsr = 1/Sqrt[2 k] (1 - I/(k τ)) E^(-I k τ);
modefristsrprime = D[modefristsr, τ];
modensr = 
  c1/Sqrt[2 k] (1 - I/(k τ)) E^(-I k τ) + 
   c2/Sqrt[2 k] (1 + I/(k τ)) E^(I k τ);
modensrprime = D[modensr, τ];
modesecondsr = 
  d1/Sqrt[2 k] (1 - I/(k τ)) E^(-I k τ) + 
   d2/Sqrt[2 k] (1 + I/(k τ)) E^(I k τ);
modesecondsrprime = D[modesecondsr, τ];
modefristsrn = Evaluate[modefristsr /. τ -> -1/H0 E^(-n)];
modefristsrprimen = 
  Evaluate[modefristsrprime /. τ -> -1/H0 E^(-n)];
modensrn = Evaluate[modensr /. τ -> -1/H0 E^(-n)];
modensrprimen = Evaluate[modensrprime /. τ -> -1/H0 E^(-n)];
modesecondsrn = Evaluate[modesecondsr /. τ -> -1/H0 E^(-n)];
modesecondsrprimen = 
  Evaluate[modesecondsrprime /. τ -> -1/H0 E^(-n)];
modefristsrn0new = Evaluate[modefristsrn /. n -> n0new];
modefristsrprimen0new = Evaluate[modefristsrprimen /. n -> n0new];
modensrn0new = Evaluate[modensrn /. n -> n0new];
modensrprimen0new = Evaluate[modensrprimen /. n -> n0new];
eqns = {modefristsrn0new - modensrn0new == 0 && 
    modensrprimen0new - modefristsrprimen0new == f0 modensrn0new};
c1c2 = Solve[eqns, {c1, c2}];
solevedmodensrn = modensrn /. c1c2;
modefristsrn0 = Evaluate[modefristsrn /. n -> n0];
modefristsrprimen0 = Evaluate[modefristsrprimen /. n -> n0];
modensrn0 = Evaluate[modensrn /. n -> n0];
modensrprimen0 = Evaluate[modensrprimen /. n -> n0];
eqnswithf2 = {modefristsrn0 - modensrn0 == 0 && 
    modensrprimen0 - modefristsrprimen0 == f2 modensrn0};
c11c21 = Solve[eqnswithf2, {c1, c2}];
solvedfirstslowroll = modefristsrn /. c1c2;
solevedmodensrnnew = modensrn /. c11c21;
solevedmodensrn1 = Evaluate[solevedmodensrnnew /. n -> n1];
solvedmodensrprimen = modensrprimen /. c11c21;
solvedmodensrprimen1 = Evaluate[solvedmodensrprimen /. n -> n1];
modesecondsrn1 = Evaluate[modesecondsrn /. n -> n1];
modesecondsrprimen1 = Evaluate[modesecondsrprimen /. n -> n1];
d1d2 = Solve[
   solevedmodensrn1 - modesecondsrn1 == 0 && 
    modesecondsrprimen1 - solvedmodensrprimen1 == 
     f1 solevedmodensrn1, {d1, d2}];
solevedmodesecondsrn = modesecondsrn /. d1d2;
powerwithc1c2 = k^3/(2 π^2)  (z^(-2)) Abs[solevedmodensrn]^2;
powerwithd1d2 = 
  k^3/(2 π^2)  (znew^(-2)) Abs[solevedmodesecondsrn]^2;
f0 = 3 k0new ( aminus1 - aplus)/aplus;
f1 = 3 k1 (astar - aminus2)/aminus2;
f2 = 3 k0 (aminus - aplus)/aplus;
MPL = 1;
a = E^(n);
k1 = a1 H0;
a0 = E^(n0);
a0new = E^(n0new);
a1 = E^(n1);
τ1 = 0.1;
H0 = 8.8 10^(-7);
σ = 0.01;
astar = 7 10^(-16) MPL^3;
aminus = 7.26 10^(-15) MPL^3;
aplus = 3.35 10^(-14) MPL^3;
deltaa = aminus1 - aplus;
n0 = 10;
n1 = 15;
n0new = 10;
k0 = a0 H0;
k0new = a0new H0;
aminus1 = 7 10^(-16);
aminus2 = 7.26 10^(-15);
LogLogPlot[
 Evaluate[{powerwithc1c2 , powerwithd1d2} /. {k -> k0 kstar, 
    n -> 200}], {kstar, 10^(-1), 1000} , 
 AxesLabel -> {"\!\(\*FractionBox[\(k\), \(k0\)]\)", 
   "Power_Spectrum"}, 
 PlotLegends -> {"one_transition", "Two_transitions"}]

Edited post: This is what should how result should look like:

$\endgroup$
14
  • $\begingroup$ Do you mean complex as in complex numbers $z=x+iy$? Or complicated? $\endgroup$
    – Michael E2
    May 3, 2022 at 14:17
  • $\begingroup$ I mean complex function as a function with imaginary part @Michael E2 $\endgroup$ May 3, 2022 at 14:22
  • $\begingroup$ Then the tangent "line" to $w=f(z)$ is a complex line $w=\alpha z+\beta$, that is in terms of real coordinates, a two-dimensional plane in the four-dimensional space $(w,z)\in{\Bbb C}^2$, no? The only tools we have are ComplexPlot3D and ComplexPlot - not sure about log-log space. But something like this, maybe: func = (var^2 + 1)/(var^2 - 1); number = 1 + I; slope = D[func, var] /. var -> number; height = func /. var -> number; ComplexPlot3D[#, {var, -2 - I, 3 + 4 I}] & /@ {func, slope (var - number) + height} // Show $\endgroup$
    – Michael E2
    May 3, 2022 at 14:40
  • $\begingroup$ Thanks a lot for your response but I want to show the tangent line in the diagram so complexplot does not help. I need to indicate the slope of the function in a specific point. @Michael E2 $\endgroup$ May 3, 2022 at 14:46
  • $\begingroup$ I see 2 lines, I expect 2 tangent functions, isn't it? $\endgroup$
    – yarchik
    May 3, 2022 at 14:47

1 Answer 1

2
$\begingroup$

Here's an example. Linear functions do not have straight graphs under log-log scaling unless they pass through $(0,0)$, in which case their slopes appear as $m=1$ in loglog space. Note that extra graphics added by hand (such as the red points) have to be scaled by hand.

func = (1 + Exp[(10 I + 1) t] + 20 (1 + I) Exp[t^2/10])/(1 + Exp[t^2/10]);
number = 2.5;
slope = D[func, t] /. t -> number;
height = func /. t -> number;

ReImPlot[{func, slope (t - number) + height}, {t, 1, 10}, 
 ScalingFunctions -> {"Log", "Log"},
 GridLines -> {{number}, {}}, PlotRange -> {1, 50},
 Epilog -> {Red, 
   Point[Thread[{Log@number, Log@ReIm[func /. t -> number]}]]}]

Here's a way to draw the lines incorrectly but straight:

ReImPlot[func, {t, 1, 10}, ScalingFunctions -> {"Log", "Log"},
 GridLines -> {{number}, {}}, PlotRange -> {1, 50},
 Epilog -> {
   Red,
   Point[Thread[{Log@number, Log@ReIm[func /. t -> number]}]],
   ColorData[97][2],
   Riffle[
    {Dashing[{}], Dotted},
    InfiniteLine /@ Transpose@{
       Thread[{Log@t, Log@ReIm[func]} /. t -> number],
       Thread[{Log@t, Log@ReIm[func]} /. 
         t -> number (1 + Sqrt@$MachineEpsilon)]
       }
    ]
   }
 ]

I used a numerical approximation to the apparent slope in scaled loglog space (by taking the line through two nearly adjacent points), because it was easier and good enough for graphing.

$\endgroup$
10
  • $\begingroup$ func = {(9 E^(-2 n) H0^4 k^3 MPL^2 Abs[( E^((I E^-n k)/ H0) (1 + (I E^(2 n0new) f0 H0^2)/(2 k^3) + (I f0)/(2 k)) (1 + ( I E^n H0)/k))/(Sqrt[2] Sqrt[k]) - ( I E^(-((I E^-n k)/H0) + (2 I E^-n0new k)/H0) f0 (1 - (I E^n H0)/k) (I E^n0new H0 + k)^2)/( 2 Sqrt[2] k^(7/2))]^2)/( 2 aminus1^2 (1 - ((aminus1 - aplus) E^(-3 (n - n0new)))/ aminus1)^2 \[Pi]^2)} $\endgroup$ May 3, 2022 at 18:38
  • $\begingroup$ number = 1; slope = D[func, k] /. k -> number; height = func /. k -> number; ReImPlot[{func, slope (k - number) + height}, {k, 1, 100}, ScalingFunctions -> {"Log", "Log"}, GridLines -> {{number}, {}}, PlotRange -> {1, 100}, Epilog -> {Red, Point[Thread[{Log@number, Log@ReIm[func /. k -> number]}]]}] $\endgroup$ May 3, 2022 at 18:39
  • $\begingroup$ Coordinate {0, {Log[ 558016.9319389397 Re[ 2.718281828459045^((-2) $CellContextn) (1 + 46.85714285714286 2.718281828459045^((-3) (-10 + \ $CellContextn)))^(-2) Abs[ Complex[-0.0007803515115195993, 0.02012192791307727] 2.718281828459045^(Complex[0., 103.18165855110193` should be a pair of numbers, or a Scaled or Offset form . $\endgroup$ May 3, 2022 at 18:40
  • $\begingroup$ Abs is not differentiable. $\endgroup$
    – Michael E2
    May 3, 2022 at 18:41
  • $\begingroup$ Thanks a lot for your answer, I write your code and it did not work for my function and it gave the massage above, I don't know what I missed. I wrote the function I'm using.@ Michael E2 $\endgroup$ May 3, 2022 at 18:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.