0
$\begingroup$

I wanted to create a matrix by following way:

NN = 3;

M[x_, J2_, J3_, a_]:=
  With[
    {d = (1. + J3)/2.,
     f = (1.+ Cos[a])/2.,
     g = (1. - Cos[a])/2.,
     h = -(J3 + Exp[I*x])/2., 
     hs = -(J3 + Exp[-I*x])/2.,
     t1 = Table[0., {i, 1, 8*NN}, {j, 1, 8*NN}],
     Do[t1[[i,i]] = 5., {i, 1,8*NN}],
     Do[t1[[i, i]] = d, {i, 1, 8*NN}]}],
     Do[
       {t1[[i, i + 1]] = f,
        t1[[i, i + 3]] = g,
        t1[[i + 1, i + 2]] = g,
        t1[[i + 2, i + 3]] = f},
       {i, 1, 8*NN-3, 4}]}, t1];

M[0., 1., 2., 0.] // MatrixForm

But it shows an error:

With::lvws: Variable Do[t1[[i,i]]=5., {i, 1, 8}] in local variable specification {d = (1. + 2.)/2., f = (1. + Cos[0.])/2., g = (1. -Cos[0.])/2., h = -((2. + Exp[I 0.])/2.), hs = -((2. + Exp[-I 0.])/2.), t1 = Table[0., {i, 1, 8 NN}, {j, 1, 8 NN}], Do[t1[[i, i]] = 5., {i, 1, 8}]} requires a value.

$\endgroup$
2
  • 1
    $\begingroup$ You have now asked several questions on this site, but you still ignore our guidelines on formatting posts. This is annoying. Please read and comply with with our formatting guidelines. We expect people who frequently post on this site to submit properly formatted posts. $\endgroup$ – m_goldberg Aug 9 '20 at 6:49
  • $\begingroup$ Sorry for not following the proper guidelines. From the next time I must keep it in mind. $\endgroup$ – atanu Aug 9 '20 at 7:17
4
$\begingroup$

First you need to understand when to use the delimiter comma ( , ) and when to use the operator CompoundExpression ( ; ). Second you need to understand that While does not make assignments to variables, but makes substituitions based on what look like assignments. So t1 in your code isn't a variable and can't be used as the first argument to Part in a Set expression. Module does create variables, so you can use it for making t1 a local variable.

Putting these remarks into action, I rewrite your code as follows:

NN = 3;
M[x_, J2_, J3_, a_] :=
  With[
    {d = (1. + J3)/2.,
     f = (1. + Cos[a])/2.,
     g = (1. - Cos[a])/2.,
     h = -(J3 + Exp[I*x])/2.,
     hs = -(J3 + Exp[-I*x])/2.,
     n = 8 NN},
    Module[{t1},
      t1 = ConstantArray[0., {n, n}];
      Do[t1[[i, i]] = 5., {i, 1, n}];
      Do[t1[[i, i]] = d, {i, 1, n}];
      Do[
        t1[[i, i + 1]] = f;
        t1[[i, i + 3]] = g;
        t1[[i + 1, i + 2]] = g;
        t1[[i + 2, i + 3]] = f,
        {i, 1, n - 3, 4}];
      t1]]
M[0., 1., 2., 0.] // MatrixForm

matrix

Note: your function doesn't use J2, so why to do have this argument?

$\endgroup$
2
  • $\begingroup$ Thanks. It works fine. Actually I have provided a small part of my full program. On the later part it includes all the arguments. $\endgroup$ – atanu Aug 9 '20 at 7:20
  • 1
    $\begingroup$ @atanu. I'm glad you find my answer useful. Since you do, please consider accepting it. $\endgroup$ – m_goldberg Aug 9 '20 at 15:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.