Integrating Mathematical Functions in Mathematica: A Step-by-Step Guide

In summary: I'm not sure how to make sense of the NIntegrate[Integrate[...], ] expression, so I left it out of the average. The first of the NIntegrate[] expressions is faster than the second. My hypothesis about why the Integrate[] expression is faster is that the cross product is being simplified during the integration, because it knows that r is real. I think I am not making sense of the Integrate[Integrate[...],{ }] expression, because I'm not specifying any assumptions, either by putting the Assumptions inside the Integrate[] expression or by using a global command like Assuming[].I wouldn't worry too much about the difference between the Integrate and NIntegrate timings, they are pretty close.
  • #1
Rasalhague
1,387
2
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

[tex]f(\phi(r,\theta))=1-r^2.[/tex]

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[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
  • #2
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
 
  • #3
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
 
  • #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[ ,{ }],{ }]
 
  • #5


I am impressed by your use of Mathematica to integrate this scalar field over a parametrized surface. It appears that you have a good understanding of how to set up the integral and use the appropriate notation. Your explanation of the different methods you used in Mathematica is clear and well-organized.

Regarding your question about why the simplified expression worked with both methods but the full one only worked with the first method, it is possible that the second method is attempting to perform a more complex integration process and encountering difficulties due to the complexity of the expression. It could also be a syntax error, as you mentioned. I would recommend consulting Mathematica documentation or seeking help from a more experienced user to determine the exact issue.

Overall, it is impressive to see how Mathematica can efficiently calculate this integral and provide a clear result. Keep exploring the capabilities of this powerful tool in your scientific research.
 

What is Mathematica?

Mathematica is a computer program used for mathematical and scientific computations. It is widely used in various fields, including physics, engineering, and economics.

What is the purpose of integrating mathematical functions in Mathematica?

The purpose of integrating mathematical functions in Mathematica is to obtain the exact or approximate value of a definite or indefinite integral. This can be useful in solving various mathematical problems and in analyzing physical systems.

How do I input a mathematical function in Mathematica?

To input a mathematical function in Mathematica, you can use the built-in function "Integrate" or "NIntegrate" followed by the function you want to integrate and the appropriate limits. Alternatively, you can define your own function and then use it in the integration process.

Can Mathematica handle complex mathematical functions?

Yes, Mathematica can handle complex mathematical functions. It has built-in functions for working with complex numbers and can perform complex integration and differentiation.

Is it necessary to know programming to integrate mathematical functions in Mathematica?

No, it is not necessary to know programming to integrate mathematical functions in Mathematica. The program has a user-friendly interface and provides helpful documentation to guide you through the integration process. However, knowing basic programming concepts can be beneficial in utilizing Mathematica's advanced features.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
212
Replies
3
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
6
Views
5K
  • Calculus and Beyond Homework Help
Replies
3
Views
547
  • Special and General Relativity
Replies
5
Views
333
  • Advanced Physics Homework Help
Replies
10
Views
2K
  • Introductory Physics Homework Help
Replies
6
Views
1K
  • Advanced Physics Homework Help
Replies
1
Views
391
  • Calculus and Beyond Homework Help
Replies
7
Views
971
Back
Top