I want to defined a function that solves some equation numerically:
h[r_, R_, l_, n_] := NSolve[l^2 == r^2 + R^2 - 2 h^2 - 2 Sqrt[(r^2 - h^2) (R^2 - h^2)] Cos[2 Pi/n] && h >= 0, h, Reals]
I then want to plot this function via
DensityPlot[h[r,1,l,6], {r,0,1}, {l,0,1}]
Of course, this does not work, as NSolve
does not return a numeric value, but a list of lists of rules, which might even be empty.
My intention would be to define h
in such a way, so that it extracts the desired numerical value (the unique positive solution, if there is one), and otherwise returns, ..., well, no idea actually. What should it return otherwise? What I want in the end, is that DensityPlot
works well with that function, so that regions where h
is not well-defined are just blank. The region where it is not well-defined are not explicitly known, though.
I thought about defining h
so that it returns Indeterminate
whenever the list is empty, but then DensityPlot
seems to throw $Failure. Also
RegionPlot[h[r,1,l,6] =!= Indeterminate, {r,0,1}, {l,0,1}]
does not to work for showing the region in which h
is well-defined.