# Line integral over a rectangular path

I have a little problem. I need to calculate an line integral of a function on a rectangular oriented closed path. For example I define

f[x_,y_]:= x-2y


and the integral must be on a square like:

Integrate[f[0,y],{y,0,1}]+Integrate[f[x,1],{x,0,1}]+Integrate[f[1,y],{y,1,0}]+Integrate[f[x,0],{x,1,0}]


My question: is there a simply way to define an oriented rectangular path?

Thanks for any tips and helps!

• Dec 3 '21 at 18:11

You may use the function "Region" in 2 dimension (it also works in 1 and 3 dimensions) to specify the path in "Integrate". Here is an example:

reg = Region[Line[{{0, 0}, {1, 0}, {1, 1}, {0, 1}, {0, 0}}]]
f[x_, y_] = x - 2 y;
Integrate[f[x, y], {x, y} \[Element] reg]


This gives the integral along the NOT oriented path.

If you want to account for the direction, we need to trick a bit to use "Region", because "Region" does not bother about the direction. However, we may incorporate the direction into your function like e.g.:

f[x_, y_] = Piecewise[{
{x - 2 y, y == 0  || x == 1},
{-x + 2 y, y == 1  || x == 0},
}];
Integrate[f[x, y], {x, y} \[Element] reg]
(* 3 *)

• I think it doesent'work because I need an 'oriented' integral, so the exact result of your example will be 3 and not -2. Dec 3 '21 at 16:47
• I suppose, like you do previously with function Line, the path is anti-clockwise. Dec 3 '21 at 16:54
• You are right, Region does not bother about direction. But you may put the direction into the function if you want to use Region. Dec 3 '21 at 17:24
• Yes, now it is what i meant, it works. But there is a better way to define an oriented path, without creating an oriented function instead ? Dec 4 '21 at 11:14
• I only know the classic way: {D[f[x,y],x],D[f[x,y],y]}.{dx,dy} Dec 4 '21 at 11:19

My question: is there a simply way to define an oriented rectangular path?

RegionBoundary is an easy way to get the boundary of a rectangle with the positive (counterclockwise) orientation:

RegionBoundary[Rectangle[{0, 0}, {1, 1}]]
(*  Line[{{0, 0}, {1, 0}, {1, 1}, {0, 1}, {0, 0}}]  *)


The harder part was not asked about: How to compute the integral over an oriented path? The usual integral over an oriented path is a vector field, not a scalar field, so the question is a bit confusing. A scalar path integral is independent of orientation. The integral presented corresponds to the vector field {x - 2 y, x - 2 y}. To compute the integral of a vector field, you have to either parametrize the path or reduce the integral to a scalar integral. The following is a function that does the latter for a oriented path defined by Line[{p1, p2,...}]:

vfIntegrate // ClearAll;
(* integrate over a single line segment *)
vfIntegrate[vf_, i : {_, _} \[Element] Line[p_?MatrixQ]] /;
Length[p] == 2 :=
Integrate[vf . Normalize@First@Differences@p, i];
(* break path integral into line segments *)
vfIntegrate[
vf_, {x_, y_} \[Element] Line[p_?MatrixQ] /; Length[p] >= 3] :=
Total[vfIntegrate[vf, {x, y} \[Element] Line[#]] & /@
Partition[p, 2, 1]];


Example:

vfIntegrate[{x - 2 y, x - 2 y}, {x, y} \[Element]
RegionBoundary[Rectangle[{0, 0}, {1, 1}]]]
(*  3  *)

• Thaks, it is clear and it works! Dec 5 '21 at 15:24

One may apply Green's theorem to this end:

f[x_,y_]:= x-2y
Integrate[D[f[x, y], x] - D[f[x, y], y], {x, 0, 1}, {y, 0, 1}]


3

• It's clear such approach works for more complex closed paths too, Dec 3 '21 at 18:03
• Yes, I think it is more clean, but there is a way or a built-in function that allow to create an oriented path without defining an oriented function instead (or using operators on thath function) ? Dec 4 '21 at 11:17

Let me continue the very nice answer of @MichealE2 and show how to parametrise the path from one given point to the other for all points. As Micheal says, you in fact have a vector field.

f[x_, y_] = {x - 2 y, x - 2 y};

pts = {{0, 0}, {1, 0}, {1, 1}, {0, 1}, {0, 0}};

parp = Partition[pts, 2, 1]

pp = (#[[1]] + Subtract @@ (Reverse@#)*u) & /@ parp

ParametricPlot[pp, {u, 0, .95}, PlotStyle -> Thick]

Total[Integrate[f[Sequence @@ #]. D[#, u], {u, 0, 1}] & /@ pp]

(*   3   *)

Plot[Evaluate[f[Sequence @@ #]. D[#, u]], {u, 0, 1}] & /@ pp


Another example

f[x_, y_] = {x - 2 y, x - 2 y};

pts = Table[{Cos[t], Sin[t]}, {t, 0, 2 Pi, 2 Pi/24}];

ListPlot@pts

parp = Partition[pts, 2, 1];

pp = (#[[1]] + Subtract @@ (Reverse@#)*u) & /@ parp;

ParametricPlot[pp, {u, 0, .9}, PlotStyle -> Thick]

Total[Integrate[f[Sequence @@ #]. D[#, u], {u, 0, 1}] & /@ pp]

(*   -9 Sqrt[2] + 9 Sqrt[6]   *)

Plot[Evaluate[f[Sequence @@ #]. D[#, u]], {u, 0, 1}] & /@ pp