Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

The inverted integral of LinearInterpolation #277

Closed
SouthEndMusic opened this issue Jun 29, 2024 · 4 comments · Fixed by #282
Closed

The inverted integral of LinearInterpolation #277

SouthEndMusic opened this issue Jun 29, 2024 · 4 comments · Fixed by #282

Comments

@SouthEndMusic
Copy link
Member

SouthEndMusic commented Jun 29, 2024

What kind of problems is it mostly used for? Please describe.

For my application I need to compute the water level in a bucket given a piecewise linear area(level) relationship and a volume:

fig

It is assumed that the area is always positive.

Describe the algorithm you’d like

volume(level) relationship:

$$ V = V_i + \int_{\ell_i}^{\ell} A_i + \frac{\Delta A_i}{\Delta \ell_i}(\ell-\ell_i)\text{d}t = \frac{1}{2}\frac{\Delta A_i}{\Delta \ell_i}(\ell-\ell_i)^2 + A_i(\ell-\ell_i) + V_i $$

So this quadratic equation must be solved for $\ell$:

$$ \ell = \ell_i + \Delta \ell_i\frac{-A_i + \text{sign}(\Delta A_i)\sqrt{A_i^2 + 2\frac{\Delta A_i}{\Delta \ell_i}(V-V_i)}}{\Delta A_i} $$

This does lead to numerical problems when $\frac{\Delta A_i}{\Delta \ell_i}$ is small, so I could use some help with that.

Other implementations to know about

I implemented this as CLinearInterpolationInvInv here: https://github.com/SouthEndMusic/CachedInterpolations.jl.

Is this too niche for DataInterpolations.jl?

@ChrisRackauckas
Copy link
Member

Is this doable for all interpolations?

@SouthEndMusic
Copy link
Member Author

In favour of doability:

  • We could cache the cumulative integral at the data points, which could be used to speed up the existing integral calculations and can serve as the t for the integral inverse

Against doability:

  • First of all: we need a not too expensive way to determine whether the integral is invertible, for which we can check whether the original interpolation is strictly positive everywhere. This is trivial for linear interpolation, but not for more complicated interpolation types.
  • Then there is the matter of computing the inverse analytically, which generally means solving a polynomial equation. This already gave me some problems with edge cases in the linear interpolation case as mentioned above, which only gets worse with more complicated methods. Or we have to use a numerical solver for this, but that is probably more expensive to compute

@ChrisRackauckas
Copy link
Member

It could be added to the interface with a trait for which algorithms support it, and just not supported by most algorithms to start. That could be fine.

@SouthEndMusic
Copy link
Member Author

I think the following expression is sufficiently stable (where $s$ is the slope of the linear interpolation):

$$ \ell = \ell_i + \frac{2(V - V_i)}{A_i + \sqrt{A_i^2 + 2s(V - V_i)}}, $$

as we require $A_i > 0$, and for $s = 0$ it nicely reduces to the linear

$$ \ell = \ell_i + \frac{V - V_i}{A_i}. $$

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants