Data show one value why all values not displying?

1 次查看(过去 30 天)
i got the different values of fid but display only show only one value of fid
  4 个评论
Matt J
Matt J 2023-3-20
See my answer. You've done nothing to retain early fid values in the loop.

请先登录,再进行评论。

采纳的回答

Sachin
Sachin 2023-3-20
编辑:Sachin 2023-3-20
I understand that you are not receiving the full output of fid. since you are not storing the output of fid in an array or vector. In each iteration fid will store a new calculated value. To store the value you have to use you can use indexing to save those values. Refer the following code :
% Define the number of states
D = 15;
% Initialize the annihilation and creation matrices
aannihilation_a = zeros(D);
creation_a = zeros(D);
aannihilation_b = zeros(D);
creation_b = zeros(D);
% Fill the off-diagonal elements of the annihilation and creation matrices
for k = 1:D-1
aannihilation_a(k,k+1) = sqrt(k);
creation_a(k+1,k) = sqrt(k);
aannihilation_b(k,k+1) = sqrt(k);
creation_b(k+1,k) = sqrt(k);
end
% Create the composite annihilation and creation operators
a1 = kron(aannihilation_a,aannihilation_b);
a2 = kron(creation_a,creation_b);
a3 = a1 - a2;
% Define the range of values for the parameter r
r_range = [0.01:0.01:0.10];
% Loop over each value of r in the range
i = 1; % to save from 1st index
for r = r_range
% Define the time evolution operator
u = exp(r*a3);
% Define the identity operator and the initial and final states
I = eye(D);
n = 10;
ket_0 = zeros(D, 1);
ket_0(1) = 1;
ket_1 = zeros(D, 1);
ket_1(2) = 1;
ket_n = zeros(D, 1);
ket_n(n+1) = 1;
bra_0 = ones(1, D);
bra_0(2:end) = 0;
bra_n_minus_1 = zeros(1, D);
bra_n_minus_1(n) = 1;
% Calculate the final state after time evolution
psi_ket = kron(I,bra_0)*u*kron(ket_n,ket_1);
psi_bra = psi_ket';
p = psi_bra*psi_ket;
psi_prime = psi_ket/sqrt(p);
% Calculate the fidelity between the final state and the target state
fid(i) = abs(bra_n_minus_1*psi_prime).^2; % store values
% Plot the fidelity as a function of r
plot(fid(i),r,'r*');
i = i+1;
hold on
end
% Set axis labels and legend
title('Fidelity vs r','fontsize',14);
xlabel('Fidelity','fontsize',14);
ylabel('r','fontsize',14);
grid on;
fid =
0.0707 0.0750 0.0795 0.0842 0.0892 0.0945 0.1001 0.1059 0.1121 0.1185
  2 个评论
Abu Zar
Abu Zar 2023-3-20
Hello sir, can we change the plot line style ,in the MATLAB above codes, because when i write 'r--*' then i only gots *

请先登录,再进行评论。

更多回答(1 个)

Matt J
Matt J 2023-3-20
编辑:Matt J 2023-3-20
That's not unthinkable, if the points in the plot were generated one at a time and discarded:
for fid=linspace(0.01,0.1185,10)
plot(fid,fid,'xr'); hold on
end; hold off; axis padded
fid
fid = 0.1185

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by