how accurate is the function fsolve?

조회 수: 1 (최근 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 CenterFile Exchange에서 Special Functions에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by