3
$\begingroup$

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}]
$\endgroup$
5
  • 5
    $\begingroup$ Welcome to Mathematica StackExchange! Please provide the whole code you are using, including definitions of pred and prey. $\endgroup$
    – Domen
    Feb 27, 2022 at 18:45
  • 1
    $\begingroup$ 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. $\endgroup$
    – user49048
    Feb 27, 2022 at 18:58
  • $\begingroup$ @Domen ok i added those things in $\endgroup$
    – Cezlock
    Feb 27, 2022 at 21:46
  • $\begingroup$ The plot appears as expected. Have you tried the code in a fresh Mathematica session? $\endgroup$ Feb 27, 2022 at 21:51
  • $\begingroup$ @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 $\endgroup$
    – Cezlock
    Feb 27, 2022 at 22:19

1 Answer 1

4
$\begingroup$

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

NDSolve

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

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.