I am trying to visualize diffusion distribution from an equilateral triangle with 45 degrees. Assuming that diffusion is a Gaussian with sigma=1 this should do:

f[a_, b_, c_] := Integrate[E^(-x^2 - y^2), {x, -a, -a + c}, {y, -b, x - b + a}] / Pi

where {a,b} are the coordinates and c is the size of the triangle's side.

A quick sanity check seems reasonable:

f[0, 0, 10] // N

returns 0.125 as expected.

However, this command:

Plot3D[f[x, y, 10], {x, -3, 13}, {y, -3, 13}]

generates these errors:

Integrate::ilim: "Invalid integration variable or limit(s) in {-2.998856`,2.998856`,12.998856`}"

NIntegrate::dupv: "Duplicate variable -2.99886 found in NIntegrate[1.54405*10^-8,{-2.99886,2.99886,12.9989},{-2.99886,2.99886,-2.99886}]."

Integrate::ilim: Invalid integration variable or limit(s) in {-1.856,1.856,11.856}. 

NIntegrate::itraw: "Raw object -1.856 cannot be used as an iterator."

Could you tell what I'm doing wrong here?

  • 2
    $\begingroup$ I think that the dummy integration variables in the definition of f get mixed up with the later arguments of the same. You could define f using \[FormalX] and \[FormalY] as integration variables in order to avoid trouble. $\endgroup$ Commented Aug 24, 2016 at 16:48
  • 1
    $\begingroup$ @b.gatessucks is <strike>probably</strike> right here, since the plotting functions all use Block to scope their variables. I'm lazy though, so I go for xx and yy when I can't use x and y $\endgroup$
    – Jason B.
    Commented Aug 24, 2016 at 16:50
  • $\begingroup$ Thanks, @b.gatessucks, I think you are right. Changed x,y to xx,yy as @JasonB suggested, and now instead of outputting the errors it's running... and running... and still running... :-) $\endgroup$
    – Michael
    Commented Aug 24, 2016 at 17:04
  • 1
    $\begingroup$ @Michael, since you are mainly interested to plot, and not looking for an analytic solution, you could switch to numeric integration. ClearAll[f]; f[a_?NumericQ, b_?NumericQ, c_?NumericQ] := NIntegrate[E^(-x^2 - y^2), {x, -a, -a + c}, {y, -b, x - b + a}]/Pi, plots in a little under a minute $\endgroup$
    – Jason B.
    Commented Aug 24, 2016 at 17:15
  • 2
    $\begingroup$ a bit of an aside to the question, but its often worthwhile with try to do such multidimensional integrals as nested 1d integrals. In this case the integral over y can be done analytically (Integrate), then you are left with a much faster 1-d NIntegrate over x. $\endgroup$
    – george2079
    Commented Aug 24, 2016 at 19:26


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.