I am trying to find the competitive equilibrium in OLG using the following code:

function F = OLG_closed_tax2(x)

%global variables

global delta beta a0 b0 alpha Z T tauk taul

% Constants

V = 8;

% Preallocate F

F = zeros(V*T,1);

% Decision variables

C_young = x(1:V:T*V); % Consumption for young
L_young = x(2:V:T*V); % Leisure for young

C_old = x(3:V:T

*V); % Consumption for old*

K = x(4:V:TV); % Capital

K = x(4:V:T

Aprime = x(5:V:T

*V); % Asset or savings*

tauk = x(6:V:TV); %tax on capital

tauk = x(6:V:T

taul = x(7:V:T

*V); %tax on labour*

G = x(8:V:TV); %Government Spending

G = x(8:V:T

% L_t and r are recalculated, not directly taken from x

L_t = 1 - L_young; % Labour for young, directly calculated

% Adjustments and calculations

A = [a0; Aprime]; % Adjustments to include initial asset and shift for correct dimension

% Utility functions and wages/returns calculations

uc_y = 1 ./ C_young; % Marginal utility of consumption by young

ul_y = 1 ./ L_young; % Marginal utility of leisure by young

uc_o = 1 ./ C_old; % Marginal utility of consumption by old

r = (alpha .* K.^(alpha-1) .* L_t.^(1-alpha)).*Z^(1-alpha) - delta; % Rate of return
w = (1-alpha) .* K.^alpha .* L_t.^(-alpha).*Z^(1-alpha); % Wages

% System of equations

F(1:V:V*T) = (1-taul(1:T)) .* w .* L_t - Aprime - C_young; % Budget constraint for young

F(2:V:V*T) = A(1:T) .* (1 + (r(1:T).*(1-tauk(1:T)))) - C_old(1:T); % Budget constraint for old
F(3:V:V*T) = uc_y .* (w .

*(1-taul(1:T))) - ul_y; % Labor-leisure condition*

F(4:V:V(T-1))= beta .* (1 + ((r(2:T)-delta).

F(4:V:V

*(1-tauk(2:T)))) .*uc_o(2:T) - uc_y(1:T-1);% Euler equation for consumption smoothing

F(5:V:V

*T) = A(1:T) - K(1:T); % Asset market clearing*

F(V(T-1)+4) = A(T+1) - A(T); % No Ponzi condition

F(V

F(6:V:V

*T) = w .*L_t .* taul + r .* tauk .* K - G;

end

Everything is fine, except I am getting a bit deviation towards the end. What could be the issue?