function [par_out,rho_out]=appr_lor_grad(ha,fra,pwa,h0,par_in,lin_dsc,ar_dsc,dlt_par_0, eps_par,cf_pw_rat)
par_out=zeros(1,4)-1e11;
rho_out=zeros(1,2)-1e11;
sz_fr=size(fra);
L_h=sz_fr(1);
w_fr=sz_fr(2);
key_grad=1;
k_grad=0;
dlt_par=dlt_par_0(1:4);
while key_grad
    f1=par_in(1);
df=par_in(2);
k=par_in(3);
alf=par_in(4);
df1=dlt_par(1);
ddf=dlt_par(2);
dk=dlt_par(3);
dalf=dlt_par(4);
f1_ar=[f1,f1-df1,f1+df1,f1,f1,f1,f1,f1,f1];
df_ar=[df,df,df,df-ddf,df+ddf,df,df,df,df];
k_ar=[k,k,k,k,k,k-dk,k+dk,k,k];
alf_ar=[alf,alf,alf,alf,alf,alf,alf,alf-dalf,alf+dalf];
for i_par=1:9
    f1c=f1_ar(i_par);
    dfc=df_ar(i_par);
    kc=k_ar(i_par);
    alfc=alf_ar(i_par);
    y=f1c+dfc*(kc*(ha-h0).^alfc./(kc*(ha-h0).^alfc+1));
    y1=y-lin_dsc(1);
    y2=y+lin_dsc(1);
    y_up=y2+ar_dsc(1);
    pw_lin=zeros(L_h,w_fr);
    pw_up=zeros(L_h,w_fr);
    for i_col=1:w_fr
        f_lin=[];
        f_up=[];
    f_lin=find(fra(:,i_col)>=y1 & fra(:,i_col)<y2);
    f_up=find(fra(:,i_col)>=y2 & fra(:,i_col)<y_up);
    L_lin=length(f_lin);
    L_up=length(f_up);
            pw_up(f_up,i_col)=pwa(f_up,i_col);
        pw_lin(f_lin,i_col)=pwa(f_lin,i_col);
        %rho_lin(i_par)=sum(pw_lin)/2/lin_dsc(1);
    
    end
    rho_lin(i_par)=sum(sum(pw_lin))/2/lin_dsc(1);
    rho_up(i_par)=sum(sum(pw_up))/(lin_dsc(1)+ar_dsc(1));
end
    f_i_par=find(rho_lin>0);
    L_i_par=length(f_i_par);
    if L_i_par>0
        k_grad=k_grad+1;
        rho_lin_ok=rho_lin(f_i_par);
        rho_up_ok=rho_up(f_i_par);
        if k_grad==1
        rho_lin_av=sqrt(mean(rho_lin_ok.^2));
        end
        
        rho_lin_n=rho_lin_ok/rho_lin_av;
        rho_rel_ok=rho_lin_ok./rho_up_ok;
        if k_grad==1
        rho_rel_av=sqrt(mean(rho_rel_ok.^2));
        end
        rho_rel_n=rho_rel_ok/rho_rel_av;
        rho_tot=cf_pw_rat*rho_lin_n+(1-cf_pw_rat)*rho_rel_n;
                   
            
        [rho_max,ii_rho_max]=max(rho_tot);
        i_rho_max=f_i_par(ii_rho_max);
           
        
        
        f_eps=find(dlt_par<=eps_par);
        L_eps=length(f_eps);
        if L_eps==4
            key_grad=0;
        elseif i_rho_max==1 
            f_gps=find(dlt_par>eps_par);
            dlt_par(f_gps)=dlt_par(f_gps)/2;
        else
            
            i_par_gr=floor(i_rho_max/2);
            par_in(i_par_gr)=par_in(i_par_gr)-dlt_par(i_par_gr)*(-1)^i_rho_max;
           
        end    
    end
end
    par_out=[f1_ar(i_rho_max),df_ar(i_rho_max),k_ar(i_rho_max),alf_ar(i_rho_max)];
            rho_out=[rho_lin(i_rho_max),rho_up(i_rho_max)];
    
