2
$\begingroup$

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!

$\endgroup$
1

4 Answers 4

3
$\begingroup$

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]

enter image description here

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 *)
$\endgroup$
5
  • 1
    $\begingroup$ I think it doesent'work because I need an 'oriented' integral, so the exact result of your example will be 3 and not -2. $\endgroup$ Dec 3, 2021 at 16:47
  • $\begingroup$ I suppose, like you do previously with function Line, the path is anti-clockwise. $\endgroup$ Dec 3, 2021 at 16:54
  • 1
    $\begingroup$ You are right, Region does not bother about direction. But you may put the direction into the function if you want to use Region. $\endgroup$ Dec 3, 2021 at 17:24
  • $\begingroup$ 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 ? $\endgroup$ Dec 4, 2021 at 11:14
  • $\begingroup$ I only know the classic way: {D[f[x,y],x],D[f[x,y],y]}.{dx,dy} $\endgroup$ Dec 4, 2021 at 11:19
2
$\begingroup$

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  *)
$\endgroup$
4
  • $\begingroup$ Thaks, it is clear and it works! $\endgroup$ Dec 5, 2021 at 15:24
  • $\begingroup$ Your method is very good. But I want to ask, is this integral path limited to the boundary of a closed region? What if the integral path is an unclosed directed curve? For example: integral path c[t_] := {t^2, t}, from point (1,-1) to point (1,1). Function: V[{x_, y_}] := {x*y, 0}. @Michael E2 $\endgroup$
    – lotus2019
    Mar 3, 2022 at 8:15
  • $\begingroup$ @lotus2019 It works with Line (geometric) paths. For parametrized path perhaps you want something like this: c[t_] := {t^2, t}; vf = {y, -x}; Integrate[vf . Dt[{x, y}, t] /. Thread[{x, y} -> c[t]], {t, 0, 1}]. But that won't work if c is defined c[t_?NumericQ], which is harder for several reasons; since it would seem to require numeric integration and not symbolic, it is perhaps not really an issue. $\endgroup$
    – Michael E2
    Mar 3, 2022 at 16:14
  • $\begingroup$ OK. Thanks a lot! @Michael E2 $\endgroup$
    – lotus2019
    Mar 4, 2022 at 3:05
1
$\begingroup$

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

$\endgroup$
2
  • $\begingroup$ It's clear such approach works for more complex closed paths too, $\endgroup$
    – user64494
    Dec 3, 2021 at 18:03
  • 1
    $\begingroup$ 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) ? $\endgroup$ Dec 4, 2021 at 11:17
1
$\begingroup$

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
$\endgroup$

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.