# Linear/Poly Fit on a set of data

**URL:** <https://discourse.julialang.org/t/linear-poly-fit-on-a-set-of-data/114168>\
**Category:** General Usage\
**Tags:** question, plotting, curve-fitting\
**Created:** [May 12, 2024, 2:05pm UTC](https://discourse.julialang.org/t/linear-poly-fit-on-a-set-of-data/114168 "2024-05-12T14:05:34Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![S\_Han](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/s_han/32/207639_2.png) [@S\_Han](https://discourse.julialang.org/u/S_Han)\
**Post date:** [May 12, 2024, 2:05pm UTC](https://discourse.julialang.org/t/linear-poly-fit-on-a-set-of-data/114168/1 "2024-05-12T14:05:34Z")

</div>

![image](https://global.discourse-cdn.com/julialang/original/3X/7/2/727b9112b658b8d68e665ccfa577a0d7ec244f2d.png)  
HI I’m trying to fit a curve on this dataset. im using CurveFit.curve\_fit() to find a linear/poly fit to this dataset.  
However, when I am using Polynomial,  
fit = CurveFit.curve\_fit(Polynomial,gamma\_k[1:21],tau\_opt[1:21],2)  
it would return  
MethodError: no method matching zero(::Type{Any}).  
When I am using LinearFit,

`fit = CurveFit.curve_fit(LinearFit,gamma_k[1:21],tau_opt[1:21])`

it would return MethodError: no method matching LinearFit(::Vector{Any}, ::Vector{Any})

I doubt it comes from the datapoints where the x value is the same which might cause a dividing by zero error? but I am not entirely sure. The two list/vectors are of the same length. What has gone wrong in this line of code? Any help is much appreciated.

---

<div class="post-metadata">

**Author:** ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)\
**Post date:** [May 12, 2024, 4:11pm UTC](https://discourse.julialang.org/t/linear-poly-fit-on-a-set-of-data/114168/2 "2024-05-12T16:11:06Z")

</div>

It looks like `gamma_k` and `tau_opt` are `Vector{Any}` and there’s no method for this. Are you actually expecting these to have elements of type `Any` or are they just vectors of numbers which are in an `Any` typed container because you either started from an untyped vector(`[]`) or read them in via XLSX.jl without setting `infer_eltypes = true`?

---

<div class="post-metadata">

**Author:** ![kevbonham](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kevbonham/32/216165_2.png) [@kevbonham](https://discourse.julialang.org/u/kevbonham)\
**Post date:** [May 12, 2024, 5:19pm UTC](https://discourse.julialang.org/t/linear-poly-fit-on-a-set-of-data/114168/3 "2024-05-12T17:19:30Z")

</div>

@nilshg beat me to it, but another way this can happen is if you build vectors the way I used to build lists in python, eg

```julia
my_vec = [] # this is equivalent to Any[]

for thing in things
    push!(my_vec, thing)
end

```

You can specify the vector type when you assign it, eg `Float64[]` instead.

---

<div class="post-metadata">

**Author:** ![S\_Han](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/s_han/32/207639_2.png) [@S\_Han](https://discourse.julialang.org/u/S_Han)\
**Post date:** [May 12, 2024, 6:46pm UTC](https://discourse.julialang.org/t/linear-poly-fit-on-a-set-of-data/114168/4 "2024-05-12T18:46:59Z")

</div>

Thanks, having not assigned type to the list was the problem! Think I’ve never really tried to fit the data obtained yet so the type doesn’t really matter. but i guess it does matter in this case 🙂

---

<div class="post-metadata">

**Author:** ![kevbonham](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kevbonham/32/216165_2.png) [@kevbonham](https://discourse.julialang.org/u/kevbonham)\
**Post date:** [May 12, 2024, 8:52pm UTC](https://discourse.julialang.org/t/linear-poly-fit-on-a-set-of-data/114168/5 "2024-05-12T20:52:29Z")

</div>

> [@S\_Han](#):
>
> so the type doesn’t really matter

Yeah, it doesn’t matter until it does 😛. When I was starting out, `MethodError`s were the most common and most perplexing error I would encounter, because coming from python I wasn’t used to worrying about types, and you can get pretty far in Julia without worrying about them either. But once you understand what it means, they also tend to be the easiest to fix.

I should mention that there are also performance implications for the vector being untyped, but considering the apparent size of your dataset, I wouldn’t expect it to matter much.
