Mathematica: Module inside For loop

In summary, the conversation discusses a code that runs without error but gives no output. The problem is eventually identified as a simple mistake in the way the loop was assigned. The use of the Module function is deemed unnecessary and the code is suggested to be rewritten using a simpler If-statement.
  • #1
ChristinaJ
Gold Member
35
1
Hi all,

I have the following code which runs without error but gives no output. It's probably simple but I just can't see the problem.

LM={};

M[line_, cur_, lq_, ld_, k_] := For [i = 1, Length[line[[1]]], i++,
Module[{m}, If[line[[1, i]] == "Q",
m = {{Cos[k*Sqrt[Abs[cur]]*lq], Sin[k*Sqrt[Abs[cur]]*lq]/(
k*Sqrt[Abs[cur]])}, {-k*Sqrt[Abs[cur]]*
Sin[k*Sqrt[Abs[cur]]*lq], Cos[k*Sqrt[Abs[cur]]*lq]}}.{{1,
ld}, {0, 1}},
m = {{1, ld}, {0, 1}}];
AppendTo[LM, m]]];

M[line, line[[3]], line[[4]], line[[5]], 1.8]


Where Dimensions[line]={5,15} the first 2 rows of which are strings and the remainder numbers.

For LM I get LM={}

Any help very much appreciated.

Christina
 
Physics news on Phys.org
  • #2
Maybe you need to re-assign the result to the list:
Code:
LM = Append[LM, m]
(If this solves the problem, don't feel bad... you don't want to know how many hours I spent looking for similar mistakes)

Also, if the whole body of your module is a single If-statement, what do you need the module for?
You can just as well write
Code:
For[..., ..., LM = Append[LM, If[...]]]
instead of
Code:
For[..., ..., Module[m = If[...]; LM = Append[LM, m]]
 
  • #3
Thanks CompuChip,

You were right; using Module was unnecessary.

The actual problem turned out to be a foolishly simple mistake. I had assigned the end value for i in the loop in an incorrect way.

Doh!
 

1. How do I use a Module inside a For loop in Mathematica?

To use a Module inside a For loop in Mathematica, you can define the variables and local expressions within the Module, and then use these variables and expressions within the body of the For loop. This allows you to keep the variables and expressions local to the For loop and avoid conflicts with other variables in your code.

2. Can I assign initial values to the variables within the Module in a For loop?

Yes, you can assign initial values to the variables within the Module in a For loop by using the syntax "variable = initial value" within the Module.

3. How does the scope of variables work in a Module inside a For loop?

The variables defined within a Module inside a For loop are only accessible within that For loop. This means that they will not conflict with variables outside of the For loop, and they will not be accessible outside of the For loop.

4. Can I use multiple Modules inside a single For loop?

Yes, you can use multiple Modules inside a single For loop. Each Module will have its own local variables and expressions that will only be accessible within that Module.

5. Are there any restrictions on the types of expressions that can be used within a Module inside a For loop?

No, there are no restrictions on the types of expressions that can be used within a Module inside a For loop. You can use any valid Mathematica expression, including functions, equations, and logical statements.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
12
Views
4K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
6
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
9
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
567
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
948
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
2K
  • Advanced Physics Homework Help
Replies
1
Views
741
Back
Top