Debugging Weird Mathematica Behavior

Click For Summary

Discussion Overview

The discussion revolves around issues encountered while using Mathematica, specifically related to inconsistent behavior with subscripts and the use of Goto statements in code. Participants explore potential reasons for these issues and seek clarification on programming practices within Mathematica.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Conceptual clarification

Main Points Raised

  • One participant reports inconsistent behavior in Mathematica when using subscripts, particularly with a variable labeled H3, and expresses confusion over why the program sometimes fails to recognize the value assigned to H3.
  • Another participant suggests avoiding the use of subscripts unless the user knows how to unprotect the head subscript and convert it to a symbol type.
  • A participant raises a question about difficulties with Goto loops, specifically that Goto cannot find a label unless all equations are on the same line, and seeks a solution for using Goto with multiple expressions in between.
  • One response asserts that there is no way to make Goto function as desired and recommends renaming subprocedures and using function calls instead of Goto, citing historical context about the deprecation of Goto in programming languages.
  • A participant expresses confusion regarding terminology such as cell, subprocedure, and function calls, requesting an example for clarification.
  • Another participant notes that the successful Goto code was in the same active cell and suggests that sharing specific code would help address the issues being faced.

Areas of Agreement / Disagreement

There is no consensus on the best approach to resolve the issues with subscripts and Goto statements, as participants present differing opinions and suggestions without reaching a definitive solution.

Contextual Notes

Participants express varying levels of familiarity with Mathematica's programming constructs, which may affect their understanding of the issues discussed. The discussion includes unresolved technical terms and concepts that some participants find unclear.

natski
Messages
262
Reaction score
2
Hi all,

I have attached a notebook which I am doing a calculation in. For some reason, Mathematica is acting inconsistently. When I run the calculation for the first time, it usually doesn't work. By that I mean I get the output that you can see in the notebook attached.

You can that the problem is coming from the subscript 3 in the term H3. I can't understand why it doesn't just read the value of H3 I input early on!?

I could just ditch the subscript but I really don't want to because I want to eventually change the subscript 3 for a general integer i and rerun the program for many different i values.

I should also add that sometimes the program runs with no problems despite having changed nothing. Once it runs once OK, then everytime thereafter is OK.

Please can someone explain what on Earth is going on!
 
Last edited:
Physics news on Phys.org
Um sorry but I solved this problem!
 
Avoid the use of subscripts, unless you know how to unprotect the head subscript and change it to type symbol.
 
Thanks for the tip. As a further question, I am trying to use a Goto loop and finding a slight difficulty.

I put a label at the beginning of my notebook and then the Goto at the end and it doesn't work because Goto can't find the Label. Even for simple expressions this happens unless all the equations are on the same line. Is there not a way to use Goto and Label when there are many, many (and so a single line calculation is inconvenient) expressions inbetween them?

Thanks
 
No there is no way to make Goto do what you want.

But if you want to Goto a cell, then it is probably because that cell contains a subprocedure. So why not rename the subprocedure, evaluate the cell which defines it, and replace the Goto with function calls?

In the 1970s it was decided that the use of Goto should be deprecated in all languages, because it leads to poorly readable code. Imagine looking all over the place for someone elses Goto Label. Mathematica compromises by allowing the Goto, but restricting it to be local in the sense you describe.
 
Hmmm I don't really understand what you mean, the terms cell, subprocedure and function calls are not really clear to me. Can you please show me an example?
 
You said that, when working, all your Goto code was on the same line. The imprortant thing is that all your code was in the same active active cell. See the example.

I tried to guess what issues were having, but if you want to talk specifics you should show me your code.
 

Attachments

Similar threads

  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 18 ·
Replies
18
Views
5K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 6 ·
Replies
6
Views
5K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 6 ·
Replies
6
Views
4K
  • · Replies 1 ·
Replies
1
Views
3K