# [SciPy-user] plot curve of a series of equations

josef.pktd@gmai... josef.pktd@gmai...
Thu Apr 23 13:37:01 CDT 2009

```On Thu, Apr 23, 2009 at 12:24 AM, Bevan Jenkins
<bevan.jenkins@ew.govt.nz> wrote:
> Hello,
>
> I have a series of equations that describe a curve.  Each equation is only
> valid for a range of x values.  For example:
>
> Equation                                                 Upperlimit Lowerlimit
> 24 + 18.75 * x + 4.79616e-013 * x ** 2 + 31.25 * x ** 3    7.800      7.600
> 28 + 22.5 * x + 5.68434e-013 * x ** 2 + 0 * x ** 3         8.000      7.800
>
> There is a lower limit for eqn1, after that the upper limit of the previous
> eqn is lower of the next.
>
> From (X) scale linear, To (Y) scale linear
> y = A0 + A1(x) + A2(x)2 + A3(x)3
> where:    x= value – LL
>
> LL = the lower limit of the applicable equation
>
> I would like to generate a sequence of values (linspace(min(lowerlimit),max
> (upperlimit)), work out x (x=value-approriate lower limit) and then using the
> appropriate equation generate the y values.
> Below is the answer I am looking for
>
> Value   x       y
> 7.600   0       24.000
> 7.650   0.05    24.941
> 7.700   0.1     25.906
> 7.750   0.15    26.918
> 7.800   0.2     28.000
> 7.850   0.050   29.125
> 7.900   0.100   30.25
> 7.950   0.150   31.375
> 8.000   0.200   32.5
>
> Thanks for any help,
>
> Bevan Jenkins
>

your explanation is not completely clear, but you seem to be looking
for a piece-wise function function.

If you have a  partitioning of the domain with non-overlapping but
connected intervals, and x is scalar, then I would use np.searchsorted
to find the interval index and then call the function by index in a
list of functions.

Kind of a python version of a case statement with searchsorted instead
of dictionary.

If the calculation in each equation is cheap/fast and many x, then you
can vectorize it, calculate all equations for all x and then get the
one corresponding to the searchsorted index, this will be much faster
in some cases than looping over x.

Josef
```