fzero, because complex function value encountered during search

22 ビュー (過去 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 件のコメント
James_111
James_111 2019 年 2 月 5 日
Star Strider
Star Strider 2019 年 2 月 5 日
Noted.

サインインしてコメントする。

その他の回答 (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 Center および File ExchangeSolver 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