Integrating Mathematical Functions in Mathematica

  • Context: Mathematica 
  • Thread starter Thread starter Rasalhague
  • Start date Start date
  • Tags Tags
    Integration Mathematica
Click For Summary

Discussion Overview

The discussion revolves around the integration of a scalar field over the top half of a unit sphere using Mathematica. Participants explore different methods of setting up and executing the integral, including parameterization of the surface and the implications of using nested integrals versus single integrals.

Discussion Character

  • Technical explanation
  • Mathematical reasoning
  • Debate/contested

Main Points Raised

  • One participant describes their approach to integrating the scalar field and provides the Mathematica code they used, noting that the simplified expression worked with both integration methods.
  • Another participant suggests that the issue with the nested integration may stem from Mathematica treating theta as a complex variable when not explicitly constrained, leading to more complicated outputs.
  • A later reply recommends using a single Integrate command for efficiency and provides timing comparisons for different integration methods in Mathematica.
  • Another participant shares their timing results for various integration methods, indicating variability in performance across different approaches.

Areas of Agreement / Disagreement

Participants express differing views on the efficiency of integration methods and the behavior of Mathematica with respect to variable assumptions. There is no consensus on the best approach, as participants share varying experiences and results.

Contextual Notes

Some limitations are noted regarding the assumptions made about the variables in the integration process, particularly concerning the treatment of theta in nested integrals.

Who May Find This Useful

This discussion may be useful for Mathematica users interested in integration techniques, particularly in the context of parameterized surfaces and scalar fields.

Rasalhague
Messages
1,383
Reaction score
2
I'm new to Mathematica. I used it to integrate the scalar field

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

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

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

so that

f(\phi(r,\theta))=1-r^2.

I set up the integral like this:

\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}

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[r*Sqrt[1-r^2],{theta,0,2*Pi},{r,0,1}]

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].

Integrate[
Integrate[
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?
 
Physics news on Phys.org
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:
Integrate[..., {r, 0, 1}, Assumptions -> {0 <= theta <= 2*Pi}]
or even
Code:
Integrate[..., {r, 0, 1}, Assumptions -> {Element[theta, Reals]} ]

then it quickly spits out 1/3 here
 
Addendum:

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

Code:
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[
  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:
In[5]:= NIntegrate[
  phi[[3]]^2*Norm[Cross[D[phi, r], D[phi, theta]]], {r, 0, 1}, {theta,
    0, 2 \[Pi]}] // Timing
%[[2]]/Pi

Out[5]= {0.469, 2.0944}

Out[6]= 0.666667
 
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[ ,{ }],{ }]
 

Similar threads

  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 1 ·
Replies
1
Views
3K
Replies
5
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 19 ·
Replies
19
Views
3K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 1 ·
Replies
1
Views
1K