I have a rather complicated function with parameters {a, b, c, d, e, f, k}, and I'd like to know its behavior as a function of k alone given other parameters, so I try the following code:
myFunction = D[ComplexExpand[Arg[((a^2 - 2 (2 k + I e - 2 c) (k + d - c))^2/((a^2 - 2 (2 k + I (b + e + 2 I c)) (k + d - c))^2 + 4 E^(2 I k f) b^2 (k + d - c)^2))], TargetFunctions -> {Re, Im}] // Simplify, k];
myFunctionWithValues[k_] := Evaluate[myFunction /. {b -> 10, a -> 2, c -> 100, d -> 0, e -> 1, f -> Pi/100} // FullSimplify]
LogPlot[myFunctionWithValues[k], {k, 95, 105}, PlotRange -> All]
and the outcome looks very bizarre. It can't be correct. To comfirm this, for example, I look at the value of myFunctionWithValues
at the center k=100
by 1) directly replacing k by 100 and 2) by taking the limit k->100, and they disagree with each other:
myFunctionWithValues[k] /. k -> 100 // N
315120.
Limit[myFunctionWithValues[k], k -> 100] // N
10.
Apparently the latter gives a more reasonable result (which is checked by other approaches). Moreover, if I replace the value of all parameters (including k) in the very beginning, Mathematica also yields the same result:
myFunction /. {b -> 10, a -> 2, k -> c, d -> 0, e -> 1, f -> Pi/100} // Simplify
10
myFunction /. {b -> 10, a -> 2, c -> 100, d -> 0, e -> 1, f -> Pi/100, k -> 100} // N
10.
And the correct plot can be given by the following two ways:
(Limit[myFunctionWithValues[k], k -> #]) & /@ Range[95, 105, 1/10] //N;
ListPlot[%, PlotRange -> All, Joined -> True, AxesOrigin -> {100, 0}, DataRange -> {95, 105}, PlotMarkers -> Automatic]
Plot[myFunction /. {b -> 10, a -> 2, c -> 100, d -> 0, e -> 1, f -> Pi/100}, {k, 95, 105}, PlotRange -> All, AxesOrigin -> {100, 0}]
Question: What's going on here? Why can't I simply plot myFunctionWithValues
but have to take the limit of k at each point? More precisely, what's the difference between using the replacement rules and taking limits? I thought it should be the same given that in my case there's no singularity causing trouble, otherwise Mathematica would give me an error message.
myFunctionWithValues[100.03] // N
and then at 100.02 and at 100.01, the values jump all over the place. You are dividing by things, and probably one of these is very small. $\endgroup$Plot[myFunction /. {b -> 10, a -> 2, c -> 100, d -> 0, e -> 1, f -> Pi/100}, {k, 95, 105}, PlotRange -> All]
also gives the correct plot which doesn't blow up. So if there is any singular behavior inmyFunctionWithValues
, it must be an artificial one. $\endgroup$LogPlot[myFunctionWithValues[k], {k, 95, 105}, PlotRange -> All, WorkingPrecision -> 50]
you will get the plot that you desire. $\endgroup$