Solve cannot find solutions where plotting shows they exist

조회 수: 2 (최근 30일)
James Akula
James Akula 2016년 7월 7일
댓글: James Akula 2016년 7월 7일
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

채택된 답변

John D'Errico
John D'Errico 2016년 7월 7일
No. Solve could not find an ANALYTICAL solution. The expression you have has no such solution. That a numerical solver could not find a solution is not true.
ZCs = solve(SymDoGoLSdYdX(x) == 0, x)
Warning: Cannot find explicit solution.
> In solve (line 316)
ZCs =
Empty sym: 0-by-1
ZCs = vpasolve(SymDoGoLSdYdX(x) == 0, x,500)
ZCs =
522.06599522322937335889187954597
  댓글 수: 1
James Akula
James Akula 2016년 7월 7일
> The expression you have has no such solution.
Yes, I can solve it numerically using fsolve or vpasolve or other approaches, but those each only identify a single solution, which you show. If I know other starting points, I can find alternative solutions, but I don't have any a priori way of knowing where to start guessing, and with different input data, the locations will be different.
In this particular case, the correct answer has four solutions, and they are certainly "determinate" from the formula, somehow. That is, I expect the expression does have an ANALYTICAL solution. But perhaps there is something happening here I don't understand.
Why wouldn't it?

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Calculus에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by