Creating matrices with elements as a function of kroneckerDelta

Hello everyone,
I want to create some spin matrices for which I know the elements as a function of matrix index and kroneckerDelta function. The functions are given here Spin Matrices.
Here's the snippet of the code:
S = 1;
syms m n
for m = -S:1:S
for n = -S:1:S
sx{m,n} = (kroneckerDelta(m,n+1) + kroneckerDelta(m+1,n))*sqrt(S*(S+1) - m*n)/2;
sy{m,n} = (kroneckerDelta(m,n+1) - kroneckerDelta(m+1,n))*sqrt(S*(S+1) - m*n)/(2*1i);
sz{m,n} = kroneckerDelta(m,n)*m;
s_plus{m,n} = kroneckerDelta(m,n+1)*sqrt(S*(S+1) - m*n);
s_minus{m,n} = kroneckerDelta(m+1,n)*sqrt(S*(S+1) - m*n);
end
end
The snippet gives errors in kroneckerDelta Function and the indices of the matrices. Can anyone help me in debugging this?

4 件のコメント

M
M 2018 年 1 月 11 日
Does It gives error mean that the result is not the one you expected or does the function return an error ? If yes, can you please give the complete error message ?
I think kroneckerDelta takes syms variables as input, but you are giving double.
Jitendra Kumar Singh
Jitendra Kumar Singh 2018 年 1 月 11 日
Yeah there is error in kroneckerDelta. But I already made m and n symbolic, then why does it give the argument error.
Error- Undefined function 'kroneckerDelta' for input arguments of type 'double'.
There is an error on the indices as well.
Error-Subscript indices must either be real positive integers or logicals.
I need the negative indices because the matrix elements are functions of indices.
Torsten
Torsten 2018 年 1 月 11 日
編集済み: Torsten 2018 年 1 月 11 日
By assigning values to m and n
for m = -S:1:S
for n = -S:1:S
m and n become double.
Best wishes
Torsten.
Jitendra Kumar Singh
Jitendra Kumar Singh 2018 年 1 月 11 日
Here is how I fixed the code. Thank you everyone for your comments.
clearvars
S = 1;
for m = 1:1:(2*S+1)
for n = 1:1:(2*S+1)
sx{m,n} = (double(eq(m, n+1)) + double(eq(m+1, n)))*sqrt(S*(S+1) - m*n)/2;
sy{m,n} = (double(eq(m, n+1)) - double(eq(m+1, n)))*sqrt(S*(S+1) - m*n)/(2*1i);
sz{m,n} = double(eq(m, n))*m;
s_plus{m,n} = double(eq(m, n+1))*sqrt(S*(S+1) - m*n);
s_minus{m,n} = double(eq(m+1, n))*sqrt(S*(S+1) - m*n);
end
end

サインインしてコメントする。

回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeMultidimensional Arrays についてさらに検索

タグ

質問済み:

2018 年 1 月 11 日

Community Treasure Hunt

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

Start Hunting!

Translated by