3
$\begingroup$

I'm trying to find a value x that satisfies an equation of the form $y=\int_0^xf(x')dx'$.

As far as I can tell, there's no closed-form solution for $y$, or $dy/dx$, so when I plug this into Solve, NSolve, FindRoot, etc. Mathematica churns for a very long time.

However, I can get an answer to the rough precision I require much quicker if I just plot $\int_0^xf(x')dx'-y$ and read off the value of the root from the plot.

This isn't accurate or precise, but it's quick, and it's good enough for my purposes. The question then is how can I wrangle Mathematica so it does something similar instead of trying to get me the most exact answer?

Playing around with precisiongoal and accuracygoal isn't cutting it, because really what I need is a less sophisticated algorithm. The function is monotonically increasing, so it really should be sufficient just to step up in x by some small amount until the sign changes. Unfortunately I don't know how to code this in Mathematica.

$\endgroup$
  • $\begingroup$ May be this answer -- that uses minimization with memoization and a coarser grid of numbers -- would give you some ideas to get not very precise answers quickly. $\endgroup$ – Anton Antonov May 23 '17 at 21:30
  • 2
    $\begingroup$ Use NDSolve[] + WhenEvent[]. $\endgroup$ – J. M. will be back soon May 23 '17 at 21:33
3
$\begingroup$

J.M.'s comment led me to a working answer.

Manipulating my above equation, I arrive at $dy/dx=f(x)$. This can be put into NDSolve with a WhenEvent:

Reap[NDSolve[{Y'[x] == f[x]), Y[0] == 0,
 WhenEvent[Y[x] - y > 0, Sow[x]]}, Y, {x, 0, 10}]]
$\endgroup$
  • 2
    $\begingroup$ Always Be Breeding $\endgroup$ – Cody May 23 '17 at 23:30
  • 1
    $\begingroup$ This event only works if Y[x] is initially less than y and f[x] is positive (at least long enough for the event to happen). You want Y[x] == y (or Y[x] - y == 0). $\endgroup$ – Michael E2 May 23 '17 at 23:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.