# Exponential Curve Fit Equation

**URL:** https://discourse.julialang.org/t/exponential-curve-fit-equation/78608
**Category:** New to Julia
**Tags:** question
**Created:** [March 28, 2022, 12:14pm UTC](https://discourse.julialang.org/t/exponential-curve-fit-equation/78608 "2022-03-28T12:14:56Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![Mahmoud](https://avatars.discourse-cdn.com/v4/letter/m/59ef9b/32.png) [@Mahmoud](https://discourse.julialang.org/u/Mahmoud)
#### Post date: [March 28, 2022, 12:14pm UTC](https://discourse.julialang.org/t/exponential-curve-fit-equation/78608/1 "2022-03-28T12:14:56Z")

</div>

I am trying to fit my data in figure 1  
 ![plot_22](https://global.discourse-cdn.com/julialang/original/3X/c/b/cbbc47a635f7f38b39785f020c7538fb280f4ba8.png)  
to be like figure 2 (exponential fit)  
 ![plot_222](https://global.discourse-cdn.com/julialang/original/3X/4/8/485f4f52e550ad69f4d99f0fa0400e38006a44da.png)

but my code does not do that

```julia
using LsqFit
#days = [1,2,3,4,5,6,7,8,9 ,...........]
#y = some code to update y value 
@. model(days, p) = p[1] * days.^(-p[2]) 
fit = curve_fit(model, days, y, [0.5,0.5]) #
@show fit.param 
plot(days, model(days, fit.param))

```

---

<div class="post-metadata">

### Author: ![feanor12](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/feanor12/32/8212_2.png) [@feanor12](https://discourse.julialang.org/u/feanor12)
#### Post date: [March 28, 2022, 2:33pm UTC](https://discourse.julialang.org/t/exponential-curve-fit-equation/78608/2 "2022-03-28T14:33:34Z")

</div>

Fitting exponential data is a bit tricky.  
If you want a quick fit try fitting the logarithm of the data. To get a correct uncertainty estimation, however, one has to use nonuniform weights.

This fit you can then use to get a better initial value.

Also your data does not look like to follow an exponential function. Maybe one could fit a sigmoid function or restrict the data range.

The function you use is also not an exponential.I would have expected something like this

```julia
@. model(days,p) = p[1] + exp(days*p[2])

```

For restricting the data range you can introduce a mask like this.

```julia
mask=y.<=50
curve_fit(model,days[mask],y[mask],[0.5,0.5])

```

In addition, I noticed that in your example there is a missing `]` for the initial value array.

One thing I wanted to do for some time was figuring out how to use GLM.jl for such applications. I think by selecting the correct link function this might work.

I hope my comments help somewhat.

---

<div class="post-metadata">

### Author: ![johnmyleswhite](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/johnmyleswhite/32/31_2.png) [@johnmyleswhite](https://discourse.julialang.org/u/johnmyleswhite)
#### Post date: [March 28, 2022, 2:55pm UTC](https://discourse.julialang.org/t/exponential-curve-fit-equation/78608/3 "2022-03-28T14:55:21Z")

</div>

This seems much easier to do by using blackbox optimization (via Optim) with the actual functional form you want that provides a closure over your data. It’s true that you can make this into a LsqFit problem, but the more general approach of blackbox optimization is easier once you’re used to it.

---

<div class="post-metadata">

### Author: ![feanor12](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/feanor12/32/8212_2.png) [@feanor12](https://discourse.julialang.org/u/feanor12)
#### Post date: [March 28, 2022, 4:03pm UTC](https://discourse.julialang.org/t/exponential-curve-fit-equation/78608/4 "2022-03-28T16:03:43Z")

</div>

Here is a function that might work.

```julia

julia> using UnicodePlots

julia> scaled_sigmoid(x,low,high,a,b) = low+(high-low)*(1/(1+exp(a+b*x)))
scaled_sigmoid (generic function with 1 method)

julia> lineplot((x)->scaled_sigmoid(x,0,2,3,-1))
          ┌────────────────────────────────────────┐      
        2 │⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⡇⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣀⠤⠒⠋⠉⠉⠉⠉⠉│ #9(x)
          │⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⡇⠀⠀⠀⠀⠀⠀⠀⠀⢀⡔⠁⠀⠀⠀⠀⠀⠀⠀⠀│      
          │⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⡇⠀⠀⠀⠀⠀⠀⠀⢀⠎⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀│      
          │⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⡇⠀⠀⠀⠀⠀⠀⠀⡜⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀│      
          │⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⡇⠀⠀⠀⠀⠀⠀⡸⠁⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀│      
          │⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⡇⠀⠀⠀⠀⠀⢀⠇⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀│      
          │⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⡇⠀⠀⠀⠀⠀⡜⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀│      
   f(x) │⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⡇⠀⠀⠀⠀⢠⠇⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀│      
          │⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⡇⠀⠀⠀⠀⡎⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀│      
          │⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⡇⠀⠀⠀⢰⠃⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀│      
          │⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⡇⠀⠀⠀⡇⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀│      
          │⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⡇⠀⠀⡸⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀│      
          │⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⡇⠀⡰⠃⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀│      
          │⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⡇⡔⠁⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀│      
        0 │⣀⣀⣀⣀⣀⣀⣀⣀⣀⣀⣀⣀⣀⣀⣀⣀⣀⣀⠤⠖⡏⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀│      
          └────────────────────────────────────────┘      
          ⠀-10⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀10⠀      
          

```

At the high and low ends this can also be approximated by an exponential function. (See Fermi-Dirac statistics)

---

<div class="post-metadata">

### Author: ![Mahmoud](https://avatars.discourse-cdn.com/v4/letter/m/59ef9b/32.png) [@Mahmoud](https://discourse.julialang.org/u/Mahmoud)
#### Post date: [March 28, 2022, 4:52pm UTC](https://discourse.julialang.org/t/exponential-curve-fit-equation/78608/5 "2022-03-28T16:52:30Z")

</div>

> [@feanor12](#):
>
> `lineplot((x)->scaled_sigmoid(x,0,2,3,-1))`

How to use it with vectors like  
x=[1,2,3,4,5,6,7,…]  
y=[2,3,3,4,6,7,8,…]

---

<div class="post-metadata">

### Author: ![feanor12](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/feanor12/32/8212_2.png) [@feanor12](https://discourse.julialang.org/u/feanor12)
#### Post date: [March 28, 2022, 5:32pm UTC](https://discourse.julialang.org/t/exponential-curve-fit-equation/78608/6 "2022-03-28T17:32:51Z")

</div>

> [@feanor12](#):
>
> `scaled_sigmoid(x,low,high,a,b) = low+(high-low)*(1/(1+exp(a+b*x)))`

```julia
using LsqFit
using Plots
x=[1,2,3,4,5,6,7]
y=[2,3,3,4,6,7,8]

@. scaled_sigmoid(x,low,high,a,b) = low+(high-low)*(1/(1+exp(a+b*x)))
scaled_sigmoid(x,p) = scaled_sigmoid(x,p...)

p0 = [0,100,0.1,-1]
fit = curve_fit(scaled_sigmoid,x,y,p0)

scatter(x,y,label="data",xlabel="x",ylabel="y")
plot!((x)->scaled_sigmoid(x,p0),label="initial")
plot!((x)->scaled_sigmoid(x,coef(fit)),label="fit")

```

![image](https://global.discourse-cdn.com/julialang/original/3X/e/a/ea76919c33bdd7a1bc7fc26e1bb3608db9f59344.png)

---

<div class="post-metadata">

### Author: ![andreypikunov](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/andreypikunov/32/29674_2.png) [@andreypikunov](https://discourse.julialang.org/u/andreypikunov)
#### Post date: [March 28, 2022, 10:10pm UTC](https://discourse.julialang.org/t/exponential-curve-fit-equation/78608/7 "2022-03-28T22:10:26Z")

</div>

Easy, like this

```julia
using UnicodePlots
x = [1, 2, 3, 4, 5, 6, 7]
y = [2, 3, 3, 4, 6, 7, 8]
lineplot(x, y)

# result will be
     ┌────────────────────────────────────────┐ 
   8 │⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢀⠔⠉│ 
     │⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢀⡠⠊⠁⠀⠀│ 
     │⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣀⠔⠁⠀⠀⠀⠀⠀│ 
     │⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢀⠤⠊⠀⠀⠀⠀⠀⠀⠀⠀│ 
     │⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⡠⠒⠁⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀│ 
     │⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢠⠊⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀│ 
     │⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⡰⠁⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀│ 
     │⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢀⠜⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀│ 
     │⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢠⠊⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀│ 
     │⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⡰⠁⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀│ 
     │⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢀⠔⠊⠁⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀│ 
     │⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢀⡠⠊⠁⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀│ 
     │⠀⠀⠀⠀⠀⢀⡠⠤⠤⠤⠤⠤⠤⠔⠁⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀│ 
     │⠀⠀⠀⡠⠔⠁⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀│ 
   2 │⣀⠔⠊⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀│ 
     └────────────────────────────────────────┘ 
     ⠀1⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀7⠀ 

```

I recommend you check the docs, they are nice:  
[https://github.com/JuliaPlots/UnicodePlots.jl](https://github.com/JuliaPlots/UnicodePlots.jl)

Or have a look at `methods(lineplot)`.

Good luck with Julia)
