fzero, because complex function value encountered during search

21 views (last 30 days)
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!

Accepted Answer

Star Strider
Star Strider on 5 Feb 2019
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 Comments

Sign in to comment.

More Answers (1)

Matt J
Matt J on 5 Feb 2019
Edited: Matt J on 5 Feb 2019
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.

Community Treasure Hunt

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

Start Hunting!