I have a module that, when given a value of the variable a, returns a function of the variable x. My actual problem is longer and more complicated; here I merely provide an MWE, say:
TestModule[a_] :=
Module[{}, {vals, sols} =
NDEigensystem[{D[\[Phi][u], {u, 2}] + 1/u D[\[Phi][u], {u, 1}] -
1/u^2 \[Phi][u] - 1/4 a^2 u^2 \[Phi][u],
DirichletCondition[\[Phi][u] == 0, u == 1]}, \[Phi][u], {u, 0,
1}, 5, Method -> {"PDEDiscretization" -> {"FiniteElement",
"MeshOptions" -> {"MaxCellMeasure" -> 10^-3}}}]; Return[\!\(
\*SubsuperscriptBox[\(\[Sum]\), \(i = 1\), \(5\)]\(
\*FractionBox[\(0.01\), \(
\*SuperscriptBox[\((x - vals[\([\)\(i\)\(]\)])\), \(2\)] +
\*SuperscriptBox[\(0.01\), \(2\)]\)] sols[\([i]\)]\)\) /. {u -> 0},
Module];]
For instance (just to make things concrete), TestModule[0.5] returns
2.51604*10^-6/(0.0001 + (14.7028 + x)^2) + 5.4659*10^-6/(
0.0001 + (49.2393 + x)^2) + 8.92959*10^-6/(
0.0001 + (103.52 + x)^2) - 0.0000128153/(
0.0001 + (177.542 + x)^2) + 0.0000170653/(0.0001 + (271.302 + x)^2)
I would now like to make a DensityPlot in the variables a and x. However, if I write
DensityPlot[Evaluate@TestModule[a],{a,0,1},{x,-500,0}]
I get the error "The PDE coefficient 1/u^2+(a^2\u^2)/4 does not evaluate to a numeric scalar at the coordinate {0.5}; it evaluated to 4. +0.0625` a^2 instead". Naively it seems to me that DensityPlot is assuming I have an explicit expression in terms of a and then trying to evaluate the function, but this obviously fails at the level of DEigensystem. Instead, I would like that for each input of a that DensityPlot feeds the module, a function of x is obtained and then evaluated at the different values of x. Is there a way to ensure this and let DensityPlot first obtain the function of x for each given a, and only then evaluate at x? Please note that my actual problem is much more complicated and I would like to only evaluate the module once for each given a. I guess it all boils down to somehow modifying the evaluation order of DensityPlot. So ideally I'd like it to focus individually on each vertical slice, and then perform many of these slices corresponding to different values of a. Hopefully what I'm asking makes sense. Thank you in advance!


TestModule[a_?NumericQ]in the definition does give a workingDensityPlot. The problem is that then that the function gets evaluated at every pair(a,x). I really need it to only evaluated once at eachaand then evaluate atx, otherwise the plot in my actual problem will take literally days to complete. Perhaps this is not possible withDensityPlotitself and I have to find an alternative. – Maisenberg Dec 10 '22 at 22:57