# Extrapolate vectors

**URL:** <https://discourse.julialang.org/t/extrapolate-vectors/70239>\
**Category:** New to Julia\
**Tags:** interpolations\
**Created:** [October 22, 2021, 8:11pm UTC](https://discourse.julialang.org/t/extrapolate-vectors/70239 "2021-10-22T20:11:35Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![lostboy](https://avatars.discourse-cdn.com/v4/letter/l/f9ae1b/32.png) [@lostboy](https://discourse.julialang.org/u/lostboy)\
**Post date:** [October 22, 2021, 8:11pm UTC](https://discourse.julialang.org/t/extrapolate-vectors/70239/1 "2021-10-22T20:11:35Z")

</div>

hello everyone.  
i’m dealing with some struggles when programming in Julia since I come from matlab.  
I need to extrapolate this plot so I can get rho(0).

 ![plot](https://global.discourse-cdn.com/julialang/original/3X/1/f/1f58549144b22a88d6f2113a93e56213f580f1c5.jpeg)

any help?

version 1.6.3

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [October 22, 2021, 8:26pm UTC](https://discourse.julialang.org/t/extrapolate-vectors/70239/2 "2021-10-22T20:26:16Z")

</div>

[http://juliamath.github.io/Interpolations.jl/dev/interpolations/](http://juliamath.github.io/Interpolations.jl/dev/interpolations/)

btw, I think you may want a “fit”, do you have a formula you want to fit?

---

<div class="post-metadata">

**Author:** ![lostboy](https://avatars.discourse-cdn.com/v4/letter/l/f9ae1b/32.png) [@lostboy](https://discourse.julialang.org/u/lostboy)\
**Post date:** [October 22, 2021, 8:37pm UTC](https://discourse.julialang.org/t/extrapolate-vectors/70239/3 "2021-10-22T20:37:53Z")

</div>

yeah, a fit would really do.  
actually this is a plot of two vectors rho x a  
a = [1 2 4 6 8 16 32]  
rho = [680 610 415 295 235 190 180]  
i was wondering if there is a command to do this instead of finding the line equation.

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [October 22, 2021, 8:46pm UTC](https://discourse.julialang.org/t/extrapolate-vectors/70239/4 "2021-10-22T20:46:49Z")

</div>

> [@lostboy](#):
>
> i was wondering if there is a command to do this instead of finding the line equation.

In order to extrapolate, you really need to have _some_ model in mind, even if you don’t explicitly fit.

For example, if you want to extrapolate using a straight line from the first two points, you can use `extrapolate(..., Line())` from [Interpolations.jl](http://juliamath.github.io/Interpolations.jl/latest/extrapolation/).

If you think that your data models a peak, so that `1/rho` is approximately a low-degree polynomial, you can use Richardson extrapolation from [Richardson.jl](https://github.com/JuliaMath/Richardson.jl)

For example, with your data I get an extrapolated value of 651 ± 7:

```julia
julia> val, err = extrapolate(zip(reverse(inv.(rho)), reverse(a)))
(0.0015350496476734183, 1.664001386944276e-5)

julia> 1/val
651.4447278728993

julia> err / val^2 # convert error estimate in val to error in 1/val via the derivative
7.061692970914665

```

If you additionally know that your data is symmetric around `a=0`, \rho(-a) = \rho(a), then you can do even-order Richardson extrapolation (i.e. force it to use symmetric polynomials), which predicts \rho(0) = 742 \pm 6:

```julia
julia> val, err = extrapolate(zip(reverse(inv.(rho)), reverse(a)), power=2)
(0.0013481200793223414, 1.1069973063407247e-5)

julia> 1/val
741.7736856962099

julia> err / val^2
6.091011361487163

```

---

<div class="post-metadata">

**Author:** ![gustaphe](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gustaphe/32/18174_2.png) [@gustaphe](https://discourse.julialang.org/u/gustaphe)\
**Post date:** [October 22, 2021, 9:45pm UTC](https://discourse.julialang.org/t/extrapolate-vectors/70239/5 "2021-10-22T21:45:27Z")

</div>

Expanding on this: if for instance you think `\rho = \frac{A}{a+B}` then you can do

```julia
using LsqFit
model(x, p) = p[1] ./ (x .+ p[2])
fit = curve_fit(model, a, rho, [1.0, 1.0])

model(0, fit.param)

```

The same can be done for any (sensible) model.

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [October 22, 2021, 9:50pm UTC](https://discourse.julialang.org/t/extrapolate-vectors/70239/6 "2021-10-22T21:50:28Z")

</div>

Without knowing the underlying model, will draw one more number in the lottery:

```julia
using Dierckx
a = [1, 2, 4, 6, 8, 16, 32]
rho = [680, 610, 415, 295, 235, 190, 180]
spl = Spline1D(a, rho; k=2, bc="extrapolate")
ρ₀ = evaluate(spl, 0) # ρ₀ = 723

```

![Dierckx_k2_extrapolation](https://global.discourse-cdn.com/julialang/original/3X/9/f/9f1f3bf03df716eaf613c61705bb4e34b802c68f.png)

---

<div class="post-metadata">

**Author:** ![Bardo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bardo/32/21601_2.png) [@Bardo](https://discourse.julialang.org/u/Bardo)\
**Post date:** [October 23, 2021, 8:40pm UTC](https://discourse.julialang.org/t/extrapolate-vectors/70239/7 "2021-10-23T20:40:47Z")

</div>

> [@lostboy](#):
>
> i’m dealing with some struggles when programming in Julia since I come from matlab.

Not sure if I understand the scope of the problem. Is the underlying function supposed to be analytic, are the points exact or can they contain noise, …  
Do you mind sharing the Matlab code?

---

<div class="post-metadata">

**Author:** ![gustaphe](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gustaphe/32/18174_2.png) [@gustaphe](https://discourse.julialang.org/u/gustaphe)\
**Post date:** [October 23, 2021, 9:34pm UTC](https://discourse.julialang.org/t/extrapolate-vectors/70239/8 "2021-10-23T21:34:35Z")

</div>

Honestly if there’s a function that lets you do this without specifying a model by which to do it, that function is lying to you. And if such a function is built into MATLAB, that’s a flaw of MATLAB, not a strength.
