# Computing the area between two curves

I want to find the area in between the two curves whenever they overlap each other. I've tried multiple different formulas but none of them have been working. I have to find it using the integral. I also keep getting that dark bar all the way to the right of the notebook and don't know what that means. Any help would be greatly appreciated.

Thanks.

• You should use the stackexchange markup to make code that can be copied and pasted. Most people won't bother to type in formulae that you include as an image in order to help you troubleshoot. – djphd Dec 12 '15 at 22:11

Why not perform the integral, rather than discretizing regions?

f[x_] := E^(-x) (x^4 - 2 x^3 - x^2 + x);
g[x_] := -x^4 + 5 x^3 - 5 x + 1;
NIntegrate[Abs[f[x] - g[x]], {x, -0.908624, 0.182851}] +
NIntegrate[Abs[f[x] - g[x]], {x, 0.182851, 0.941036}] +
NIntegrate[Abs[f[x] - g[x]], {x, 0.941036, 4.76}]


(* 101.175 *)

• Under this approach, you don't really need to split up the integral like that—integrating Abs[f[x] - g[x]] over {x, -0.908624, 4.76} would do the trick just fine. – Michael Seifert Dec 14 '15 at 16:47
f[x_] := E^-x (x^4 - 2 x^3 - x^2 + x)
g[x_] := -x^4 + 5 x^3 - 5 x + 1

Plot[{f[x], g[x]}, {x, -1.5, 5},
Filling -> {1 -> {2}},
Mesh -> {{0.0}},
MeshFunctions -> {f[#] - g[#] &},
MeshStyle -> {Red, PointSize@Large},
PlotTheme -> "Detailed"]


sol = Partition[x /. NSolve[f[x] == g[x], x, Reals], 2, 1]


{{-0.908624, 0.182851}, {0.182851, 0.941036}, {0.941036, 4.76709}}

Now, based upon @RunnyKine in

Use Mathematica to calculate the area enclosed between two curves

Area@DiscretizeRegion@RegionUnion[
ImplicitRegion[g[x] > y && f[x] < y, {{x, ##}, y}] & @@@ sol]


100.684

• I like the use of NSolve to get around problems with FindRoot, but isn't the MapThread and Region math a little overkill for this? Is there something about this solution that makes it more general? – djphd Dec 13 '15 at 1:58
• I didn't realise you could use Mesh in this manner. I would have gone with explicitly calculating the intersections with Epilog. Good to know. (+1) – Edmund Dec 14 '15 at 17:16

This is not a very general solution, but if you know roughly how your zeros are spaced you can just build a list of the roots like so:

f=E^-x(x^4-2x^3-x^2+x);
g=-x^4+5x^3-5x+1;
DeleteDuplicates[Round[x/.FindRoot[f==g,{x, #}],.0001]&/@Range[-1,3,.5]


The Round is to round off the machine precision so that equivalent roots aren't identified more than once. Now you can integrate across each segment.

ranges = Partition[%,2,1]


{{-0.9086, 0.1829}, {0.1829, 0.941}}

Integrate[f-g,{x,#[[1]],#[[2]]}]&/@ranges


{-2.3811, 0.51221}

Take the Abs of each if you only care about the area, of course.

• I couldn't quite understand from the question what is the purpose of finding the intermediate roots? Still, you can get them exactly, as the OP does, using Solve[f - g == 0, x, Reals]. Then you're free to consider adjacent pairs, or the maximum and minimum ones, or whatever, as integration bounds. – Oleksandr R. Dec 12 '15 at 22:28
• Agreed. I am trying to read between the lines a bit. It seems like the OP cares about just the area between intersections, but maybe I am wrong. It may be that the real question is one about integration... – djphd Dec 12 '15 at 22:33
• Sorry for being unclear but you're correct I was looking for the area under the curve. I did what you said and it worked perfectly, thank you. I also had trouble with the last part, maybe you could help me with that? Compute numerically the integral| f(x) − g(x)| dx. I've spent a lot of time on these 2 questions and have gotten no where. – Austin Dec 12 '15 at 23:13
• Just put Abs inside or outside the integrand. – djphd Dec 13 '15 at 1:55