Matlab中含Dirac Delta函数的二重积分计算异常问题咨询
Hey there, let's break down why you're seeing a zero result in MATLAB but a correct one in Wolfram Alpha—there are two key issues at play here:
1. Numerical vs. Symbolic Integration Differences
Wolfram Alpha uses symbolic computation to handle integrals with Dirac Delta functions. It can analytically recognize that the integral of $\delta(f(x,y))$ over a region corresponds to a line integral along the curve $f(x,y)=0$ (scaled by the reciprocal of the gradient's magnitude), so it can compute the exact result.
On the other hand, MATLAB's integral2 is a numerical integration tool. Numerical methods work by sampling the function at discrete points across the domain. Since the Dirac Delta function is only non-zero on a single zero-width curve (a straight line in your case), the chance of the numerical sampler hitting exactly that line is effectively zero—hence the result of 0.
2. Potential Angle Unit Confusion (Double-Check This!)
While the above is the main culprit, let's confirm angle units aren't adding extra confusion:
- In MATLAB,
cosd(135)andsind(135)calculate cosine/sine using degrees (so $\text{cosd}(135) = -\sqrt{2}/2 \approx -0.7071$, $\text{sind}(135)=\sqrt{2}/2 \approx 0.7071$). - In Wolfram Alpha,
cos(135)andsin(135)default to radians. 135 radians is equivalent to ~21 full circles plus ~3 radians, so $\text{cos}(135) \approx -0.9999$ and $\text{sin}(135) \approx -0.0523$. This defines a line that only touches your integration domain $[0,40] \times [0,60]$ at the origin. If you intended to use 135 degrees in Wolfram Alpha, you need to specify it explicitly (e.g.,cos(135 degrees)), otherwise the two integrals are actually computing entirely different things!
How to Fix This in MATLAB
To get the correct result in MATLAB, switch to symbolic integration using the Symbolic Math Toolbox, just like Wolfram Alpha does. Here's the working code:
syms x y % Define the Dirac Delta function argument (using degrees, matching your original code) f = x*cosd(135) + y*sind(135); % Compute the double integral symbolically q = int(int(dirac(f), x, 0, 40), y, 0, 60); % Convert the symbolic result to a numeric value if needed q_num = double(q);
This will correctly evaluate the integral by leveraging symbolic math instead of numerical sampling.
内容的提问来源于stack exchange,提问作者Olli Ver

