First Pic

Second Pic

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.


  • 6
    $\begingroup$ 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. $\endgroup$
    – djphd
    Dec 12, 2015 at 22:11

3 Answers 3


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 *)

  • 1
    $\begingroup$ 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. $\endgroup$ Dec 14, 2015 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"]

enter image description here

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

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


  • $\begingroup$ 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? $\endgroup$
    – djphd
    Dec 13, 2015 at 1:58
  • $\begingroup$ 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) $\endgroup$
    – Edmund
    Dec 14, 2015 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:

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}}


{-2.3811, 0.51221}

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

  • $\begingroup$ 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. $\endgroup$ Dec 12, 2015 at 22:28
  • $\begingroup$ 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... $\endgroup$
    – djphd
    Dec 12, 2015 at 22:33
  • $\begingroup$ 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. $\endgroup$
    – Austin
    Dec 12, 2015 at 23:13
  • $\begingroup$ Just put Abs inside or outside the integrand. $\endgroup$
    – djphd
    Dec 13, 2015 at 1:55

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.