Message Boards Message Boards

0
|
4974 Views
|
3 Replies
|
7 Total Likes
View groups...
Share
Share this post:

How to use a loop of a big amount of code lines

Posted 5 years ago

I have done a whole program in Wolfram Mathematica and now I have to execute it changing one variable again and again. The tool I'd like to use is Do, but I don't know how to use it.

POSTED BY: mikel gomez ruiz
3 Replies
Posted 5 years ago

In my opinion, this is a weakness of the notebook interface, which I forgive because of its many virtues. I use two methods for processing sets of data.

The first is easy for simple applications. In this case I develop the calculation in multiple cells in the usual way. I can follow the calculation by seeing the results one cell at a time. When I am happy with the result, I delete all the output, separate all lines with semicolons, and merge the result into a single cell. I use that single cell as the bases for a function, generally defined in a Module to localize variables. This can the be used with Table or Map to process a set of inputs. I maintain both the multiple-cell notebook and the function.

When the calculations are more complicated, I use a Master-Slave notebook method. This takes advantage of the fact that all notebooks in a single session share the kernel, and therefor have variable definitions in common. The Master contains the sets of data. It explicitly opens the slave which performs the calculations. The master can set values to global variables used by the slave and then call NotebookEvaluate on the slave to perform the calculations. The Slave sets values to one or more global variables as its output. When NotebookEvaluate finishes, the Master has the results from the Slave to do with as needed. The Slave is essentially a subroutine for the Master.

I find the second method much better for doing complicated work. I attach a made-up example in notebooks.

Kind regards, David

Attachments:
POSTED BY: David Keith

This seems like the sort of thing one might find in Help > Wolfram Documentation.

POSTED BY: Daniel Lichtblau
Posted 5 years ago

If you need to further process the output from your 'program' then Table is a better option than Do. Either way, you will have to express your program as a function that takes the variable you need to iterate over as an argument.

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

Group Abstract Group Abstract