how accurate is the function fsolve?

2 ビュー (過去 30 日間)
Diogo
Diogo 2016 年 5 月 5 日
コメント済み: Star Strider 2016 年 5 月 5 日
I have a nonlinear equation to solve and I know (from the textbook) that the exact solution is zero but when I use this method I get 0.9891. Here's how I did it:
function F = myFunction(z)
x = z(1);
% Probability of False Alarm [Pfa]
Pfa=0.5;
F(1) = exp(-x^2/2)/(x^2/2) - sqrt(2*pi)*Pfa;
end
Then on command window: >> zg=[1]; z=fsolve(@myFunction,zg) gives: z=0.9891; However if I change Pfa to 0.001, the exact solution is 3 and it gives a much better solution of 2.9939!

採用された回答

John D'Errico
John D'Errico 2016 年 5 月 5 日
編集済み: John D'Errico 2016 年 5 月 5 日
I'll be lazy, and write the function as a function handle.
Pfa=0.5;
F = @(x) exp(-x.^2/2)./(x.^2/2) - sqrt(2*pi)*Pfa;
ezplot(F,[.5,1.5])
grid on
fsolve(F,1)
ans =
0.989138491394299
To me, that solution seems pretty accurate compared to the plot. Lets try a symbolic solution.
vpasolve(exp(-x.^2/2)./(x.^2/2) - sqrt(2*pi)*Pfa == 0,1)
ans =
0.98913855820552302829982713582152
Fsolve uses a tolerance. (read the help docs for fsolve) The default tolerance is 1.e-6, which is roughly where I see some deviation.
Anyway, you claim that when Pfa == 0.5, the exact solution is zero. Of course that is completely wrong. At zero that function has a singularity, essentially 1/0 in the limit, so it must produce a +inf as a result as x approaches 0.
By the way, for PFA = 0.001, the exact solution is not 3.
Pfa = 0.001;
vpasolve(exp(-x.^2/2)./(x.^2/2) - sqrt(2*pi)*Pfa == 0,1)
ans =
2.9958361659324325479490777418211
Anyway, what seems to be the problem? Besides the error in either your textbook, or what you think you understood from that textbook. Or, maybe the function that you wrote is not what was actually in that textbook. I cannot know for sure. But fsolve seems to have worked reasonably well.
  6 件のコメント
John D'Errico
John D'Errico 2016 年 5 月 5 日
What you have written is simply not the analytical integral of the normal density function. As others have pointed, there is no simple expression for that integral, but there are ways to solve the problem in MATLAB. I'd suggest using the stats toolbox functions normcdf or norminv. If you don't have that TB, then there are still many ways to solve the problem.
Star Strider
Star Strider 2016 年 5 月 5 日
Diogo — Thank you!
No apologies necessary. It would have been helpful to us if you had posted the image earlier. We would then know what you actually want to do.
The function you want (and that does this integration for you) is the ‘complementary error function’, erfc. The erfc function calculates it for you. It is a core MATLAB function, so no Toolboxes are necessary. I used the anonymous function and integral to demonstrate how to use fsolve to calculate it, since that appears to be the essence of your question.
If you have the opportunity, take a course in partial differential equations. That’s where I first encountered the maths behind the error function, and that it cannot be analytically integrated.

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeSpecial Functions についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by