Mathematica Updating a Vector in a For Loop: Troubleshooting Tips

  • Thread starter Thread starter member 428835
  • Start date Start date
  • Tags Tags
    Loop Vector
AI Thread Summary
The discussion centers on updating a vector in Mathematica using a For loop. The original code attempts to concatenate a list, but the user encounters issues with the output not reflecting the expected values. Participants clarify that the syntax used results in a Null output due to the Print function modifying the return value. They suggest that instead of using Print within the concatenation, the user should separate the concatenation and the Print function to correctly update the vector. Ultimately, the user confirms that they resolved the issue and successfully implemented the solution.
member 428835
I am trying to update a vector. I have tried this but it's not working. Any ideas?

\[Lambda] = {1, 2}
For[\[CapitalLambda] = 0.1, \[CapitalLambda] <=
0.2, \[CapitalLambda] += 0.1, \[Lambda] =
Catenate[{\[Lambda], {\[CapitalLambda]}}] // Print]
 
Physics news on Phys.org
Dumb question, but what language is this? I don't recognize the syntax.

Edit: Ah, just noticed the Mathematica tag. Sorry. Maybe someone more knowledgeable with Mathematica can figure it out.

What are you expecting to be the output? It looks like you intended Lambda to be concatenated with the values taken by CapitalLambda, so at the end it should look like {1, 2, 0.1, 0.2}?
 
joshmccraney said:
I have tried this but it's not working.
What do you mean by “it’s not working”? What happens?
 
Have you tried to simplify it to verify the loop itself works?

Is there a stepping debugger in mathematica that you can use to diagnose the issue?
 
joshmccraney said:
\[Lambda] = {1, 2}
For[\[CapitalLambda] = 0.1, \[CapitalLambda] <=
0.2, \[CapitalLambda] += 0.1, \[Lambda] =
Catenate[{\[Lambda], {\[CapitalLambda]}}] // Print]
The // modifies what it operates on. What you get back from
Code:
\[Lambda] = Catenate[{\[Lambda], {\[CapitalLambda]}}] // Print
is not λ but a Null,

Edit: the above is not well formulated. What I mean is that what is on the right-hand-side returns Null, so it is equivalent to λ = NullIn[16]:= FullForm[Sqrt[2]]
Out[16]//FullForm=
Power[2, Rational[1, 2]]

In[17]:= FullForm[Sqrt[2] // Print]
##\sqrt{2}##

Out[17]//FullForm=
NullYou have to instead use in the For loop
Code:
\[Lambda] = Catenate[{\[Lambda], {\[CapitalLambda]}}]; Print[\[Lambda]]
 
Last edited:
  • Like
Likes mfb and Dale
Thanks all! I would have replied a while ago but for some reason I did not receive email notifications. I have it working!
 

Similar threads

Replies
4
Views
5K
Replies
1
Views
2K
Replies
4
Views
3K
Replies
2
Views
2K
Replies
12
Views
5K
Replies
8
Views
2K
Replies
3
Views
2K
Replies
4
Views
1K
Back
Top