Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Integration in Mathematica

  1. Jul 24, 2010 #1
    I'm new to Mathematica. I used it to integrate the scalar field

    [tex]f:\mathbb{R}^3 \to \mathbb{R} \; \bigg| \;f(x,y,x)=z^2[/tex]

    over the top half of a unit sphere centered on the origin, paramaterising this surface with

    [tex]\phi: \mathbb{R}^2 \rightarrow \mathbb{R}^3 \; \bigg| \; \phi(r,\theta)=(r \cos \theta, r \sin \theta, \sqrt{1-r^2})[/tex]

    so that


    I set up the integral like this:

    [tex]\int_R f(\phi(r,\theta))\left \| \partial_r \phi \times \partial_\theta \phi \right \| dr d\theta = \int_0^{2\pi} \int_0^1 r \sqrt{1-r^2} \; dr d\theta = \frac{2 \pi}{3}[/tex]

    where the partial sign (curly d) with subscript variable stands for the partial derivative with respect to that variable, ||v|| denotes the norm (a.k.a. magnitude) of a vector v, and the times symbol, x, is the cross product of vectors.

    In Mathematica, I was able to calculate this as follows:

    phi={r*Cos[theta],r*Sin[theta],Sqrt[1-r^2]}; Integrate[phi[[3]]^2*Norm[Cross[D[phi,r],D[phi,theta]]],{theta,0,2*Pi},{r,0,1}]

    and also, in the following two different ways, by plugging in the already simplified integrand:


    Integrate[Integrate[r*Sqrt[1 - r^2], {r, 0, 1}], {theta, 0, 2*Pi}]

    But the last of these methods didn't work when I used the unsimplified expression phi[[3]]^2*Norm[Cross[D[phi,r],D[phi,theta]]] in place of r*Sqrt[1 - r^2].

    phi[[3]]^2*Norm[Cross[D[phi, r], D[phi, theta]]], {r, 0,
    1}], {theta, 0, 2*Pi}]

    It took a long time to calculate, then produced many lines of complicated symbolic expressions involving complex numbers and hyperbolic trig functions. A similar thing happened when I asked it to calculate just the inner integral:

    Integrate[phi[[3]]^2*Norm[Cross[D[phi, r], D[phi, theta]]], {r, 0, 1}]

    Can anyone tell me what went wrong: why the simplified expression worked with both methods, Integrate[ ,{ },{ }] and Integrate[Integrate[ ,{ }],{ }], but the equivalent full one only worked by the first method, Integrate[ ,{ },{ }]? Something to do with the order of operations that leads it to try dividing by something unpleasant, or am I making an elementary syntactic mistake?
  2. jcsd
  3. Jul 24, 2010 #2


    User Avatar
    Science Advisor
    Homework Helper

    The reason is probably that, with the Integrate[..., {}, {}] syntax, Mathematica looks ahead to see what kind of object theta will be when performing the r-integration. Apparently, when you just do the r-integration without telling it anything about theta, it will assume that it can be any complex variable, and it has to apply all kinds of generalities to write the answer in such a way that it is valid, for example, for theta = 3 + 2i as well.

    You can easily solve the problem by telling it what theta will be, like so:
    Code (Text):
    Integrate[..., {r, 0, 1}, Assumptions -> {0 <= theta <= 2*Pi}]
    or even
    Code (Text):
    Integrate[..., {r, 0, 1}, Assumptions -> {Element[theta, Reals]} ]
    then it quickly spits out 1/3 here
  4. Jul 24, 2010 #3


    User Avatar
    Science Advisor
    Homework Helper


    It is better to use a single Integrate command whenever possible, it seems a bit more efficient:

    Code (Text):

    In[1]:= phi = {r*Cos[theta], r*Sin[theta], Sqrt[1 - r^2]};

    In[2]:= Integrate[
      phi[[3]]^2*Norm[Cross[D[phi, r], D[phi, theta]]], {r, 0, 1}, {theta,
        0, 2 \[Pi]}] // Timing

    Out[2]= {0.546, (2 \[Pi])/3}

    In[2]:= Integrate[
       phi[[3]]^2*Norm[Cross[D[phi, r], D[phi, theta]]], {r, 0, 1},
       Assumptions -> Element[theta, Reals]], {theta, 0,
       2 \[Pi]}] // Timing

    Out[2]= {0.796, (2 \[Pi])/3}
    Actually, if you don't care about the exact result, NIntegrate is even a tad quicker than thát
    Code (Text):

    In[5]:= NIntegrate[
      phi[[3]]^2*Norm[Cross[D[phi, r], D[phi, theta]]], {r, 0, 1}, {theta,
        0, 2 \[Pi]}] // Timing

    Out[5]= {0.469, 2.0944}

    Out[6]= 0.666667
  5. Jul 24, 2010 #4
    Thanks CompuChip. My timings vary a bit. (I'm using Mathematica 7.0.) Taking the average over 10 tries, I got

    0.2814 for Integrate[ ,{ },{ }]
    0.57 for Integrate[Integrate[ ,{ }],{ }]

    0.3453 for NIntegrate[ ,{ },{ }]
    0.8845 for NIntegrate[Integrate[ ,{ }],{ }]
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook