function [par_new,h_h, H_22]=appr_2_lor_from_2int(pwr,h_e,fr_e,appr_dsc,h0,h1_int,h2_int,fr_w_d,cf_lin_1,cf_lin_2)
% reconstructs lorentzian function from two linear segments
par_new=zeros(1,4)-1e11;
C2=cf_lin_2(1);
C1=cf_lin_1(1);
c_f=appr_dsc(1);
N_c_f=appr_dsc(2);
d_c_f=c_f/N_c_f;
c_d=appr_dsc(3);
N_c_d=appr_dsc(4);
d_c_d=c_d/N_c_d;
d_c_f_min=c_f*appr_dsc(5);
d_c_d_min=c_d*appr_dsc(6);
num_itr=appr_dsc(7);

 key_appr=1;
 
 i_itr=0;

gr_1=subplot(2,1,2);
 while key_appr
     cf_ar=zeros(5,2);
cf_ar(1,:)=[c_f,c_d];
for i1=1:2
    for i2=1:2
        i_r=i1*2+i2-1;
        cf_ar(i_r,1)=c_f+d_c_f*(-1)^i1;
        cf_ar(i_r,2)=c_d+d_c_d*(-1)^i2;
    end
end
H_22=(h2_int(2)-h0)/exp(1);
H1=(h1_int(1)-h0)/H_22;
H12=(h1_int(2)-h0)/H_22;
H1_int=[H1,H12];
H2=(h2_int(1)-h0)/H_22;
H22=(h2_int(2)-h0)/H_22;
H2_int=[H2,H22];
Hc=(H1+H12)/2;
S2=cf_lin_2(2)*H_22;
S1=cf_lin_1(2)*H_22;
R12=H1/H2;
Rc2=Hc/H2;
% initial approximation
cf_arc=zeros(5,3);
for i_r=1:5
    
    c_f=cf_ar(i_r,1);
    c_d=cf_ar(i_r,2);
S0=S1*c_d;
F0=C1-S0*H1;
F2=C2+S2*(H22-H2);
DF=F2*c_f-F0;

Cc=C1+S1*(Hc-H1);
C12=C1+S1*(H12-H1);
C22=C2+S2*(H22-H2);
C1_int=[C1,C12];
C2_int=[C2,C22];
kh1_alf=(C1-F0)/DF;
kh2_alf=(C2-F0)/(DF-C2+F0);
alf=log(kh2_alf/kh1_alf)/log(H2/H1);
k=kh2_alf/H2^alf;

f_h=find(h_e>h0);
h_h=h_e(f_h);
H_h=(h_h-h0)/H_22;
fr_h=F0+DF*(k*H_h.^alf)./(1+k*H_h.^alf);

set(gr_1,'nextplot','replace','xgrid','on','ygrid','on');
pl_a=plot(h_h,fr_h);
set(pl_a,'color','y','linewidth', 2);
set(gr_1,'nextplot','add');
plot(h1_int,C1_int,'r');
plot(h2_int,C2_int,'r');
df1=DF*alf*k*Hc^(alf-1)/(k*Hc^alf+1)^2;
df2=DF*alf*k*H2^(alf-1)/(k*H2^alf+1)^2;
R=sqrt((df1/S1-1)^2+(df2/S2-1)^2);
cf_arc(i_r,:)=[df1,df2,R];
end
c_f=cf_ar(1,1);
c_d=cf_ar(1,2);
[cf_by, i_cf_by]=min(cf_arc(:,3));
switch i_cf_by
    case 0
        d_c_f=d_c_f/N_c_f;
        d_c_d=d_c_d/N_c_d;
    case 1
        d_c_f=d_c_f/N_c_f;
        d_c_d=d_c_d/N_c_d;
    case 2
        c_f=c_f-d_c_f;
        c_d=c_d-d_c_d;
    case 3
        c_f=c_f-d_c_f;
        c_d=c_d+d_c_d;
    case 4
        c_f=c_f+d_c_f;
        c_d=c_d-d_c_d;
    case 5
        c_f=c_f+d_c_f;
        c_d=c_d+d_c_d;
end
if d_c_f<=d_c_f_min && d_c_d<=d_c_d_min || i_itr>num_itr
    key_appr=0;
else
    i_itr=i_itr+1;
end
 end
 par_new=[F0,DF,k,alf];

key_itr=0;
if key_itr>0
% Iterations
for i_h=1:L_e_h
    fr_c=FR_pl(i_h);
    fr_d=fr_c-fr_w_d(1);
    fr_u=fr_c+fr_w_d(2);
    f_bnd=find(fr_e>=fr_d & fr_e<=fr_c);
    f_pln=find(fr_e<=fr_u & fr_e>fr_c);
    fr_bnd=fr_e(f_bnd);
    fr_pln=fr_e(f_pln);
    pwr_bnd=pwr_e_h(f_bnd,i_h);
    pwr_pln=pwr_e_h(f_pln,i_h);
    [Pln_bnd]=polyfit(fr_bnd',pwr_bnd,1);
    [Pln_pln]=polyfit(fr_pln',pwr_pln,1);
end
end
end
