2

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!

  • 1
    Related: https://mathematica.stackexchange.com/questions/258132/is-it-possible-to-write-a-mma-version-of-lineint-like-maple – Michael E2 Dec 03 '21 at 18:11

4 Answers4

3

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 *)
Daniel Huber
  • 51,463
  • 1
  • 23
  • 57
2

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  *)
Michael E2
  • 235,386
  • 17
  • 334
  • 747
  • Thaks, it is clear and it works! – Lorenzo Bagnasacco Dec 05 '21 at 15:24
  • 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 – lotus2019 Mar 03 '22 at 08:15
  • @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. – Michael E2 Mar 03 '22 at 16:14
  • OK. Thanks a lot! @Michael E2 – lotus2019 Mar 04 '22 at 03:05
1

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

user64494
  • 26,149
  • 4
  • 27
  • 56
  • It's clear such approach works for more complex closed paths too, – user64494 Dec 03 '21 at 18:03
  • 1
    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) ? – Lorenzo Bagnasacco Dec 04 '21 at 11:17
1

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

Akku14
  • 17,287
  • 14
  • 32