MATLAB Answers

Plotting a function in a range of variable

24 ビュー (過去 30 日間)
mk_ballav
mk_ballav 2014 年 11 月 2 日
Commented: mk_ballav 2014 年 11 月 3 日
I am having a problem defining a variable and plotting a function in a range of variable. I want to plot trace(Mt) in the range alpha=0:0.1:2; but Matlab is not allowing me to do it. Can anyone help me on this? The code is attached here.
syms alpha n1p = 0+1i; n1=1; n2=2; m = 10; gamma =(n1p*alpha)./n1;
beta = -1i.*log(1/16.*exp(-2i.*alpha).*(9-2.*exp(2i.*alpha)+9.*exp(4i.*alpha)+3.*(-1+exp(2i.*alpha)).*sqrt(9+14.*exp(2i.*alpha)+9.*exp(4i.*alpha))));
a = exp(1i.*alpha).*(n1+n2)./(2);
b = exp(-1i.*alpha).*(n2-n1)./(2); c = exp(1i.*alpha).*(n2-n1)./(2); d = exp(-1i.*alpha).*(n1+n2)./(2); e = (1i.*(2-m).*beta); M1= [a./n2 b./n2;c./n2 d./n2]; M2 = [a./n1 -b./n1;-c./n1 d./n1]; M1p = [(exp(1i.*gamma).*(n1p+n2)./(2.*n2)) (exp(-1i.*gamma).*(n2-n1p)./(2.*n2));(exp(1i.*gamma).*(n2-n1p)./(2.*n2)) (exp(-1i.*gamma).*(n1p+n2)./(2.*n2))]; M2p = [(exp(1i.*alpha).*(n1p+n2)./(2.*n1p)) (exp(-1i.*alpha).*(n1p-n2)./(2.*n1p));(exp(1i.*alpha).*(n1p-n2)./(2.*n1p)) (exp(-1i.*alpha).*(n1p+n2)./(2.*n1p))]; T = M2*M1; [W,D] = eig(T); X = inv(W); UN = [exp(e) 0;0 exp(-e)]; Tp = M2*M1p*M2p*M1; Tt = W*UN*X; Mt = Tt*Tp;

採用された回答

Roger Stafford
Roger Stafford 2014 年 11 月 3 日
If you want to do plotting with 'alpha' in the range alpha=0:0.1:2, you should not declare 'alpha' as a symbolic variable. Instead, you need to place all the above code in a for-loop like this:
al = 0:0.1:2;
for k = 1:length(al)
alpha = al(k);
..... Do all the computations with alpha up to Mt
t(k) = trace(Mt);
end
plot(al,t)
I assume by 'trace' you mean the sum of the diagonal elements of a square matrix. By the way, you should not use that name for other purposes such as the name of a file: "trace.m". Otherwise you will confuse matlab and it may misbehave.
For future reference: In posing such questions as this you should clearly describe in detail the troubles you are having rather than such vague statements as "Matlab is not allowing me to do it." Carefully copy any error messages including the lines they refer to.

  1 件のコメント

mk_ballav
mk_ballav 2014 年 11 月 3 日
Thanx and I will do as your advice in future

サインイン to comment.

More Answers (0)

サインイン してこの質問に回答します。


Translated by