function MinEnergyDataArray = analyzeGSWavefunction_optimal_system_size(folder_path, Lx_Range, Ly_Range)
    
    % Initialize matrix to store [Lx, Ly, minEnergy]
    MinEnergyDataArray = [];
    
    % Get a list of all subfolders in the parent folder
    subfolders = dir(fullfile(folder_path, 'Run_*'));
    
    % Loop through each subfolder
    for i = 1:length(subfolders)
        % Get the full path of the current subfolder
        subfolder_path = fullfile(subfolders(i).folder, subfolders(i).name);
        
        % Check if the current item is a folder
        if subfolders(i).isdir
            % Load the data file from the current folder
            data_file = fullfile(subfolder_path, 'psi_gs.mat');
            if isfile(data_file)
                % Load required variables from the .mat file
                Data = load(data_file, 'Params', 'VParams');
                
                % Extract Lx and Ly from Params.Dimensions
                Lx = Data.Params.Lx;
                Ly = Data.Params.Ly;
                
                % Extract the minimum energy from VParams.E_vs_iter
                try
                    minEnergy = min(gather(Data.VParams.E_vs_iter));
                catch
                    minEnergy = NaN;
                end
                
                % Append [Lx, Ly, minEnergy] to the results array
                MinEnergyDataArray(end + 1, :) = [Lx, Ly, minEnergy];
            else
                fprintf('Warning: File psi_gs.mat not found in %s\n', subfolder_path);
            end
        end
    end
    
    % Initialize a padded matrix with zeros
    paddedMatrix = zeros(length(Lx_Range), length(Ly_Range));
    
    % Map Lx and Ly values from minEnergyData into paddedMatrix
    for i = 1:size(MinEnergyDataArray, 1)
        Lx     = MinEnergyDataArray(i, 1);
        Ly     = MinEnergyDataArray(i, 2);
        energy = MinEnergyDataArray(i, 3);
    
        tolerance = 1e-10;  % Define a small tolerance

        % Find indices in the padded matrix corresponding to Lx and Ly with tolerance
        Lx_idx = find(abs(Lx_Range - Lx) < tolerance);
        Ly_idx = find(abs(Ly_Range - Ly) < tolerance);
    
        % Assign the energy value to the appropriate position in the matrix
        paddedMatrix(Lx_idx, Ly_idx) = energy;
    end

    figure
    clf
    set(gcf,'Position',[50 50 950 750])
    imagesc(Lx_Range, Ly_Range, paddedMatrix');
    hold on 
    set(gca, 'YDir', 'normal');              % Correct the y-axis direction
    cbar1      = colorbar;
    cbar1.Label.Interpreter = 'latex';
    ylabel(cbar1,'$E_{var}$','FontSize',16,'Rotation',270)
    xlabel('$L_x$','fontsize',16,'interpreter','latex');
    ylabel('$L_y$','fontsize',16,'interpreter','latex');
    title('Minimum variational energy for different unit cell sizes', 'FontSize', 16);
end
