Leastsq function error : Wrong type for output argument #1

Hello.
I hope everyone is fine and having a great day.
Right now I am not.

Context: I have a dataset, about greenhouse gazes emissions from 1990 to 2020, and I have to analyze it in Scilab. Specifically, I am trying to find the probability distribution that my dataset might be following.

In the code portion I will give you, I am testing the normal distribution.

dataWorldMin=dataWorldMin( : )
year=year( : )
function z=normDis(B,x)
z=(1./sqrt(2.*%pi.*B(1))).exp(-(1./2).((x-B(2)).^2)./B(1))
endfunction
function w=normRes(B,x,y)
w=normDis(B,x)-y
endfunction
AI=[1 0.5]
[R,Aopt]=leastsq(list(normRes,year,dataWorldMin),AI)

The “dataWorldMin” variable is a line matrix that stock the GHG emissions in the world for the 31 past years.

The year matrix is the line matrix of each of those yers (1990-2020).

The normDis is the normal distribution function.

The normRes is the residuaal function.

AI is the iniatial values of the parameters, Aopt being the optimal values I want to find. and R is the residual.

Now, the problem:

The Aopt, so the optimal value of the parameters stays the same as the initial value. So whatever it is that I give as AI, it gives me the same thing as Aopt. Which is not normal, especially since the Aopt value is unique and should be the same for everyone, independently of what we use as the inital value (At least that’s what I was taught)

I don’t really know what is causing that anymore, so please, I would be really grateful if anyone of you could enlighten me.

Thanks. And have a good day !

Hello billllliee,

Would you mind share your data ? (x,y table in txt)

BR
F

Hello,

I suggest the use of a zero lower bound for B(1). There is a high probability that the linesearch produces negative values (and you take square roots of a term that becomes negative, which produces complex values, then the error).

S.

1 Like

Hi everyone.
Thanks for all the people who answered me. I ended up finding that lowering my x values, resolved the problem.
Previously they were years, starting around 1990 and more. But then I just made an equivalence to 1,2,3,… and the problem was corrected.
Also make sure the y matrix have values of the 10^-1 order.
That is the best combination for appropriate fitting models.

Thanks again.