Group Abstract Group Abstract

Message Boards Message Boards

0
|
2.7K Views
|
7 Replies
|
2 Total Likes
View groups...
Share
Share this post:

Do loop gives an error named 'Set::write'

Posted 2 years ago

Hello I am trying to evaluate the bottom expression inside the Do loop

testMatrixSummed=Table[Total[testMatrix[[i,j]],2],{i,1,2},{j,1,2}]
testValues=Eigenvalues[testMatrixSummed];
N[testValues];
(*new eigenvector*)
testWaveFunctions=Eigenvectors[testMatrixSummed];
N[testWaveFunctions]
(*new eigenvalues*)
Min[chemicalPotentialValues] (*choosing the min eigenvalue*)
waveFunctions[[ Min[Position[chemicalPotentialValues,Min[chemicalPotentialValues]]] ]](*setting the eigenvector corresponding to the min eigenvalue to the list*)
testN=N[ testWaveFunctions[[ Min[Position[testValues,Min[testValues]]] ]] ]

When I put this expression inside the 'Do' loop it says 'Set::write' Tag Times in {0.6856, -0.72} is Protected. When I just copy and paste the above expression multiple times and run, It computes. But when I put it inside the 'Do' loop, it gives me an error.

If you want to see what I am doing here is the full code

(*Test*)
testN=List[1,2];
test[x_,y_,z_,t_]:=testN[[y]]*testN[[z]]*(x+y+z+t)
testMatrix=Table[Table[test[x,y,z,t],{y,{1,2}},{z,{1,2}}],{x,{1,2}},{t,{1,2}}];
testMatrix=Table[Table[test[x,y,z,t],{y,{1,2}},{z,{1,2}}],{x,{1,2}},{t,{1,2}}];
testMatrixSummed=Table[Total[testMatrix[[i,j]],2],{i,1,2},{j,1,2}]
testValues=Eigenvalues[testMatrixSummed];
testWaveFunctions=Eigenvectors[testMatrixSummed];
testN=N[ testWaveFunctions[[ Min[Position[testValues,Min[testValues]]] ]] ]

Do[
testMatrix=Table[Table[test[x,y,z,t],{y,{1,2}},{z,{1,2}}],{x,{1,2}},{t,{1,2}}];
testMatrixSummed=Table[Total[testMatrix[[i,j]],2],{i,1,2},{j,1,2}]
testValues=Eigenvalues[testMatrixSummed];
N[testValues]

testWaveFunctions=Eigenvectors[testMatrixSummed];
N[testWaveFunctions]

testN=N[ testWaveFunctions[[ Min[Position[testValues,Min[testValues]]] ]] ]
,2]

Could you help me understand what I am doing wrong inside the loop?

Thank you.

7 Replies
Posted 1 year ago

The issue in your code is related to variable scoping. Inside the Do loop, you are using the variable testWaveFunctions which is also defined outside the loop. This can lead to conflicts and unintended behavior.

To fix this issue, you can use a different variable inside the loop. Here's an updated version of your code:

(* Test *)
testN = List[1, 2];
test[x_, y_, z_, t_] := testN[[y]]*testN[[z]]*(x + y + z + t)

Do[
  Module[{testMatrix, testMatrixSummed, testValues, localWaveFunctions, localTestN},
    testMatrix = Table[Table[test[x, y, z, t], {y, {1, 2}}, {z, {1, 2}}], {x, {1, 2}}, {t, {1, 2}}];
    testMatrixSummed = Table[Total[testMatrix[[i, j]], 2], {i, 1, 2}, {j, 1, 2}];
    testValues = Eigenvalues[testMatrixSummed];

    localWaveFunctions = Eigenvectors[testMatrixSummed];
    localTestN = N[localWaveFunctions[[Min[Position[testValues, Min[testValues]]]]]];

    (* Use localWaveFunctions and localTestN as needed *)
    Print[localWaveFunctions];
    Print[localTestN];
  ],
  2
]

In this modification, I introduced localWaveFunctions and localTestN inside the Module to ensure that they are local to each iteration of the loop. This avoids conflicts with the variables defined outside the loop. Adjust the usage of these local variables as needed within the loop.

POSTED BY: jilwaas biss

Thanks, Bill

So, my next question is when I write a previously mentioned whole code in a single cell, the looping part runs and has no problem. However, when I declare functions in a separate cell, where it is placed before the looping part, the looping part that is in the separate cell throws an error named 'Part::pkspec1', 'The expression "infinity sign" cannot be used as a part specification'. So I want to know the reason.

Also if it is possible could you recommend me something to read about Mathematica's kernel, how declared variables are used in different cells, and basically the overall structure and working scheme under the front end of the Mathematica?

Thank you.

Posted 2 years ago
POSTED BY: Bill Nelson

Ok, I think I have something wrong in my code that I need to debug very carefully. Thank you.

Posted 2 years ago

From your first question, I am guessing it would be fine to add another question onto the end of this. If the question is on a very different problem then perhaps a new question might be better.

POSTED BY: Bill Nelson
Posted 2 years ago

Try

(*Test*)
testN=List[1,2];
test[x_,y_,z_,t_]:=testN[[y]]*testN[[z]]*(x+y+z+t)
testMatrix=Table[Table[test[x,y,z,t],{y,{1,2}},{z,{1,2}}],{x,{1,2}},{t,{1,2}}];
testMatrix=Table[Table[test[x,y,z,t],{y,{1,2}},{z,{1,2}}],{x,{1,2}},{t,{1,2}}];
testMatrixSummed=Table[Total[testMatrix[[i,j]],2],{i,1,2},{j,1,2}];
Print[testMatrixSummed];
testValues=Eigenvalues[testMatrixSummed];
testWaveFunctions=Eigenvectors[testMatrixSummed];
testN=N[ testWaveFunctions[[ Min[Position[testValues,Min[testValues]]] ]] ];
Print[testN];
Do[
testMatrix=Table[Table[test[x,y,z,t],{y,{1,2}},{z,{1,2}}],{x,{1,2}},{t,{1,2}}];
testMatrixSummed=Table[Total[testMatrix[[i,j]],2],{i,1,2},{j,1,2}];
Print[testMatrixSummed];
testValues=Eigenvalues[testMatrixSummed];
Print[N[testValues]];
testWaveFunctions=Eigenvectors[testMatrixSummed];
Print[N[testWaveFunctions]];
testN=N[ testWaveFunctions[[ Min[Position[testValues,Min[testValues]]] ]] ];
Print[testN];
,2]
POSTED BY: Bill Nelson

Thank you Bill. It works.

If it is possible Can I ask one more question down here? Or should I close this one? And open another discussion?

Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard