# Evaluating a line integral of a vector field numerically

I have a vector field (velocity) and I want to integrate around a rectangle. If I had a function for the field it would be easy and I know how to do it. However, I only have the coordinates. Is there an easy way of doing this?

I thought about how I'm going to solve it, started writing the steps for the solution: parametrise each line, find the derivative of the parametrisation. However, I got stuck because in the integral, the field has to be evaluated at the parametric function.

\begin{align} \int_C \vec F \cdot d\vec r &= \int_C \vec F \cdot \vec T \: ds \\ &= \int_a^b \vec F\left(\vec r(t)\right) \cdot \frac{\vec r'(t)}{\left\lVert \vec r'(t) \right\rVert} \left\lVert \vec r'(t) \right\rVert \: dt \\ &= \int_a^b \vec F\left(\vec r(t)\right) \cdot \vec r'(t) \: dt \end{align}

The first part of the last line is where I got stuck, t would be involved and I don't know how to do it numerically.

***** Side note, I'm trying to calculate circulation which can also be found by calculating the surface integral of the curl of velocity (vorticity), but I guess that would be even harder *numerically***

I use MatLab but anything that would get me started would do

Thanks

Edit: The above equation was copied from a website, let me use my own notation:

I have a vector field $\vec V = \vec V(x,y)$ which has an x component $u$ and a y component $v$.

$\Rightarrow \vec V(x,y) = u(x,y) \; \vec i + v(x,y) \; \vec j$

I want to integrate along C which is a rectangle so I will have to divide the integration into 4 parts (4 smooth curves).

The vertices of the rectangle are $(0.0274,0.0386)\: (0.0926,0.0386) \: (0.0926,0.1184) \: (0.0274,0.1184)$

So the four curves are: \begin{align} C_1: \: \: r_1(t) = t \; \vec i + 0.0386 \; \vec j \hspace{2cm} for\hspace{1cm} 0.0274 \le t \ge 0.0926 \\ C_2: \: \: r_2(t) = 0.0926 \; \vec i + t \; \vec j \hspace{2cm} for\hspace{1cm} 0.0386 \le t \ge 0.1184 \\ -C_3: \: \: r_3(t) = t \; \vec i + 0.1184 \; \vec j \hspace{2cm} for\hspace{1cm} 0.0274 \le t \ge 0.0926 \\ -C_4: \: \: r_4(t) = 0.0274 \; \vec i + t \; \vec j \hspace{2cm} for\hspace{1cm} 0.0386 \le t \ge 0.1184 \end{align} and the derivatives: \begin{align} r_1'(t) &= 1 \; \vec i + 0 \; \vec j \\ r_2'(t) &= 0 \; \vec i + 1 \; \vec j \\ r_3'(t) &= 1 \; \vec i + \; \vec j \\ r_4'(t) &= 0 \; \vec i + 1 \; \vec j \end{align} then: \begin{align} \Gamma = \int_C \vec V(x,y) \cdot d\vec r &= \\ &= \int_{C_1} \vec V(x,y) \cdot d\vec r + \int_{C_2} \vec V(x,y) \cdot d\vec r - \int_{-C_3} \vec V(x,y) \cdot d\vec r - \int_{-C_4} \vec V(x,y) \cdot d\vec r \\ &= \int^{b_1}_{a_1} \vec V(\vec r_1(t)) \cdot r_1'(t) dt + \int^{b_2}_{a_2} \vec V(\vec r_2(t)) \cdot r_2'(t) dt \\ &- \int^{b_3}_{a_3} \vec V(\vec r_3(t)) \cdot r_3'(t) dt -\int^{b_4}_{a_4} \vec V(\vec r_4(t)) \cdot r_4'(t) dt \end{align}

This is where I got to, I know that $\vec V(\vec r(t)) = \vec V(x(t),y(t))$, here I cannot think of a way of integrating numerically (mainly the $t$ term in $r(t)$ confuses me). $x$ and $y$ are 2-D grids (a 41 by 35 matrix) where the columns in x are equal and the rows in y are equal. x-component $u(x,y)$ is also a 41 by 35 matrix and so is the y-component $v(x,y)$

-
I can't post images yet! here are the two images: i.stack.imgur.com/soxfY.png i.stack.imgur.com/CxJUj.gif – pythonista Feb 24 '12 at 20:03
So the function $F$ is sampled on an evenly spaced grid? Do you have an analytic expression for $C$? If this is the case, fit an interpolating polynomial to $F(r(t))$ and compute $r'(t)$ analytically, then integrate the product of these two functions numerically along the interval $[a,b]$. – dls Feb 24 '12 at 20:26
I added the image and the equation to your post. How exactly do you have your vector field $F$? Is it a pair of rectangular arrays for the $x$ and $y$ components? – Rahul Feb 24 '12 at 20:26
Thanks for editing the question. The vector F is as @dls wrote; it is sampled on an evenly spaced grid. The x and y are given as if produced by the meshgrid function (Rectangular grid in 2-D and 3-D space) While the components of the vector are ud and vd, the first is the x component and the second is the y component. *C is the rectangle, and I do have the vertices. – pythonista Feb 24 '12 at 23:45
i am stuck with same proble. can you share the code or algo – user77073 May 9 '13 at 19:45

Your work looks pretty good, but you can actually take it a step further since you know $r_i'(t)$. When you substitute in this information, each integral depends only on one component of $\vec V$, but not both. For instance $$\int^{b_1}_{a_1} \vec V(\vec r_1(t)) \cdot r_1'(t) \; dt =\int^{b_1}_{a_1} u(\vec r_1(t)) \; dt$$
The next task is to write a routine to implement the function $\vec V$, that is, you want a function from $\mathbb{R}^2\rightarrow\mathbb{R}^2$. Use something like interp2 (though this may not be the most efficient). Once you have this accomplished, simply evaluate the now 1-d integrals using the quad function, for instance.
Thanks a lot. I used interp2 but I had no idea how to use its output. This lead me to something easier: the surface fitting tool. I fitted the surface then used feval to evaluate the fit at $r(t)$ and finally used trapzto evaluate the integral. However, feval returns lots of NaN, when I increase the number of evaluation points it gets better. Anyway, as you said, it's not efficient. I did it the way I thought it would be harder; surface integrals. The sfit object can be integrated using quad2d. So I integrated the curl of $\vec V$ over the rectangle and I got better results. – pythonista Feb 26 '12 at 2:33