I will be fitting 3D differences of Gaussians to data, and from these fits I want to be able to identify troughs and peaks (points of zero slope) in my resultant functions. I can compute XYZ data points and look for local maxima, but the results are not satisfactory, at least the way I am doing it.
Below is a sample script showing my problem:
syms a1 a2 b1 b2 b3 b4 c x x0 y y0
DoG3D(a1, a2, b1, b2, b3, b4, c, x, x0, y, y0) = ...
(a1 * exp(-1/2 * (((x - x0)/b1)^2 + ((y - y0)/b2)^2))) - ... % Gaus 1
(a2 * exp(-1/2 * (((x - x0)/b3)^2 + ((y - y0)/b4)^2))) + ... % Gaus 2
c;
% The constants below will differ, depending on a fit, but here are some
% convenient ones
% a1 a2 b1 b2 b3 b4 c x x0 y y0
DoG3DFit(x, y) = DoG3D(-50, -25, 50, 150, 100, 300, 10, x, 220, y, 180);
pretty(DoG3D)
figure
subplot(2, 2, 1)
fsurf(DoG3DFit, [-1000, 1000])
daspect([50, 50, 1])
% Trying to solve it numerically is unsatisfactory (at least this way)
[X, Y] = meshgrid(-1000:1000, -1000:1000);
matDoG3DFit = matlabFunction(DoG3DFit);
Z = matDoG3DFit(X, Y);
Zpoints = or(imregionalmax(Z), imregionalmax(-Z));
subplot(2, 2, 2)
imagesc(Zpoints)
axis image xy
% In 2D, this is easy
DoG(a1, a2, b1, b2, c, x, x0) = ...
a1 * exp(-1/2 * (((x - x0)/b1)^2)) - ... % Gaus 1
a2 * exp(-1/2 * (((x - x0)/b2)^2)) + ... % Gaus 2
c;
pretty(DoG)
zeroPoints = solve(diff(DoG(-50, -25, 50, 150, 10, x, 220), x) == 0, x);
subplot(2, 2, 3)
fplot(DoG(-50, -25, 50, 150, 10, x, 220), [-1000, 1000])
hold on
scatter(zeroPoints, DoG(-50, -25, 50, 150, 10, zeroPoints, 220), 'r')
% How do I do the same in 3D?
I am hoping there is a way, using the Symbolic Math or other toolbox, to generate formulae that will describe the locations where the slope is zero. These should be, to my understanding, at point (x0, y0), and then at points around the rim of the "caldera." The location of those points will depend upon what constants I supply DoG3D, above.
If this were a 2D difference of Gaussians, I would simply take the derivative and solve for 0, but I do not know what the equivalent approach is for a 3D function.
Can someone please help me to achieve this? Or is there no such solution?