function z=defaultprob(eta,c,mu,sigma,r_m,d)

%this function computes the default probability for a solution of
%problem 2.

%eta: percent of assets retained by firm in case of bankruptcy
%c  : bankruptcy cost
%mu : Mean of normal (or T-distribution)
%sigma: stdv of normal (or T-distribution)
%r_m  : reservation return to entrepreneur
%d    : percent of debt finance (1-d) is equity

%the following variable is starting point for the integration. x_lbar
%should be chosen such that almost no probability mass is on its left.

x_lbar=-10;

for ie=1:length(eta)
    for ic=1:length(c);
        for im=1:length(mu);
            for is=1:length(sigma);
                for ir=1:length(r_m);
                    for id=1:length(d);
                        bar_u=(1+r_m(ir))*(1-d(id));
                        [v_bar,x_star,payoff_lender,payoff_firm]=optimization(eta(ie),c(ic),mu(im),sigma(is),bar_u);
                        z(ie,ic,im,is,ir,id)=quad(@(x) density(x,mu(im),sigma(is)),x_lbar,x_star);
                    end;
                end;
            end;
        end;
    end;
end;

z=squeeze(z);
       
