Vector must be the same length.

1 visualización (últimos 30 días)
Abu Zar
Abu Zar el 11 de Feb. de 2023
Comentada: Dyuman Joshi el 22 de Feb. de 2023
I created the m.file to plot alpha vs Vnorm but the vector is not the same length ,its request to hellp me at this stage
D=15;
tmpI=eye(D);
ket0=tmpI(:,1); %|0>
ket1=tmpI(:,2); %|1>
ket2=tmpI(:,3); %|2>
ket3=tmpI(:,4); %|3>
ket4=tmpI(:,5); %|4>
ket5=tmpI(:,6); %|5>
ket6=tmpI(:,7); %|6>
ket7=tmpI(:,8); %|7>
ket8=tmpI(:,9); %|8>
ket9=tmpI(:,10); %|9>
ket10=tmpI(:,11); %|10>
ket11=tmpI(:,12); %|11>
ket12=tmpI(:,13); %|12>
ket13=tmpI(:,14); %|13>
ket14=tmpI(:,15); %|14>
sym n
alpha1 =0.03;
ketalpha1 = symsum(exp(-abs(alpha)^2 * alpha^n / sqrt(factorial(n)) ), n, 0, 14)*(ket1+ket2+ket3+ket4+ket5+ket6+ket7+ket8+ket9+ket10+ket11+ket12+ket13+ket14);
sym n
alpha2 =0.06;
ketalpha2 = symsum(exp(-abs(alpha)^2 * alpha^n / sqrt(factorial(n)) ), n, 0, 14)*(ket1+ket2+ket3+ket4+ket5+ket6+ket7+ket8+ket9+ket10+ket11+ket12+ket13+ket14);
sym n
alpha3 =0.09;
ketalpha3 = symsum(exp(-abs(alpha)^2 * alpha^n / sqrt(factorial(n)) ), n, 0, 14)*(ket1+ket2+ket3+ket4+ket5+ket6+ket7+ket8+ket9+ket10+ket11+ket12+ket13+ket14);
sym n
alpha4 =0.12;
ketalpha4 = symsum(exp(-abs(alpha)^2 * alpha^n / sqrt(factorial(n)) ), n, 0, 14)*(ket1+ket2+ket3+ket4+ket5+ket6+ket7+ket8+ket9+ket10+ket11+ket12+ket13+ket14);
sym n
alpha5 =0.15;
ketalpha5 = symsum(exp(-abs(alpha)^2 * alpha^n / sqrt(factorial(n)) ), n, 0, 14)*(ket1+ket2+ket3+ket4+ket5+ket6+ket7+ket8+ket9+ket10+ket11+ket12+ket13+ket14);
sym n
alpha6 =0.18;
ketalpha6 = symsum(exp(-abs(alpha)^2 * alpha^n / sqrt(factorial(n)) ), n, 0, 14)*(ket1+ket2+ket3+ket4+ket5+ket6+ket7+ket8+ket9+ket10+ket11+ket12+ket13+ket14);
creation=circshift(diag(sqrt(0:1:14)),-1);
annihilation=creation';
V1=annihilation*ketalpha1-alpha1*ketalpha1;
V2=annihilation*ketalpha2-alpha2*ketalpha2;
V3=annihilation*ketalpha3-alpha3*ketalpha3;
V4=annihilation*ketalpha4-alpha4*ketalpha4;
V5=annihilation*ketalpha5-alpha5*ketalpha5;
V6=annihilation*ketalpha6-alpha6*ketalpha6;
Vnorm1=V1*V1';
Vnorm2=V2*V2';
Vnorm3=V3*V3';
Vnorm4=V4*V4';
Vnorm5=V5*V5';
Vnorm6=V6*V6';
plot(Vnorm1,alpha1,Vnorm2,alpha2,Vnorm3,alpha3,Vnorm4,alpha4,Vnorm5,alpha5,Vnorm6,alpha6);

Respuesta aceptada

Dyuman Joshi
Dyuman Joshi el 11 de Feb. de 2023
Movida: Image Analyst el 11 de Feb. de 2023
I have tidied up your code. Avoid using dynamically named variables as much as you can, it is not recommended, Indexing is much simpler and efficient.
Coming onto the issue, alpha1 is a scalar and Vnorm1 is a matrix. How exactly do you plan to plot a scalar against a matrix (or vice-versa) ?
D = 15;
tmpI = eye(D);
%sum of ket1 to ket14 is
ket = [0;ones(14,1)];
syms n
ct = 6;
creation = circshift(diag(sqrt(0:1:14)),-1);
annihilation = creation';
%pre-allocation
[ketalpha, V, Vnorm] = deal(cell(1,ct));
for k=1:ct
alpha = 0.03*k;
temp0 = symsum(exp(-abs(alpha)^2 * alpha^n / sqrt(factorial(n)) ), n, 0, 14)*ket;
ketalpha{k} = temp0;
temp1 = annihilation*temp0 - alpha*temp0;
V{k} = temp1;
Vnorm{k} = temp1*temp1';
end
  19 comentarios
Abu Zar
Abu Zar el 22 de Feb. de 2023
hello sir you know my codes ,i check values of every variables but i did not got the values of n, i put the values of n from 0 to 14.
Dyuman Joshi
Dyuman Joshi el 22 de Feb. de 2023
"but i did not got the values of n"
Because you have not assigned any value to n.
"i put the values of n from 0 to 14."
Yes, to find the value of a sum, with respect to a variable. But the value of n has not been explicitly defined.

Iniciar sesión para comentar.

Más respuestas (0)

Community Treasure Hunt

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

Start Hunting!

Translated by