Let's consider a 2D smooth surface embedded in 3D shown in the figure below. The corresponding metric tensor in Cartesian coordinates is \begin{equation} h = \biggr(\matrix{1 + \frac{A^2\pi^2}{4} \sin^2(\pi x/2) \ \ \ \ \ -\frac{A^2\pi^2}{4} \sin(\pi x/2)\cos(\pi y/2)\\ -\frac{A^2\pi^2}{4}\sin(\pi x/2)\cos(\pi y/2) \ \ \ \ \ \ \ \ 1 + \frac{A^2\pi^2}{4} \cos^2(\pi y/2)}\biggr) \end{equation} where $A=0.1$ is a constant setting the height of the surface oscillations.

enter image description here

The surface is given by the following set of points in Cartesian coordinates: $(x,y, A [\cos(\pi x/2) + \sin(\pi y/2)])$. You can plot it using ParametricPlot3D.

What I'd like to do is to compute the area of the portion of surface enclosed in each of the loops shown in the figure. There's not an analytical expression for such curves and I have saved them in separate files. Each file contains a list of points with 4 columns (ignore the first one, the last three are for x,y,z coordinates). The number or rows in each file is ~3000. Here's the link to a sample curve you can use https://drive.google.com/file/d/1fePyxrZ_4eyrQPPr0Y_pbHapjxYd2RYs/view?usp=sharing .

It is known (https://en.wikipedia.org/wiki/Metric_tensor) that the integral of the sqrt of the determinant of the metric is a measure of a portion of area on the specific surface, provided the correct boundaries for the integration.

So far, I've therefore tried to do so using the following code in Mathematica (V12):

NIntegrate[Sqrt[Det[h[x, y]]], {x, y} \[Element] DiscretizeGraphics[Polygon[curve[[All, 2;; 3]]]]]

where "curve" is the table containing the coordinates of the boundary points of the region. First of all, is this the right way to do so? In either case, this little chunk of code takes ages to give a result (hours on my laptop!) and sometimes Mathematica just doesn't compute anything at all returning no error.

Is there a smarter/more efficient/more general way to do so? Thanks for your help!

  • 1
    $\begingroup$ Are you able to give a sample curve for people to test? $\endgroup$ May 31, 2022 at 15:00
  • $\begingroup$ I have now edited the text adding some details that should allow people to test their code. Thanks for the remark. $\endgroup$
    – lorenzop
    May 31, 2022 at 15:15
  • $\begingroup$ Do you also have an expession for the surface itself? $\endgroup$ May 31, 2022 at 16:19
  • $\begingroup$ Added in the text, below the figure. Thanks. $\endgroup$
    – lorenzop
    May 31, 2022 at 16:30
  • $\begingroup$ How did you define h[x, y] and curve in Mathematica? You're using the 2nd and 3rd coordinates of curve but the text of the question implies it should be the 1st and 2nd coordinates. (BTW, I prefer data I can import with Import["url"] when possible.) $\endgroup$
    – Michael E2
    May 31, 2022 at 17:34

1 Answer 1


As long as I've transcribed h correctly, I think this might be a case of just tidying up your integrand before putting it into NIntegrate. All I do is use Simplify.

A = 0.1;

h[x_, y_] :=
{{1 + (A^2 Pi^2)/4 * Sin[Pi x/2]^2, -(A^2 Pi^2)/4 * Sin[Pi x/2] Cos[Pi y/2]},
 {-(A^2 Pi^2)/4 * Sin[Pi x/2] Cos[Pi y/2], 1 + (A^2 Pi^2)/4 * Cos[Pi y/2]^2}}

(* Note that the following step (generating the region) takes
   some amount of time. I separated it just to illustrate that. *)

reg = DiscretizeGraphics[Polygon[curve[[All, 2 ;; 3]]]]

NIntegrate[Sqrt@Det@h[x, y] // Simplify, {x, y} \[Element] reg]

(* 0.1603455524 *)

This returns in several seconds for me.

Note that not using Simplify causes NIntegrate to simply return unevaluated! Using Simplify and/or FullSimplify on your expressions can work wonders when feeding them to integration functions.

There might well be better ways to do this in general though, so I'd keep the question open to see if anyone else responds.

  • $\begingroup$ This actually helps giving the value! I had never thought about using Simplify inside an integral before. Thanks a lot! Let's see if other people have better ways, but this is already good. $\endgroup$
    – lorenzop
    Jun 1, 2022 at 15:47

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.