You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Matlab中含Dirac Delta函数的二重积分计算异常问题咨询

Why Your MATLAB Integral Returns Zero (vs. Wolfram Alpha)

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) and sind(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) and sin(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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 06:51:20