# Finding the enclosed area between two equations

I have to find the sum of the enclosed area between two functions that goes from 0 to 2pi:

f[x_]:=x-(1+3/2)Sin(2x);
g[x_]:=(x/2)-8Cos(3x-pi/19);
Plot[{f[x],g[x]},{x,0,2pi}, PlotLegends->"Expressions"]


I got the intersections by using NSolve:

{a,b,c,d,e,f}:=x/.NSolve[{f[x]==g[x]&&0<x<2pi},x,Reals]


But when I use ImplicitRegion and try to get the area between the two lines, I get nothing usefull for RegionUnion or RegionPlot. I do a ImplicitRegion between every intersection (0-a, a-b, b-c and so on to f-2pi) and then try to plot the Region.

r1=ImplicitRegion[g[x]<y&&f[x]>y,{{x,0,a},y}]
r2=ImplicitRegion[g[x]<y&&f[x]>y,{{x,a,b},y}]
Area@RegionUnion[r1,r2]
RegionPlot[{r1, r2}, PlotRange -> {{0, 2pi}, {-8, 10}}]


But Mathematica enters an infite loop which I have to abort. What is it that I'm missing?

Clear["Global*"]

f[x_] := x - (1 + 3/2) Sin[2 x];
g[x_] := (x/2) - 8 Cos[3 x - Pi/19];

area = Integrate[Abs[f[x] - g[x]], {x, 0, 2 Pi}] // N

(* 33.9833 *)

pts = {x, f[x]} /. NSolve[{f[x] == g[x], 0 < x < 2 Pi}, x];

Plot[{f[x], g[x]}, {x, 0, 2 Pi}, PlotLegends -> "Expressions",
Filling -> 1 -> {2},
Epilog -> {Red, AbsolutePointSize, Point[pts]}] intervals =
Between[x, #] & /@ Partition[{0, First /@ pts, 2 Pi} // Flatten, 2, 1] //
Simplify

(* {0 <= x <= 0.500703, 0.500703 <= x <= 1.58895, 1.58895 <= x <= 2.80023,
2.80023 <= x <= 3.73933, 3.73933 <= x <= 4.91606, 4.91606 <= x <= 5.57567,
5.57567 <= x <= 2 π} *)

areafg = Total[
Area[ImplicitRegion[g[x] < y < f[x] && #, {x, y}]] & /@
intervals[[1 ;; ;; 2]]]

(* 21.9265 *)

areagf = Total[
Area[ImplicitRegion[f[x] < y < g[x] && #, {x, y}]] & /@
intervals[[2 ;; ;; 2]]]

(* 12.0569 *)


Summing the two areas

area = areafg + areagf

(* 33.9833 *)


This agrees with direct integration

First, constants in MMA are always capitalized and function arguments are enclosed in square brackets.

I assume that you want the area between the two functions like:

f[x_] := x - (1 + 3/2) Sin[2 x];
g[x_] := (x/2) - 8 Cos[3 x - Pi/19];
Plot[{f[x], g[x]}, {x, 0, 2 Pi}, PlotLegends -> "Expressions",
Filling -> {1 -> {2}}] This can be calculated by the integral of the absolute value of the difference:

Integrate[Abs[f[x] - g[x]], {x, 0, 2 Pi}] // N
(* 33.9833 *)


In r2 we should use g[x] > y && f[x] < y instead of g[x]<y&&f[x]>y. And we also need to replace {a, b, c, d, e, f} by {a, b, c, d, e, ff} etc.

f[x_] := x - (1 + 3/2) Sin[2 x];
g[x_] := (x/2) - 8 Cos[3 x - Pi/19];
Plot[{f[x], g[x]}, {x, 0, 2 Pi}, PlotLegends -> "Expressions"]
{a, b, c, d, e, ff} =
x /. NSolve[{f[x] == g[x] && 0 < x < 2 Pi}, x, Reals]
r1 = ImplicitRegion[g[x] < y && f[x] > y, {{x, 0, a}, y}];
r2 = ImplicitRegion[g[x] > y && f[x] < y, {{x, a, b}, y}];
Area@RegionUnion[r1, r2]
RegionPlot[{r1, r2}, PlotRange -> {{0, 2 Pi}, {-8, 10}}]
` 