Vector must be the same length.
显示 更早的评论
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);
采纳的回答
Dyuman Joshi
2023-2-11
移动:Image Analyst
2023-2-11
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 个评论
Hello sir,your code is very helpful. Please, how to put the another's values 0.06,0.09,0.12,0.15,0.18 for alpha? I want to find the plot of alpha, Vnorm.
I think the plot will if I find the numeric value. How I can find the numeric value of for plot? For example of the plot given below
xlist=1:1:10
xlist = 1×10
1 2 3 4 5 6 7 8 9 10
for id=1:10
ylist(id)=xlist(id)*2;
end
plot(xlist, ylist, 'b.-', 'MarkerSize', 20)
grid on

"Please, how to put the another's values 0.06,0.09,0.12,0.15,0.18 for alpha?"
I have already taken care of that in the code, as you can see below
ct=6;
for k=1:ct
alpha = 0.03*k
So, here
ketalpha{1} is ketalpha1, ketalpha{2} is ketalpha2 and so on,
Similarly, V{1} is V1, V{2} is V2, ... Vnorm{1} is Vnorm1, Vnorm{2} is Vnorm2 ...
"I want to find the plot of alpha, Vnorm."
As I mentioned earlier, alpha is a scalar and Vnorm is a matrix.
How exactly do you plan to plot a scalar against a matrix? The example you have shown is plotting vector vs vector.
What exactly are you trying to do?
Thank you very much.
basically i want to do this two question 

With the result you want to get, I think you have the multiplication order incorrect -
D = 15;
tmpI = eye(D);
%sum of ket1 to ket14 is
%generalised this line of code for any value of D
ket = sum(tmpI(:,2:D),2);
syms n
ct = 6;
creation = circshift(diag(sqrt(0:1:(D-1))),-1);
annihilation = creation';
%pre-allocation
%[ketalpha, V, Vnorm] = deal(cell(1,ct));
Vnorm = zeros(1,ct);
alpha = 0.03*(1:ct);
%loop for calculation
for k = 1:ct
temp0 = symsum(exp(-abs(alpha(k))^2 * alpha(k)^n / sqrt(factorial(n)) ), n, 0, 14)*ket;
%ketalpha{k} = temp0;
temp1 = annihilation*temp0 - alpha(k)*temp0;
%V{k} = temp1;
Vnorm(k) = temp1'*temp1;
end
plot(alpha,Vnorm, '-*k')
xlim([0 0.21])
xticks(0:0.03:0.21)
xlabel('alpha', 'Color', 'b', 'FontSize', 15)
ylabel('Vnorm', 'Color', 'b', 'FontSize', 15)

it's outstanding.
thank you very much.
You are welcome!
Hi,
the temp1>>1,i want to evaulate the temp1<<1,
I try to use vpa(temp) then double(vpa(temp) but i did not get my result.
can i get my result if i use any other technique in this m.file.
thanks.
I dont understand what you are trying to convey.
let me explain little again
i want Temp1’*temp1 should be << 1 but my value Temp1’*temp1 >>1 ,also plot show this,
Can you check it?
somone recommend me to use a column vector to define temp0. For example.
temp0=zeros(D,1);
for id =0:D-1
Temp0(id)=...
end
Then, you can give a numeric result.
i try my self but not know how to do this above recommandtion.
Dyuman Joshi
2023-2-16
编辑:Dyuman Joshi
2023-2-16
"my value Temp1’*temp1 >>1"
Yes the values are big, in the order of 10^4, do you mean to say this?
"i want Temp1’*temp1 should be << 1"
For that you will have to change the data. For the given data, you can't modify the outcome.
"Then, you can give a numeric result."
Which result?
Hello Sir,
please help me to put follow this message in these codes ;
---->Temp0 is a sum from n=0 to n=14. try the following and find the result, please.
for n=0:14
temp0 =temp0+ double(symsum(exp(-0.5*abs(alpha(k))^2)* alpha(k)^n / sqrt(factorial(n)), n, 0, 14)*ket);
end
Thank you
Message? as in a comment?
Or do you want to include this in the code?
Yes I want to include 🙏 Thank you
D = 15;
tmpI = eye(D);
%sum of ket1 to ket14 is
%generalised this line of code for any value of D
ket = sum(tmpI(:,2:D),2);
syms n
ct = 6;
creation = circshift(diag(sqrt(0:1:(D-1))),-1);
annihilation = creation';
%pre-allocation
Vnorm = zeros(1,ct);
alpha = 0.03*(1:ct);
temp0=0;
%loop for calculation
for k = 1:ct
temp0 = temp0 + symsum(exp(-abs(alpha(k))^2*alpha(k)^n/sqrt(factorial(n))), n, 0, 14)*ket;
temp1 = annihilation*temp0 - alpha(k)*temp0;
Vnorm(k) = temp1'*temp1;
end
plot(alpha,Vnorm, '-*k')
xlim([0 0.21])
xticks(0:0.03:0.21)
xlabel('alpha', 'Color', 'b', 'FontSize', 15)
ylabel('Vnorm', 'Color', 'b', 'FontSize', 15)

Sir,in this way i improved my plot this becoming less then 1 but last value is very large ,please help me to solve this point,you can see my plot.
D = 15;
tmpI = eye(D);
%sum of ket1 to ket14 is
%generalised this line of code for any value of D
ket = sum(tmpI(:,1:D),1)';
syms n
ct = 6;
creation = circshift(diag(sqrt(0:1:(D-1))),-1);
annihilation =creation';
%pre-allocation
%[ketalpha, V, Vnorm] = deal(cell(1,ct));
Vnorm = zeros(1,ct);
alpha = 0.03*(1:ct);
temp0=0;
%loop for calculation
for k = 1:ct
temp0 =14*temp0+double(symsum(exp(0.5*(-abs(alpha(k))^2))* alpha(k)^n / sqrt(factorial(n)), n, 0, 14)*ket);
%ketalpha{k} = temp0;
%temp2= annihilation*temp0;
temp1 = annihilation*temp0 - alpha(k)*temp0;
%V{k} = temp1;
Vnorm(k) = temp1'*temp1;
end
plot(alpha,Vnorm, '-*k')
xlim([0 0.21])
%xticks(0:0.03:0.21)
xlabel('alpha', 'Color', 'b', 'FontSize', 15)
ylabel('Vnorm', 'Color', 'b', 'FontSize', 15)

You changed the definition of the variable 'ket', I don't know why.
At this point, I do not know what you are trying to achieve.
If you are doing a Homework question, copy and paste the original question. Otherwise, I don't understand what you are trying to convey.

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.
"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.
更多回答(0 个)
类别
在 帮助中心 和 File Exchange 中查找有关 Data Distribution Plots 的更多信息
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!选择网站
选择网站以获取翻译的可用内容,以及查看当地活动和优惠。根据您的位置,我们建议您选择:。
您也可以从以下列表中选择网站:
如何获得最佳网站性能
选择中国网站(中文或英文)以获得最佳网站性能。其他 MathWorks 国家/地区网站并未针对您所在位置的访问进行优化。
美洲
- América Latina (Español)
- Canada (English)
- United States (English)
欧洲
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
