Mathematica: Module inside For loop

Click For Summary
SUMMARY

The discussion centers on a Mathematica code issue where the user, Christina, encounters an empty list output from a For loop that utilizes a Module. The problem was identified as an incorrect assignment of the loop's end value, which prevented any output from being generated. Additionally, CompuChip suggested that the use of Module was unnecessary, simplifying the code by directly appending results to the list without it. The final resolution involved correcting the loop's end value assignment.

PREREQUISITES
  • Understanding of Mathematica syntax and functions
  • Familiarity with control structures in programming, specifically For loops
  • Knowledge of list manipulation in Mathematica, including Append and Module
  • Basic mathematical concepts related to trigonometric functions
NEXT STEPS
  • Explore Mathematica's Module function and its appropriate use cases
  • Learn about list operations in Mathematica, focusing on Append and other list-building techniques
  • Investigate debugging techniques in Mathematica to identify common coding errors
  • Study control structures in Mathematica, particularly the For loop and its alternatives
USEFUL FOR

This discussion is beneficial for Mathematica users, programmers seeking to improve their coding skills, and anyone interested in debugging and optimizing their code within the Mathematica environment.

ChristinaJ
Gold Member
Messages
34
Reaction score
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
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]]
 
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!
 

Similar threads

  • · Replies 3 ·
Replies
3
Views
1K
  • · Replies 6 ·
Replies
6
Views
5K
Replies
24
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K
Replies
20
Views
2K
  • · Replies 1 ·
Replies
1
Views
5K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 3 ·
Replies
3
Views
5K
  • · Replies 6 ·
Replies
6
Views
17K
Replies
2
Views
2K