Linear/Poly Fit on a set of data

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.

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?


@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

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

for thing in things
    push!(my_vec, thing)

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


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 :slight_smile:

1 Like

Yeah, it doesn’t matter until it does :stuck_out_tongue:. When I was starting out, MethodErrors 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.

1 Like