clear all;
% Вектор параметров
%      thetab* gammab* sigmaB  xiB=        =xiP   (5) beta   zeta   zeta0    varkappa    alphaK (10) alphaM delta  eta omega kappaI (15)     kappaB=kappaH=kappaP varphi Q (20)       R          Rstar; A; vareps Lambda (25)     mc        pi thetap gammap    tauK (30) tauDstar tauN   sigmaP Ystar    Pstar     35 ibar rhoi OmegaPi             
Par = [0.475;    6.4;   0.494;  0.000588; 0.000588;    0.985; 0.2;   5.89;    0.000985;    0.3;       0.15;  0.98;   9; 0.66;   1;             0.05; 0.05; 0.05;       1; 1;         1.01522843;  1.005; 1; 1;    0.985;         0.88888889; 1; 0.475; 1;       0.01;         0.01;      0.01;     0.94;    0.412053; 1;1;0.85;1.5] 
%*содержат дублеров далее
% Вектор переменных
%         Y       K            Kb       Kp     L 5          M    w %         Vp     Nb     Np     10 x     z         ThetaB ThetaP Bb 15 Bh         Dstar       phi     psi       Omega 20 mu       muDstar mustar Rk         nu 25     I        Ex         eps C       P 30 g D i Ibar
%x0=[2.951663; 19.578389; 14.603804; 4.974586;0.309719;0.393555;4.659174;   1; 2.473792;2.473792;0.255225;0.040203;  1;      1;   1;     1; 3.727254;5.903407;3.388629;3.196631; 0.015902;0.032815;2;     1; 3.245311;0.391568; 0.412053; 1;2.135855;1;     1 ;8.402758;1;1]

%Lower limit 33*zero
lb_k=-100000000.01
lb=[lb_k;lb_k;lb_k;lb_k;lb_k;lb_k;lb_k;lb_k;lb_k;lb_k;lb_k;lb_k;lb_k;lb_k;lb_k;lb_k;lb_k;lb_k;lb_k;lb_k;lb_k;lb_k;lb_k;lb_k;lb_k;lb_k;lb_k;lb_k;lb_k;lb_k;lb_k;lb_k;lb_k;lb_k]
%Upper limit 32*1000
%ub=[100;100;100;100;100;100;100;100;100;100;100;100;100;100;100;100;100;100;100;100;100;100;100;100;100;100;100;100;100;100;100;100]
ub_k=100000
ub=[ub_k;ub_k;ub_k;ub_k;ub_k;ub_k;ub_k;ub_k;ub_k;ub_k;ub_k;ub_k;ub_k;ub_k;ub_k;ub_k;ub_k;ub_k;ub_k;ub_k;ub_k;ub_k;ub_k;ub_k;ub_k;ub_k;ub_k;ub_k;ub_k;ub_k;ub_k;ub_k;ub_k;ub_k]



f = @(x) mm(x,Par);
%initial x0=[2.951663; 19.578389; 14.603804; 4.974586;0.309719;0.393555;4.659174;   1; 2.473792;2.473792;0.255225;0.040203;  1;      1;   1;     1; 3.727254;5.903407;3.388629;3.196631; 0.015902;0.032815;2;     1; 3.245311;0.391568; 0.412053; 1;2.135855;1;     1 ;8.402758;1;1];
x0=[2.951663; 19.578389; 14.603804; 4.974586;10.309719;0.393555;4.659174;   1; 2.473792;2.473792;10.255225;10.040203;  1;      1;   1;     1; 3.727254;5.903407;3.388629;3.196631; 10.015902;10.032815;2;     1; 3.245311;10.391568; 10.412053; 1;2.135855;1;     1 ;8.402758;1;1];
%                         базовые настройки      общ.число прогона функ.  циклов в итер.  проверка на real
options = optimoptions('lsqnonlin','Display','iter','MaxFunctionEvaluations',200000000,'MaxIter',40000, 'FunValCheck','on','StepTolerance',0.00001);
%[x,fval] = lsqnonlin(f,x0,lb,ub,options);
[x,fval] = lsqnonlin(f,x0,[],[],options);
FILE=fopen('ouput_x.txt','w','n');
VarNames=["Y","K","Kb","Kp","L","M","w","Vp","Nb","Np","x","z","Thetab","ThetaP","Bb","Bh","Dstar","phi","psi","Omega","mu","mudstar","mustar","Rk","nu","I","Ex","eps","C","P","g","D","i", "Ibar"]

for i=1:34
    fprintf(FILE,'%s = %5.10f; \n',VarNames(i),x(i));
end
j=0
%for i=1:37
    %j=j+1;
    %fprintf('%i\n%f\n',j,f(x));
    %fprintf('iter %i\n',i);
   fprintf(' %f\n',f(x));
%end
%fprintf(FILE,'%5.10f \n',x);
%ll=f(x);
%ll1=ll;

%x1=[2.951663; 19.578389; 14.603804; 4.974586;0.309719;0.393555;4.659174;   1; 2.473792;2.473792;0.255225;0.040203;  1;      1;   1;     1; 3.727254;5.903407;3.388629;3.196631; 0.015902;0.032815;2;     1; 3.245311;0.391568; 0.412053; 1;2.135855;1;     1 ;8.402758;5];
%[x,fval] = lsqnonlin(f,x1,lb,ub,options);
%ll2=f(x);
%ll_diff=ll2;
%for i=1:33
%    ll_diff(i)=ll1(i)-ll2(i);
%end

%options = optimoptions('fsolve','Algorithm','Levenberg-Marquardt','Display','iter','MaxFunEvals',800000,'MaxIter',30000);
%[x,fval] = fsolve(f,x0,options);
%for i=1:33
%    fprintf('%s = %5.10f; \n',VarNames(i),x(i));
%end
%R * Lambda - 1; deleted
%Lambda - beta; deleted
%Q=1 deleted
% Equation for i R pi has been deleted as trash, because no one else (i)
