Solve cannot find solutions where plotting shows they exist
Show older comments
I am trying to find points in a function, after fitting to some data, where the slope is zero. The obvious (to me) way to do this is to take the derivative of the equation and find the zero-crossings. The equation I am fitting is a difference of Gaussians (with shared mean but different standard deviations) on a linear slope. I am using a mixture of standard and symbolic functions because I am not sure how to do everything using one or the other. Basically, I
- Define my function using a function handle and using symbolic math
- Pull some reasonable initial parameters from the data
- Optimize the fit of the function
- Compute the derivative of the optimized function
- Solve the derivative for y = 0.
The following code looks, from the graph produced, like it should have four solutions for this particular data-set, but I get none.
load yData.mat
% MATLAB Function
DoGoLS = @(Params, xData) ...
Params(1) + ...
Params(2) * exp(-.5 * ((xData - Params(3))/Params(4)).^2) - ...
Params(5) * exp(-.5 * ((xData - Params(3))/Params(6)).^2) + ...
Params(7) * xData
% Symbolic Function
syms y0 a1 x0 b1 a2 b2 c x
SymDoGoLS(y0, a1, x0, b1, a2, b2, c, x) = ...
y0 + ...
a1 * exp(-.5*((x - x0)/b1)^2) - ...
a2 * exp(-.5*((x - x0)/b2)^2) + ...
c * x
% Initial parameters
DoGoLSInit = ...
[median(yData), ... %y0
max(yData) - min(yData), ... %a1
size(yData, 1) / 2, ... %x0
35, ... %b1
max(yData) - min(yData), ... %a2
350, ... %b2
0]; %c
% Optimal parameters
DoGoLSFit = lsqcurvefit(...
DoGoLS, ... % handle
DoGoLSInit, ... % initial params
(1:size(yData, 1))', ... % xData
yData); % yData
% Compute and plot derivative of solved function
SymDoGoLSdYdX(x) = diff(SymDoGoLS(...
DoGoLSFit(1), ...
DoGoLSFit(2), ...
DoGoLSFit(3), ...
DoGoLSFit(4), ...
DoGoLSFit(5), ...
DoGoLSFit(6), ...
DoGoLSFit(7), x), x);
subplot(2, 1, 1)
plot((1:size(yData, 1)), yData, 'b', ...
(-750:1750), DoGoLS(DoGoLSFit, (-750:1750)), '--r')
axis auto; title('Data and Fit')
subplot(2, 1, 2)
ezplot('0', [-750, 1750]);
hold on
ezplot(matlabFunction(SymDoGoLSdYdX), [-750, 1750]);
axis auto; title('Fit dY/dX')
% Find zero-crossings
ZCs = solve(SymDoGoLSdYdX(x) == 0, x)
Can someone explain what I am doing wrong? Or is there some better way to obtain the solution? I've attached the data
Accepted Answer
More Answers (0)
Categories
Find more on Conversion Between Symbolic and Numeric in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!