# Fast computation of the area of a surface using MATLAB

I've written an algorithm in MATLAB which has to be real time (by real time I mean anything less than 1 s is perfect for my purpose and less than 10 s is still acceptably good). My algorithm contains a for loop which at each iteration it has to compute a certain number of areas. Each of these areas are computed using the following formula: $$\begin{equation} A = \int_0^1\int_0^1 \sqrt{\det (g)}\ d u\ d v \end{equation}$$ where $$g$$ is a $$2\times 2$$ matrix with polynomial entries (either linear or quadratic in variables $$u$$ and $$v$$). I already have managed to do that in Matlab in two ways as I explain below but both are very slow plus the fact that one of them has one additional problem in some special cases. In the first attempt I wrote the following:

syms u v real
volume = sqrt(det(g));
area(i) = int(int(volume, u,0,1),v,0,1); # area of the i-th ruled surface


in this case the area is computed but its extremely slow. The other way that I thought solving it was to do the following:

syms u v real
volume = sqrt(det(g));
h = matlabFunction(volume);
area(i) = integral2(h,0,1,0,1);


This method makes it a bit faster (but noway close to being fast enough). It also has one additional problem, in some rare cases when computing the determinant of $$g$$, the polynomial obtained is univariate (depends on either $$u$$ or $$v$$) and when I use the matlabFunction command it only assigns one variable to it (@(u) or @(v)) which makes problems in the next line (as integral2 doesn't recognize the other variable which is undefined by the matlabFunction). So my Questions are:

1. Is there a faster numerical-way to compute these integrals (or does Matlab have some special functions for a similar purpose)? regarding the accuracy, it is ok if its accurate up to 3 or 4 digits.
2. How can I force matlabFunction command to introduce the other variable in the case where det(induced_metric) is a univariate polynomial.

Thank you in advance,

Here I add the extra information regarding the ruled surfaces that I want to compute the area of. Each of these are 2-dimensional surfaces originally in $$\mathbb{R}^6$$ associated with a metric $$g^\prime$$. Lets say each one of them is an enclosed area by the points $$A$$, $$B$$, $$P$$ and $$Q$$. Then I have the following parametrization for each of them: $$\begin{equation} X:\ (u,v) \longrightarrow \left( \begin {array}{c} \left( \left( a_{{1}}-b_{{1}}-p_{{1}}+q_{{ 1}} \right) v+p_{{1}}-a_{{1}} \right) u+ \left( -a_{{1}}+b_{{1}} \right) v+a_{{1}}\\ \left( \left( a_{{2}}-b_{{2}} -p_{{2}}+q_{{2}} \right) v+p_{{2}}-a_{{2}} \right) u+ \left( -a_{{2}}+ b_{{2}} \right) v+a_{{2}}\\ \left( \left( a_{{3}}- b_{{3}}-p_{{3}}+q_{{3}} \right) v+p_{{3}}-a_{{3}} \right) u+ \left( -a _{{3}}+b_{{3}} \right) v+a_{{3}}\\ \left( \left( a _{{4}}-b_{{4}}-p_{{4}}+q_{{4}} \right) v+p_{{4}}-a_{{4}} \right) u+ \left( -a_{{4}}+b_{{4}} \right) v+a_{{4}}\\ \left( \left( a_{{5}}-b_{{5}}-p_{{5}}+q_{{5}} \right) v+p_{{5}}-a_{{ 5}} \right) u+ \left( -a_{{5}}+b_{{5}} \right) v+a_{{5}} \\ \left( \left( a_{{6}}-b_{{6}}-p_{{6}}+q_{{6}} \right) v+p_{{6}}-a_{{6}} \right) u+ \left( -a_{{6}}+b_{{6}} \right) v+a_{{6}}\end {array} \right) \end{equation}$$ Then in order to compute the area of the surface I have to obtain the induced metric on it, which earlier I named it $$g$$, I obtained it by computing $$\frac{\partial X}{\partial u}$$ and $$\frac{\partial X}{\partial v}$$, and then $$\begin{eqnarray} g_{1,1} = \langle\frac{\partial X}{\partial u}, \frac{\partial X}{\partial u} \rangle ,\\ g_{1,2} = g_{2,1} = \langle\frac{\partial X}{\partial u}, \frac{\partial X}{\partial v} \rangle ,\\ g_{2,2} = \langle\frac{\partial X}{\partial v}, \frac{\partial X}{\partial v} \rangle . \end{eqnarray}$$ where $$\langle - , - \rangle$$ is the scalar product computed with respect to the metric $$g^\prime$$ (which is symmetric and positive-definite).

• Did you try to explicitly provide the metric matrix without using any symbolic variables? It might be that there is still some substantial overhead even after using matlabFunction. – LutzL Jan 20 at 16:15
• @LutzL Unfortunately it is not possible to define it without syms, since each entry of the induced_metric matrix is of degree 2 depending on u and v. If it was of one variable it was then possible to define the polynomial with just a matrix in matlab. But I have no idea how to do it for polynomials with 2 or more variables. – Arvin Rasoulzadeh Jan 20 at 16:34
• I do not understand your restriction. function z=f(x,y) z=x.^3+x.*y.^2+y.^5; end is a polynomial in two variables. The conversion of symbolic derivatives requires more effort, if such are contained in the metric computation. – LutzL Jan 20 at 17:02
• @LutzL I didn't get what you mean well. Are you suggesting writing volume as function of u and v by defining a range for these variables (like u = 0:0.01:10)? If thats so it is not possible for me as I do not have the formula for the volume. I actually have d_u and d_v (which are two vectors) and then I have the metric_tensor of $\mathbb{R}^6$, then I obtain the induced_metric by computing each entry, Like induced_metric(1,2) = d_x_u' * metric_tensor * d_x_v;. The formula for the volume would be too long to be implemented directly in front of the integral. – Arvin Rasoulzadeh Jan 20 at 18:29
• I do not know what level of vectorization will prove best. Usually more vectorization is faster, at least where linear operations are concerned. It might be that calling the integration on a vector of non-linear function orients its effort (adaptive sub-divisions) on the worst patch, doing too much work on easy patches relative to calling each integration separately. However, computing scalar products and norm squares can very well done simultaneously, as that is a homogeneous effort over all patches. – LutzL Jan 21 at 11:06