everyone! I've got a model which consist of one equation with many terms. So basically the question is how better to fit real data to model adjusting several (3-6) parameters. I'll explian physics a little bit just to show the purpose of the efforts.
The used model was published here 1 (equation 9 is result). It deals with volume bragg gratings and describes a diffraction efficiency of such gratings. So the idea is to creat such volume grating somehow and then find out several optical parameters from one curve. Usually the curve looks like this
As I said this plot was obtainded experimentally and by fitting several parameters of equation form 1 it possible to fit model and estimate parameters of grating. The most important are following: refractive index modulation - dn, absorption modulation - a1 and thicknes of grating - d.
Firstly, I've tried to fit some simple function like exponential and gauss with 2 parameters and randomly generated array of data. It worked pretty well with function fit(). But in my case there are at least 5 terms and each of them involves sinh, cosh, sin, cos, exp etc. Anyway for me it kind of complicated and I think maybe someone knows a better approach to find parameters for big equations. Consiquently, I expect to select about 6 parameters for this model, so the task for fit function gonna be even more complicated.
Here is part of my MATLAB code just give you a taste what I'm doing.
mdl=@(dn_,a1_,d_, x) zfit(dn_,a1_,d_, x); algo1 = 'Trust-Region'; p_low = [1e-4 1*cm_1 0.5*mili ]; p_up = [1e-3 50*cm_1 1.1*mili ]; p = [1e-3 5*cm_1 1*mili ]; fit_opt = fitoptions('Method','NonlinearLeastSquares', ... 'Lower',p_low,... 'Upper',p_up,... 'Robust','off',... 'Normalize','off',... 'Algorithm',algo1); fit_typ = fittype(mdl,'option',fit_opt); [Yfitt,gof,output] = fit(k,z,fit_typ,'Start',p)
and zfit function itself
function [ y ] = zfit( dn, a1,d, x )
% units
grad = pi / 180;
m = 1; % meter;
nano = 1e-9 * m;
santi = 1e-2 * m;
mili = 1e-3 * m;
cm_1 = 1/santi; % cm in 1 meter
% parameters
a2 = 1; % absorption of material
x0 = 1.311e-5; % shift of angle
y0 = 15.78; % amplitude, just to adjust exper. data
kf = 38.12; % stretching factor, just to adjust exper. data
n = 1.498; % refractive index
teta1 = 15.7 * grad; % angle between beams
lambda = 632.8 * nano; % wavelength
teta2 = asin(sin(teta1 + x + x0) / n); % x axis data. different angles
tetab = asin(sin(teta1) / n); % bragg angle
k1 = pi * dn / lambda;
k2 = a1 / 2;
%% equations from [1]
v = 4 * pi * n * sin(tetab) / lambda * (sin(teta2) - sin(tetab));
z0 = sqrt((v.^2 + 4 * (k1^2 - k2^2)).^2 + (8 * k1 * k2).^2);
phi0 = acos((-(v.^2 + 4 * (k1^2 - k2^2))) ./ z0);
W1 = sqrt(z0) * d .* cos(phi0/2) ./ cos(teta2);
W2 = sqrt(z0) * d .* sin(phi0/2) ./ cos(teta2);
A = ((v / 2).^2 + z0 / 4) * 2 .* cosh(W1);
B = ((v / 2).^2 - z0 / 4) * 2 .* cos(W2);
C = v .* sqrt(z0) .* sin(phi0/2) .* sinh(W1);
D = v .* sqrt(z0) .* cos(phi0/2) .* sin(W2);
% final euation
y = y0 + exp(-2 * a2 * d ./ cos(teta2)) ./ z0 ...
.* (A - B + C - D)*kf;
end