function generate_laffer_curve()



% Assigning parameters
crra              = 1/2  ;
wage              = 1.0  ;
interest_rate     = 1.04 ;
capital           = 0.0  ;

% Creating a structure of parameters
parameters.crra              = crra              ;
parameters.wage              = wage              ;
parameters.interest_rate     = interest_rate     ;
parameters.capital           = capital           ;


% Calculating government revenue
n_taxrates = 15                         ;
tax_rates  = linspace(0,.99,n_taxrates) ;

% Preallocating government revenue
government_revenues = zeros(1, n_taxrates);

% Solving government revenue on a grid of tax rates
for it = 1:n_taxrates
    
    initial_guess                    = 0.15                                                                                       ;
    [optimal_labor, optimal_utility] = fminsearch(@(labor) calculate_utility( labor, tax_rates(it), parameters), initial_guess ) ;
    optimal_utility                  = -1*optimal_utility                                                                        ; %#ok<NASGU>
    
    government_revenues(it)          = tax_rates(it) * parameters.wage * optimal_labor;
    
end

plot(tax_rates,government_revenues)


% Defining the tax revenue function for a given tax rate
tax_revenue_linear = @(x) interp1(tax_rates,government_revenues,x, 'linear');
tax_revenue_spline = @(x) interp1(tax_rates,government_revenues,x, 'spline');

% Solving government-revenue-maximizing tax rate
initial_guess = 0.25;
maximizing_rate_linear = fminsearch(@(x) -1*tax_revenue_linear(x), initial_guess);
maximizing_rate_spline = fminsearch(@(x) -1*tax_revenue_spline(x), initial_guess);

fprintf('The government-revenue-maximizing tax rate using linear interpolation is %0.3f.\n',maximizing_rate_linear)
fprintf('The government-revenue-maximizing tax rate using spline interpolation is %0.3f.\n',maximizing_rate_spline)



end


% Creating objective function

function utility = calculate_utility( labor, tax_rate, parameters )
    
    consumption = parameters.wage*(1-tax_rate) * labor + parameters.interest_rate * parameters.capital               ;
    
    utility     = ( consumption^parameters.crra ) / parameters.crra  - labor ;
    
    utility     = -1 * utility                                                                                       ;    % Converting into minimization problem.
    
end