[Scilab-users] Weird error with leastsq()

classic Classic list List threaded Threaded
5 messages Options
Dang Ngoc Chan, Christophe Dang Ngoc Chan, Christophe
Reply | Threaded
Open this post in threaded view
|

[Scilab-users] Weird error with leastsq()

Hello all,

Let's talk about optics.
I have a glass, its refraction index n varies with the wavelength lamba.
I have a CSV file with the corresponding data (verre_HOQ.cvs attached), 52 points.

I want to model this with the Sellmeier model
https://en.wikipedia.org/wiki/Sellmeier_equation
and use for this the attached script (regression_sellmeier.sce).

Mind to set the path at line 7 of the script if you want to run it.

My problem:
I can run piecewise regression,
i.e. it runs when I work with the points 1 to 25, 13 to 39, or 32 to 52;
e.g. at lines 11 and 12, it works when I use

// ----------

lambda = 1e-3*donnees(1:25, 1); // en µm

nexp = donnees(1:25, 2);

// ----------

but when I try to extend these ranges (and of course when I try to use all the points),
it fails with the following error:

5215700pt: Wrong type for output argument #1: Real scalar expected.

Can anyone tell me what goes on here?

Regards

--
Christophe Dang Ngoc Chan
Mechanical calculation engineer


General
This e-mail may contain confidential and/or privileged information. If you are not the intended recipient (or have received this e-mail in error), please notify the sender immediately and destroy this e-mail. Any unauthorized copying, disclosure or distribution of the material in this e-mail is strictly forbidden.

_______________________________________________
users mailing list
[hidden email]
http://lists.scilab.org/mailman/listinfo/users

regression_sellmeier.sce (1K) Download Attachment
verre_HOQ.csv (928 bytes) Download Attachment
Antoine Monmayrant Antoine Monmayrant
Reply | Threaded
Open this post in threaded view
|

Re: Weird error with leastsq()

Hello,

I can tell you what happened: for some values of lambda and some values of K(4,5,6), your function is undefined and the leastsq goes kaboom!

It is never a good idea to fit a function that can become undefined for some parameters/xvalues.
Try to redefine the function you fit to avoid any undefined values.

Antoine

I was able to use the range [1:51], but not  [1:52].

Le 27/08/2020 à 16:52, Dang Ngoc Chan, Christophe a écrit :
Hello all,

Let's talk about optics.
I have a glass, its refraction index n varies with the wavelength lamba.
I have a CSV file with the corresponding data (verre_HOQ.cvs attached), 52 points.

I want to model this with the Sellmeier model
https://en.wikipedia.org/wiki/Sellmeier_equation
and use for this the attached script (regression_sellmeier.sce).

Mind to set the path at line 7 of the script if you want to run it.

My problem:
I can run piecewise regression,
i.e. it runs when I work with the points 1 to 25, 13 to 39, or 32 to 52;
e.g. at lines 11 and 12, it works when I use

// ----------

lambda = 1e-3*donnees(1:25, 1); // en µm

nexp = donnees(1:25, 2);

// ----------

but when I try to extend these ranges (and of course when I try to use all the points),
it fails with the following error:

5215700pt: Wrong type for output argument #1: Real scalar expected.

Can anyone tell me what goes on here?

Regards

--
Christophe Dang Ngoc Chan
Mechanical calculation engineer


General
This e-mail may contain confidential and/or privileged information. If you are not the intended recipient (or have received this e-mail in error), please notify the sender immediately and destroy this e-mail. Any unauthorized copying, disclosure or distribution of the material in this e-mail is strictly forbidden.

_______________________________________________
users mailing list
[hidden email]
http://lists.scilab.org/mailman/listinfo/users

_______________________________________________
users mailing list
[hidden email]
http://lists.scilab.org/mailman/listinfo/users
Antoine Monmayrant Antoine Monmayrant
Reply | Threaded
Open this post in threaded view
|

Re: Weird error with leastsq()

In reply to this post by Dang Ngoc Chan, Christophe

OK, forget my previous answer.

Use this:

function [n] = sellmeier(KL, lambda)
    K1 = KL(1); K2 = KL(2); K3 = KL(3);
    L1 = KL(4); L2 = KL(5); L3 = KL(6);
    lambdacarre = lambda.*lambda;
    foo1 = lambdacarre - L1;
    foo2 = lambdacarre - L2;
    foo3 = lambdacarre - L3;
    foo = 1./(foo1.*foo2.*foo3);
    n = sqrt(1 +...
    lambdacarre.*(K1*foo.*foo2.*foo3 + K2*foo.*foo1.*foo3 + K3*foo.*foo1.*foo2));
    n=real(n);
endfunction

Now it works.

Antoine

PS: Here is the issue: your sellmeier function should be bulletproof and gracefully return real values no matter what parameters you feed to it.

Le 27/08/2020 à 16:52, Dang Ngoc Chan, Christophe a écrit :
Hello all,

Let's talk about optics.
I have a glass, its refraction index n varies with the wavelength lamba.
I have a CSV file with the corresponding data (verre_HOQ.cvs attached), 52 points.

I want to model this with the Sellmeier model
https://en.wikipedia.org/wiki/Sellmeier_equation
and use for this the attached script (regression_sellmeier.sce).

Mind to set the path at line 7 of the script if you want to run it.

My problem:
I can run piecewise regression,
i.e. it runs when I work with the points 1 to 25, 13 to 39, or 32 to 52;
e.g. at lines 11 and 12, it works when I use

// ----------

lambda = 1e-3*donnees(1:25, 1); // en µm

nexp = donnees(1:25, 2);

// ----------

but when I try to extend these ranges (and of course when I try to use all the points),
it fails with the following error:

5215700pt: Wrong type for output argument #1: Real scalar expected.

Can anyone tell me what goes on here?

Regards

--
Christophe Dang Ngoc Chan
Mechanical calculation engineer


General
This e-mail may contain confidential and/or privileged information. If you are not the intended recipient (or have received this e-mail in error), please notify the sender immediately and destroy this e-mail. Any unauthorized copying, disclosure or distribution of the material in this e-mail is strictly forbidden.

_______________________________________________
users mailing list
[hidden email]
http://lists.scilab.org/mailman/listinfo/users

_______________________________________________
users mailing list
[hidden email]
http://lists.scilab.org/mailman/listinfo/users
Dang Ngoc Chan, Christophe Dang Ngoc Chan, Christophe
Reply | Threaded
Open this post in threaded view
|

Re: {EXT} Re: Weird error with leastsq()

Hello,

> De : Antoine Monmayrant
> Envoyé : jeudi 27 août 2020 17:25
>
>   n=real(n);
>
> Now it works.

Basic.
I thought about this and I know I tested some isreal(sellmeier(lambda)) stuff during the development but I did not thought it would happen during the regression process with some parameter values.

Thanks a lot.

Regards

--
Christophe Dang Ngoc Chan
Mechanical calculation engineer

General
This e-mail may contain confidential and/or privileged information. If you are not the intended recipient (or have received this e-mail in error), please notify the sender immediately and destroy this e-mail. Any unauthorized copying, disclosure or distribution of the material in this e-mail is strictly forbidden.
_______________________________________________
users mailing list
[hidden email]
http://lists.scilab.org/mailman/listinfo/users
Antoine Monmayrant Antoine Monmayrant
Reply | Threaded
Open this post in threaded view
|

Re: {EXT} Re: Weird error with leastsq()


On 27/08/2020 17:55, Dang Ngoc Chan, Christophe wrote:

> Hello,
>
>> De : Antoine Monmayrant
>> Envoyé : jeudi 27 août 2020 17:25
>>
>>    n=real(n);
>>
>> Now it works.
> Basic.
> I thought about this and I know I tested some isreal(sellmeier(lambda)) stuff during the development but I did not thought it would happen during the regression process with some parameter values.

Well, been there, done that: if you think your function can only be fed
cats and dogs, you can be sure that leastsq will send it giraffes and
unicorns! :-)

Antoine

>
> Thanks a lot.
>
> Regards
>
> --
> Christophe Dang Ngoc Chan
> Mechanical calculation engineer
>
> General
> This e-mail may contain confidential and/or privileged information. If you are not the intended recipient (or have received this e-mail in error), please notify the sender immediately and destroy this e-mail. Any unauthorized copying, disclosure or distribution of the material in this e-mail is strictly forbidden.
> _______________________________________________
> users mailing list
> [hidden email]
> http://lists.scilab.org/mailman/listinfo/users
>
_______________________________________________
users mailing list
[hidden email]
http://lists.scilab.org/mailman/listinfo/users