sannolikhetsfordelning

mail@pastecode.io avatar
unknown
matlab
a year ago
985 B
2
Indexable
numSteps = 100;    
numWalks = 10000;

endPositions = zeros(1, numWalks);

for i = 1:numWalks
    steps = randsrc(1, numSteps, [-1, 1]);   
    endPositions(i) = sum(steps);           
end

[x_counts, bin_positions] = histcounts(endPositions, 'Normalization', 'probability');

bin_centers = (bin_positions(1:end-1) + bin_positions(2:end))/2;

plot(bin_centers, x_counts, 'LineWidth', 2);
hold on;

C = [1, 1; 1, -1] / sqrt(2);


psi = zeros(2, 2*numSteps+1);
psi(:, numSteps+1) = [1, 1i] / sqrt(2); 

for step = 1:numSteps
    psi = C * psi;
    psi = [circshift(psi(1,:), [0, 1]); circshift(psi(2,:), [0, -1])];
end

prob_distribution = sum(abs(psi).^2, 1);
mask = mod([-numSteps:numSteps], 2) == 0; 
positions = [-numSteps:numSteps];
prob_distribution_masked = prob_distribution .* mask;

plot(positions(mask), prob_distribution_masked(mask), 'LineWidth', 2);

xlabel('Position');
ylabel('Sannolikhet');
legend('Klassisk Random Walk', 'DTQW', 'Location', 'Best');
grid on;

hold off;