the integral of two methods are so different,can we trust the matlab anymore?

4 visualizzazioni (ultimi 30 giorni)
the integral of two methods are so different,can we trust the matlab anymore?
codes are as this
clc;
clear all;
close all;
syms x
syms pi
f=exp(-x^2/2)/sqrt(2*pi);
answer1=int(f,-10^4,10^4)
vpa(answer1)
clear x
clear pi
g=@(x)exp(-x.^2/2)./sqrt(2*pi);
answer2=integral(g,-10^4,10^4)
answer is as this
answer1 =
erf(5000*2^(1/2))
ans =
1.0
answer2 =
5.8465e-35
>>
  2 Commenti
Alex Mcaulley
Alex Mcaulley il 24 Ott 2019
I don't know what the problem is, but if you change the integration limits it works:
g=@(x)exp(-x.^2/2)./sqrt(2*pi);
answer2 = integral(g,-inf,inf)
answer2 =
1.0000

Accedi per commentare.

Risposte (1)

Steven Lord
Steven Lord il 24 Ott 2019
Each function returns the correct answer for the question it was asked. You asked the question in a particularly difficult way for integral. The integral function evaluates your integrand at a set of points. If it decides that it doesn't have an accurate enough value for the integral in a particular region, it evaluates the integrand at more points in that region. If it decides its value is accurate enough, it doesn't.
Let's write a version of your integrand that shows where integral evaluated it.
function y = myintegrand(x)
y = exp(-x.^2/2)./sqrt(2*pi);
disp([x.', y.'])
If you integrate myintegrand over a short range, the points at which it evaluates the integrand are in the "interesting" region of the function.
integral(@myintegrand, -5, 5)
If you integrate it over a really long range, because your function value decreases to zero so quickly (look at its value at say x = 20) almost all the points where integral evaluates it give zero as your function value. There's a (very) small bump in the middle, but that bump is so small integral decides that it doesn't need to evaluate your function at more points to improve the accuracy of the answer.
integral(@myintegrand, -10^4, 10^4)
In a comment I wrote on this Answers post I use the analogy of counting vehicles in a parking lot. Integrating over the interval [-5, 5] is like counting when you're standing in the parking lot. Integrating over the interval [-10^4, 10^4] is like counting those same vehicles by eye out the window of a plane cruising at 30,000 feet.
If you want to count vehicles, do it from the parking lot not the windows of an airplane. If you want to integrate a function, integrate over the region where the function is "interesting".
  2 Commenti
dcydhb dcydhb
dcydhb dcydhb il 25 Ott 2019
Modificato: dcydhb dcydhb il 25 Ott 2019
so in fact in the numerical integration,we can't believe the result of matlab anymore,can i say that?
it is because i use the symbolic integration to verify that i found this question,as this
f=exp(-x^2/2)/sqrt(2*pi);
answer1=int(f,-10^4,10^4)
vpa(answer1)
if i didn't use the int,maybe i can't find the question,so how can we beliveve matlab numerical integration or how can we set the integration settings?
Steven Lord
Steven Lord il 25 Ott 2019
You could say that but it wouldn't be true, any more than your inability to count cars in a parking lot from the window of a plane says we can't believe the results of your eyes anymore.
As another analogy, suppose I tell you that I've hidden a gold coin [I haven't, this is just a hypothetical example!] and if you can find it you can keep it. You ask me where you should start looking, hoping for a hint. It's going to be easier and faster for you to find it if I tell you I hid it in Natick, Massachusetts (where MathWorks headquarters is located) than if I tell you I hid it on Earth.
Similarly, if you tell integral to integrate your integrand over a small range where the function takes on non-zero values it'll do better than if you tell it to integrand over a huge range over most of which it returns zero.
For this specific integrand, though, there's another tool that may be more appropriate: the erf function.

Accedi per commentare.

Prodotti


Release

R2014a

Community Treasure Hunt

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

Start Hunting!

Translated by