# Functions can graph but can't solve for partial derivatives

First I got a function g[xo,yo] by solving a system of partial differential equations

Clear["Global*"]
θ = 0.;
λ = 1.2398/5.207/1000;
f = 168390.566457655;
outz = 0.954;
Subscript[δ, 1] = 1.12306632*10^-4;
Subscript[δ, 2] = 10^-10;
Subscript[bt, 1] = 2.28577792*10^-5;
Subscript[bt, 2] = 10^-10;
χ1 = -2 Subscript[δ, 1] + 2 I Subscript[bt, 1];
χ2 = -2 Subscript[δ, 2] + 2 I Subscript[bt, 2];
Δχ = χ1 - χ2;
k = 2 (π/λ);
Table[β[b] = -((2 (b x) Sin[θ])/f) - ((b x)/f)^2, {b, -5,
5}];
Table[χ[
a] = (Δχ (1 - (-1)^
Abs[a]))/(2 I a π), {a, -10, -1}];
Table[χ[
a] = (Δχ (1 - (-1)^Abs[a]))/(2 I a π]), {a,
1, 10}];
Table[χ[a] = (χ1 + χ2)/2, {a, 0, 0}];
eqns =
Join[
Table[
(Sin[θ] + (h x)/f) D[Subscript[Y, h][x, z], x] +
Cos[θ] D[Subscript[Y, h][x, z], z] ==
((I π)
(β[h] Subscript[Y, h][x, z] +
Sum[χ[h - l] Subscript[Y, l][x, z], {l, -5, 5}]))/λ,{h, -5, 5}],
Table[Subscript[Y, h][x, 0] == If[h == 0, 1, 0], {h, -5, 5}]];
s = NDSolve[eqns,
Table[Subscript[Y, h], {h, -5, 5}], {x, 0, 143.277}, {z, 0, 30},
Method -> {"MethodOfLines",
"SpatialDiscretization" -> {"TensorProductGrid",
"MaxPoints" -> 100, "MinPoints" -> 100,
"DifferenceOrder" -> 4}, "TemporalVariable" -> z}];

YO[x_?NumericQ /; (0 > x >= -150)] := YO[-x]
YO[x_?NumericQ] =
First[E^((I j \[Pi])/(\[Lambda] f) x^2)
Subscript[Y, -1][x, outz] /. s /. j -> -1];

f1[xo_?NumericQ, zo_?NumericQ] :=
NIntegrate[(E^(I k zo)  E^(((I k) xo^2)/(2 zo))
YO[x] E^(((I k) x^2)/(2 zo)) E^(-(((I k) x xo)/zo)))/
Sqrt[I λ zo], {x, -143.277, 143.277},
Method -> {"LocalAdaptive", "SymbolicProcessing" -> 0}];
g[xo_?NumericQ, zo_?NumericQ] := Abs[f1[xo, zo]^2];


g[xo,168390] can be ploted:

Plot[g[xo, 168390], {xo, -1, 1}, PlotRange -> All, AxesLabel -> {"Radius(μm)", "Intensity"}]


NIntegrate and Findroot

NIntegrate[g[xo, 168390], {xo, -2, 2}]/(143.277*2)

(*0.241637*)

FindMaximum[{g[xo, zo], -1. <= xo <= 1. && 168390. - 1000 <= zo <= 168390. + 1000.}, {{xo, 0}, {zo, 168390}}]

(*{498.448, {xo -> 1.61611*10^-14, zo -> 168390.}}*)


But when I wanted to mark the position and size of its peaks, I found that it could not locate the peak by taking partial derivatives.

I tried

NSolve[(D[g[xo, 168390], xo]) == 0 && D[g[xo, 168390], {xo, 2}] < 0, Reals]


but shows NSolve::fulldim: The solution set contains a full-dimensional component; use Reduce for complete solution information. And then I tried to take the partial derivative of the function directly and it didn't work.

I found a couple of ways to figure out the peak of the function. But the prerequisite for success was to be able to take partial derivatives. So I want to know what's wrong with the partial derivative of g[xo,yo].

Your function g is not defined "symbolically", so you should not be calculating the derivative.

rs = Range[-1, 1, .005];
vals = g[#, 168390] & /@ rs;
{maxP, max} = Transpose@FindPeaks[vals];
{minP, min} = Transpose@FindPeaks[-vals];

Plot[g[x, 168390], {x, -1, 1},
Epilog -> {AbsolutePointSize[5], Green,
Point@Transpose@{rs[[maxP]], max}, Red,
Point@Transpose@{rs[[minP]], -min}}, PlotRange -> Full]
`

• Thank you for your answer, which perfectly solved my question! The first three functions should be unrelated and can be deleted. There's one thing I don't understand, what is "symbolically", Why can't differentiate if I can draw it? Dec 26 '21 at 13:21
• @shrocat Derivatives are obtained by reasoning about how the function changes with an infinitesimal change of the independent variable. The rules of differentiation cannot generally be applied to a function whose nature is only defined through a numerical procedure. Dec 26 '21 at 22:53
• @John Doty Thank you for answering my questions！Although I'm still a little confused o (￣▽￣)ｄ. Dec 27 '21 at 2:03