# function that describes accumulated area between curves?

I'm still a beginner to this so bear with me

I'm trying to create a function that describes the accumulated area between two functions, but I'm having no luck with getting a graph to appear while also being confused on whether my function is correct or not.

Here's what I have so far:

A = x /. FindRoot[pred[x] == prey[x], {x, 150}]
B = x /. FindRoot[pred[x] == prey[x], {x, 250}]
preypop := NIntegrate[prey[x] - pred[x], {x, 0, A}] + NIntegrate[pred[x] - prey[x], {x, A, B}] + NIntegrate[prey[x] - pred[x], {x, B, 365}]


The plot function only shows an empty graph after taking a while to process. I'd appreciate if someone could also check if the function would actually work because I'm at a loss on what to do.

EDIT: here's the function declaration and plot function

prey[t_] :=
5 (Sin[\[Pi]*t/365])^4 (Cos[\[Pi]*t/365])^2 (5 + Sin[2 \[Pi]*t/365])

pred[t_] := 6 (Sin[\[Pi]*t/365])^6

Plot[{pred[t], prey[t]}, {t, 0, 365}, Filling -> 1 -> {2},
PlotLegends -> "Expressions",
AxesLabel -> {"Day of the year", "Population dynamic"}]


EDIT2: oh god well this is embarassing (proper functions)

prey[t_] :=
5 (Sin[\[Pi]*t/365])^4 (Cos[\[Pi]*t/365])^2 (5 + Sin[2 \[Pi]*t/365])

pred[t_] := 6 (Sin[\[Pi]*t/365])^6

A = x /. FindRoot[pred[x] == prey[x], {x, 150}]
B = x /. FindRoot[pred[x] == prey[x], {x, 250}]

preypop :=
NIntegrate[prey[x] - pred[x], {x, 0, A}] +
NIntegrate[pred[x] - prey[x], {x, A, B}] +
NIntegrate[prey[x] - pred[x], {x, B, 365}]

Plot[preypop[x], {x, 0, 365}]

• Welcome to Mathematica StackExchange! Please provide the whole code you are using, including definitions of pred and prey. Feb 27, 2022 at 18:45
• To add the comment by @Domen please provide the definitions for pred and prey, but also give us the Plot command to which you are referring. In the code you provided there's only a numerical integration, no Plot whatsoever.
– user49048
Feb 27, 2022 at 18:58
• @Domen ok i added those things in Feb 27, 2022 at 21:46
• The plot appears as expected. Have you tried the code in a fresh Mathematica session? Feb 27, 2022 at 21:51
• @SimonWoods oof sorry for making you run in a circle i posted the wrong graph function. The newest edit should be the segment i'm actually referring to Feb 27, 2022 at 22:19

Given your definition of preypop, I assume you want to calculate $$\int \left|{\rm prey}(x) - {\rm pred}(x)\right| \, \mathrm d x$$.

It turns out that for non-analytical integrals, it is usually more efficient to use NDSolve instead of Integrate.

sol = NDSolve[{y'[x] == Abs[pred[x] - prey[x]], y[0] == 0}, y[x], {x, 0, 365}];
Plot[y[x] /. sol, {x, 0, 365}]


• thanks for answering, i don't think i would ever have been able to figure that out on my own. Feb 27, 2022 at 22:36