Re: [Meep-discuss] Nonlinear Complex fit

2019-12-17 Thread Andrey V. Panov
You may want to try lmfit, https://lmfit.github.io/lmfit-py/

On Tue, Dec 17, 2019 at 01:31:29PM +, md mir wrote:
> Hello, Steven G. Johnson/Oskooi  and meep users,
> I post this topic earlier but didn't get reply. So I'm writing it again. In 
> my Research I have Complex Refractive Index data from an Experiment.  Like as 
> shown below,
>  Wavelength  Re(index)  Im(index)0.2041   2.572896.30832E-12 0.2042   
> 2.572876.32153E-12 0.2043   2.572856.3348E-12 0.2044   
> 2.572846.34812E-12 0.2045   2.572826.3615E-12 ---  -  
>    -  - ---   -  - ---    
>   -  - ---  -  - ---     -  
> - ---   -  -      4.02.572826.3615E-12     
> Dr. Steven wrote in here 
> https://www.mail-archive.com/meep-discuss@ab-initio.mit.edu/msg01001.html  
> that we have to use a nonlinear optimization program to do the fit. I spent 
> quite a long time to find a proper code/software. But release that, most 
> off-the-shelf fitting programs only fit a real-valued function, whereas here 
> I have to fit a complex-valued function. 
> 
> I'm stuck in here for a long time now to find out the parameters (omega, 
> sigma, gamma) for the Lorentzian model. Does anyone have any program/code to 
> fit the data?Any help will be highly appreciated.
> Thank you in advance.Regards Mir

> ___
> meep-discuss mailing list
> meep-discuss@ab-initio.mit.edu
> http://ab-initio.mit.edu/cgi-bin/mailman/listinfo/meep-discuss


-- 
Andrey V. Panov
panov /AT/ iacp.dvo.ru
http://canopus.iacp.dvo.ru/~panov/

___
meep-discuss mailing list
meep-discuss@ab-initio.mit.edu
http://ab-initio.mit.edu/cgi-bin/mailman/listinfo/meep-discuss

[Meep-discuss] Nonlinear Complex fit

2019-12-17 Thread md mir
Hello, Steven G. Johnson/Oskooi  and meep users,
I post this topic earlier but didn't get reply. So I'm writing it again. In my 
Research I have Complex Refractive Index data from an Experiment.  Like as 
shown below,
 Wavelength  Re(index)  Im(index)0.2041   2.572896.30832E-12 0.2042 
  2.572876.32153E-12 0.2043   2.572856.3348E-12 0.2044   
2.572846.34812E-12 0.2045   2.572826.3615E-12 ---  -
     -  - ---   -  - ---    
  -  - ---  -  - ---     -  - 
---   -  -      4.02.572826.3615E-12     Dr. Steven 
wrote in here 
https://www.mail-archive.com/meep-discuss@ab-initio.mit.edu/msg01001.html  that 
we have to use a nonlinear optimization program to do the fit. I spent quite a 
long time to find a proper code/software. But release that, most off-the-shelf 
fitting programs only fit a real-valued function, whereas here I have to fit a 
complex-valued function. 

I'm stuck in here for a long time now to find out the parameters (omega, sigma, 
gamma) for the Lorentzian model. Does anyone have any program/code to fit the 
data?Any help will be highly appreciated.
Thank you in advance.Regards Mir___
meep-discuss mailing list
meep-discuss@ab-initio.mit.edu
http://ab-initio.mit.edu/cgi-bin/mailman/listinfo/meep-discuss

Re: [Meep-discuss] nonlinear complex fit

2007-07-08 Thread Mischa Megens
 On Tue, 3 Jul 2007, matt wrote:
 I have the following data for a lossy material (nylon), and I'd like to
 find the parameters for meep's lorentzian model which fit the data:

 frequency (hz)  real(eps) imag(eps)
 60  3.7 -.0666
 1e3 3.5 -.0651
 1e6 3.14-.068452
 1e8 3.0 -.06
 3e9 2.84-.033228
 2.5e10  2.73-.028665

 There was an indication in a previous post that this is simple
 [...]

 I'm not sure it's quite so simple, because most off-the-shelf fitting
 programs only fit a real-valued function, whereas here you have to fit a
 complex-valued function.  In general, you have to use a nonlinear
 optimization program to do the fit (to minimize the sum-of-squares errors
 or whatever error criterion you prefer).

 I don't have any off-the-shelf code to do this for the Lorentzian problem
 (although of course there are lots of off-the-shelf nonlinear optimization
 routines), but perhaps someone on the list does.

 Steven


Here is a snippet to coax MatLab into fitting a complex valued function (a 
single Lorentzian):
--
%Example data:
freq=[1 5 8 10 12 15 20];
epsre=[1.10 1.13 1.19 1.00 0.86 0.93 0.97];
epsim=[0.0031 0.026 0.13 0.33 0.11 0.026 0.0064];
epsilon=epsre+i*epsim;

%Plot it:
plot(freq,real(epsilon),'bo', freq,imag(epsilon),'ro');

lorentzian=inline('p(1)+p(2)*p(3)^2./(p(3)^2-f.^2-i*f*p(4))','p','f');
%p(1) - epsilon_infinity
%p(2) - Delta epsilon_n
%p(3) - w_n
%p(4) - gamma_n
fitfunc=inline('[real(p(1)+p(2)*p(3)^2./(p(3)^2-f.^2-i*f*p(4)))-re,imag(p(1)+p(2)*p(3)^2./(p(3)^2-f.^2-i*f*p(4)))-im]','p','f','re','im');
p=[0.9 0.15 8 2];  %initial guess
p=lsqnonlin(fitfunc,p,[],[],[],freq,real(epsilon),imag(epsilon));
disp(p)  %show the answer

%Plot the result:
f=1:0.01:20;
hold on; plot(f,real(lorentzian(p,f)),'b', f,imag(lorentzian(p,f)),'r');


Unfortunately, Matt's data do not look like a Lorentzian, so it won't fit 
very well if you try it on his data.  Of course it is possible to add more 
Lorentzians, but quite a few Lorentzians would be needed to span the wide 
frequency range and since there are only few data points in Matt's table, a 
multiple Lorentzian fit would start doing funny things in between the data 
points (as if fitting a high order polynomial to scant data).
Perhaps you can interpolate the table a bit to alleviate this, and fix the 
w_n at the original frequency ordinates, only fitting the Delta epsilons and 
gammas?

Best regards,
m.


___
meep-discuss mailing list
meep-discuss@ab-initio.mit.edu
http://ab-initio.mit.edu/cgi-bin/mailman/listinfo/meep-discuss


Re: [Meep-discuss] nonlinear complex fit

2007-07-08 Thread G.J. Parker
---

there's two ways i've done this.

first was to use nl2sol fortran routine which is a non-linear  
optimization routine (http://www.csit.fsu.edu/~brkardt/f_src/nl2sol/ 
nl2sol.f90- original fortran is at netlib.org as ACM TOMS 573). the  
second is to use Excel's Solver.

in either case you are trying to adjust the parameters in one or more  
Lorentzians to minimize the difference between the data points and  
the fit.

the obvious metric to use is something like sum over data points i of  
| eps_data(w_i) - eps_fit(w_i) |, where eps_data(w_i) are your data  
points (complex numbers) and eps_fit(w_i) is from your sum of  
Lorentzians (also complex numbrs).  In the literature, some claim to  
first do the imaginary fit first and then use that to do the full  
fit. it really depends on the data.

using nl2sol this is straight forward. in Excel, use ToolsAdd- 
ins...ToolPak Analysis so you have support of complex numbers. after  
some excel drudgery, you can set up Solver (also an Add-in) to vary  
the parameters in your Lorentzians to minimize the metric you have  
decided on.

Steven is right, though, almost every time the schemes fail. you tend  
to have to help them along usually by giving them some reasonable  
values of the starting values. that is why i tend to do fits in  
Excel- it's easy to quickly adjust the parameters to be almost  
correct values and let Solver (hopefully) find the final solution.

for your problem, Matt, i see:

1) too few points. one Lorentz has three parameters. you have seven  
data points. fitting to such a small data set is problematic (in a  
statistical meaningful sense).
2) the sign of Im(eps) is correct?

gp

--




On Jul 8, 2007, at 1:20 PM, Steven G. Johnson wrote:

 On Tue, 3 Jul 2007, matt wrote:
 I have the following data for a lossy material (nylon), and I'd  
 like to
 find the parameters for meep's lorentzian model which fit the data:

 frequency (hz)  real(eps) imag(eps)
 60  3.7 -.0666
 1e3 3.5 -.0651
 1e6 3.14-.068452
 1e8 3.0 -.06
 3e9 2.84-.033228
 2.5e10  2.73-.028665

 There was an indication in a previous post that this is simple:

 G.J. Parker
  original patch posted by Steven, this does reduce memory  
 requirements when
  using Lorentzians. i have no problems using five Lorentzians w/  
 this patch.
  one can write a stupid excel spread sheet to fit N lorentzians  
 to a table of
  (experimental) complex epsilons...

 I'm not sure it's quite so simple, because most off-the-shelf fitting
 programs only fit a real-valued function, whereas here you have to  
 fit a
 complex-valued function.  In general, you have to use a nonlinear
 optimization program to do the fit (to minimize the sum-of-squares  
 errors
 or whatever error criterion you prefer).

 I don't have any off-the-shelf code to do this for the Lorentzian  
 problem
 (although of course there are lots of off-the-shelf nonlinear  
 optimization
 routines), but perhaps someone on the list does.

 Steven

 ___
 meep-discuss mailing list
 meep-discuss@ab-initio.mit.edu
 http://ab-initio.mit.edu/cgi-bin/mailman/listinfo/meep-discuss


___
meep-discuss mailing list
meep-discuss@ab-initio.mit.edu
http://ab-initio.mit.edu/cgi-bin/mailman/listinfo/meep-discuss


Re: [Meep-discuss] nonlinear complex fit

2007-07-05 Thread matt




Hello,

Thanks for your suggestion.  It seems that a fitting complex data is not 
possible with grace.  It's possible to fit one curve at a time (real, or 
imaginary, separately).


Best Regards,
Matt



On Tue, 3 Jul 2007, Andreas Wilde wrote:


matt wrote:



Hello,

I have the following data for a lossy material (nylon), and I'd like to
find the parameters for meep's lorentzian model which fit the data:

frequency (hz)  real(eps) imag(eps)
60  3.7 -.0666
1e3 3.5 -.0651
1e6 3.14-.068452
1e8 3.0 -.06
3e9 2.84-.033228
2.5e10  2.73-.028665

There was an indication in a previous post that this is simple:


G.J. Parker
 original patch posted by Steven, this does reduce memory requirements when
 using Lorentzians. i have no problems using five Lorentzians w/ this patch.
 one can write a stupid excel spread sheet to fit N lorentzians to a table of
 (experimental) complex epsilons...



Best Regards,
Matt


___
meep-discuss mailing list
meep-discuss@ab-initio.mit.edu
http://ab-initio.mit.edu/cgi-bin/mailman/listinfo/meep-discuss





Hello,
under Unix/Linux you could use xmgrace to fit the parameters of the
model. I don´t know if this is better or worse than using a spreadsheet,
but it should be one possibility.

andreas

___
meep-discuss mailing list
meep-discuss@ab-initio.mit.edu
http://ab-initio.mit.edu/cgi-bin/mailman/listinfo/meep-discuss

[Meep-discuss] nonlinear complex fit

2007-07-03 Thread matt



Hello,

I have the following data for a lossy material (nylon), and I'd like to 
find the parameters for meep's lorentzian model which fit the data:

frequency (hz)  real(eps) imag(eps)
60  3.7 -.0666
1e3 3.5 -.0651
1e6 3.14-.068452
1e8 3.0 -.06
3e9 2.84-.033228
2.5e10  2.73-.028665

There was an indication in a previous post that this is simple:

 G.J. Parker
  original patch posted by Steven, this does reduce memory requirements when
  using Lorentzians. i have no problems using five Lorentzians w/ this patch.
  one can write a stupid excel spread sheet to fit N lorentzians to a table of
  (experimental) complex epsilons...


Best Regards,
Matt


___
meep-discuss mailing list
meep-discuss@ab-initio.mit.edu
http://ab-initio.mit.edu/cgi-bin/mailman/listinfo/meep-discuss