fzero, because complex function value encountered during search

조회 수: 20 (최근 30일)
James_111
James_111 2019년 2월 5일
댓글: Star Strider 2019년 2월 5일
Hi my friend, I have one question regarding the fzero. My code is as follows:
p=11;
spdf = @(x) 3.*(50.^3)./(x+50).^(3+1)./(1-(50./(50+50)).^3);
fun2 = @(x,z) min(x,max(0,fzero(@(y) (100-p-x+y).^(-0.5)-2.*z(2)-4.*z(1).*y,0))).^2.*spdf(x);
fun3 = @(z) integral(@(x) fun2(x,z),0,50,'arrayvalued', true);
fun21 = @(x,z) min(x,max(0,fzero(@(y)(100-p-x+y).^(-0.5)-2.*z(2)-4.*z(1).*y,0))).*spdf(x);
fun31 = @(z) integral(@(x) fun21(x,z),0,50,'arrayvalued', true);
fsolve(@(z)[fun31(z)-10; fun3(z)-178.57145], [0.002; 0.04])
This code could run and the answer is:
ans =
0.000243425504922
0.046499744202347
But I test the answer. It is not correct. The information before showing the answer is
because complex function value encountered during search.
(Function value at -40.96 is -0.053117-0.73555i.)
Check function or try again with a different starting value.
No solution found.
fsolve stopped because the relative size of the current step is less than the
default value of the step size tolerance squared, but the vector of function values
is not near zero as measured by the default value of the function tolerance.
I know that fzero cannot deal with complex function. But if I changed "fzero" to be "fsolve". The code is really really slow and the error is still "no solution solved". Another way I tried is I put "abs" in my code and the new code becomes:
p=11;
spdf = @(x) 3.*(50.^3)./(x+50).^(3+1)./(1-(50./(50+50)).^3);
fun2 = @(x,z) min(x,max(0,fzero(@(y) abs((100-p-x+y)).^(-0.5)-2.*z(2)-4.*z(1).*y,0))).^2.*spdf(x);
fun3 = @(z) integral(@(x) fun2(x,z),0,50,'arrayvalued', true);
fun21 = @(x,z) min(x,max(0,fzero(@(y) abs((100-p-x+y)).^(-0.5)-2.*z(2)-4.*z(1).*y,0))).*spdf(x);
fun31 = @(z) integral(@(x) fun21(x,z),0,50,'arrayvalued', true);
fsolve(@(z)[fun31(z)-10; fun3(z)-178.57145], [0.002; 0.04])
But the new error is
Equation solved, fsolve stalled.
fsolve stopped because the relative size of the current step is less than the
default value of the step size tolerance squared and the vector of function values
is near zero as measured by the default value of the function tolerance.
Although I know if change it to be abs of my function, it will not make my result correct. But I am still very interested in how to fix the second code as well. Any one has some ideas? Thanks in advance!

채택된 답변

Star Strider
Star Strider 2019년 2월 5일
The ‘new error’ does not appear to be an error. The fsolve function encountered what appears to be a global minimum (or at least a minimum where the values of the functions are alll near zero), making the solution a success.
  댓글 수: 8

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

추가 답변 (1개)

Matt J
Matt J 2019년 2월 5일
편집: Matt J 2019년 2월 5일
Since you only have two unknowns, you could plot the function (as a surface). Then you can see graphically if a solution even exists, and if so, approximately where your initial guess should be (i.e., where the surface reaches zero).
In any case, I would use fminsearch to solve this problem. fsolve is not a good choice here, because it is not even clear if your function is smooth.
  댓글 수: 1
James_111
James_111 2019년 2월 5일
Thanks, Matt! I will look for the difference between fsolve and fminsearch.

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

카테고리

Help CenterFile Exchange에서 Solver Outputs and Iterative Display에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by