Questions tagged [procedural-programming]

Questions about Mathematica's procedural programming paradigm.

Filter by
Sorted by
Tagged with
55
votes
5answers
6k views

Are there any cases when For[] loops are reasonable?

To my mind the only reason for the existence of For[] loops in Mathematica is to allow new users with some experience in procedural programming languages to write ...
13
votes
3answers
7k views

“do … while” loop equivalent in Mathematica [closed]

Have I missed something or is there no built-in which mimics the behaviour of do ... while loop? I am looking for a construct that evaluates procedure once and ...
13
votes
3answers
574 views

Mimic a procedural, recursive clustering algorithm for site percolation using functional programming

Sorry in advance for my logorrhea: I just want to make sure all of the information is here. Context and Question I am investigating site percolation on a square lattice. I have a working, depth-...
11
votes
2answers
3k views

How to pass variables by reference and by value [duplicate]

I haven't seen this question specifically addressed before in this site, although some hints and traces have been given, for instance, here: Pass by reference for an option argument Does pass-by-...
9
votes
2answers
160 views

Problem with Excel operation in NETLink way

I'm using the NETLink package to build a Excel file and trying to fill it with some data as this: ...
8
votes
3answers
538 views

Functional equivalent of a For loop referring to consecutive elements in a list

I have implemented this function which smooths the values in a list by, if it encounters a zero, replacing this entry by the average of its two neighbours (apart from the first and last values of the ...
8
votes
3answers
389 views

How do I achive the function of Continue using Catch and Throw?

Procedural programming is considered a bad practice in writing Mathematica code. But for education's sake, is there any way I can do Continue using Catch and Throw? According to WRI's documentation, ...
8
votes
1answer
217 views

How would one return from a place of a pure function's body using Return?

From previous answers, replies and discussions it is known, while not formally documented (even in the most recent Wolfram Language Documentation Center), that Return can be used in a two-argument ...
5
votes
7answers
280 views

How to use AppendTo in a While loop? [closed]

I am trying to code a while loop that uses AppendTo to append the loop variable to an empty list. ...
5
votes
1answer
461 views

An efficient way to translate a triple for-loop from Matlab to Mathematica

This is my first post on StackExchange, so if I violate any etiquettes, I apologize in advance. I am a senior engineering student who has been using Mathematica for a year now. For one of my classes, ...
5
votes
2answers
249 views

Using Throw within ParallelDo

I've noticed that I can't Throw from within a ParallelDo in the same way that I can from inside Do. For instance, the following will work as anticipated with Do but not with ParallelDo: ...
5
votes
1answer
5k views

Creating a multiline function with localized variables

I would like to create a function with several expressions in the code body. For example, I would like to write something like this, but which will actually work. ...
5
votes
1answer
225 views

Newton's Method for Robotic Arm with Constrained Motion

My question is related to the system in Using a Grobner basis to calculate common roots of a system of polynomial equations, but I am trying to solve with Newton root-finding methods the inverse ...
4
votes
5answers
296 views

How to build these functions without nested For loops?

I am trying to solve the 8-puzzle using two heuristics, which are: Pieces out of place and Distance Manhattan. After consulting several websites for example see and some books, I'm with some doubts, ...
4
votes
3answers
270 views

Writing my own code to find the power set of a set [closed]

(The Goal is to create a program that given an input set, yields an output showing the Power Set of the entered set without using the built-in mathematica function" "When I enter this code into ...
4
votes
2answers
250 views

Populating an array in do loop, allocating memory or not

I am doing a calculation where I have a do loop can be as long as 1 million. In each loop, I want to store a value into an array to plot afterward. In MATLAB normally I was preallocating the memory ...
4
votes
2answers
5k views

how to remove Null's from output? [duplicate]

I'm a math enthusiast and I'm looking for examples of rare divisibilities, let's look at the following one: ...
4
votes
1answer
282 views

For loop to perform a shooting search in NDSolve

First comes the disclosure: I am very new to Mathematica and not so much a numerical guy in general. I am having a hard time using the for loop routine in Mathematica the correct way as well as ...
3
votes
3answers
187 views

Improving performance of algorithm to compute Code puzzles

I hope to explore Mathematica by using it for Advent of Code puzzles this year, so spoiler alert for anyone interested in solving https://adventofcode.com/2018/day/1 for themselves. Part 2 of ...
3
votes
2answers
82 views

Extract outputs from a WHILE loop in a table format

How can I convert my multiple outputs of a while loop in a table form and save it for later use. Here is a sample code: ...
3
votes
2answers
106 views

Overloading a function to pass in an expression rather that the name of a function

This is an example of procedural programming borrowed from Paul Wellin's book. It's a simple implementation of the Newton-Rhapson method. I overloaded the findRoot ...
3
votes
1answer
451 views

Use of NDSolve in loops

Im new to mathematica and have some problems to use NDSolve in a loop (at least i suspect this is the reason for my problems). I hope anybody can help me! Thanks a lot in advance! I created the ...
3
votes
2answers
109 views

Programming with Mathematica Syntax - Dunford decomposition

I am always amazed while I am reading some solutions of problems in mathematical problems of this site with Mathematica. For instance, look at the problem 48. Whereas I am writing 100 lines of code ...
3
votes
1answer
64 views

Looping routine to evaluate a recursively defined function

I'd like to do a calculation which is basically a looping routine to evaluate a recursively defined function. For example: ...
3
votes
1answer
147 views

Is there any way that convert entire mathematica code to java or python?

I am novice to Mathematica. I want to convert Mathematica code from my textbook to Java or Python to trace coding.
3
votes
1answer
366 views

Encountering 'Error: Set::setraw: Cannot assign to raw object' during procedural function definition

Sorry for the rather vague title, but I don't know what is wrong with my code. Here is a Ruby version of what I'm trying to do: ...
2
votes
4answers
163 views

How to generate repetitive graphs?

I need to create several graph for different values of a parameter $h$. The code is the following ...
2
votes
3answers
581 views

Is for loop suitable for functions?

I have a question about for loop. In this example i made For loop without function, like this ...
2
votes
4answers
243 views

Table and IF statement

I want to make a table to get a nbyn matrix. However, the diagonal are divided by zero. For example: n = 5; Table[1/(Sin[i] - Sin[j]), {i, 1., n}, {j, 1, n}] I ...
2
votes
3answers
129 views

Evaluating number of iteration with a certain map with While

Beeing used to programming in C-like languages I am struggling with iterations and loops with mathematica. I am trying not to use the For command, as already lots of people recommended. I am trying ...
2
votes
4answers
136 views

How do I use the values generated in a for-loop in my first iteration to my next iteration?

This one is somewhat hard to explain, but I'll try my best to. I'm trying to generate a list of numbers (containing only pi/10, 0, -pi/10). Numbers are randomly selected from these 3, where the ...
2
votes
2answers
424 views

For/ Do loop on a lists of two variables

I am trying to get an output of the following kind Out = Do[Evaluate[f[x,y]],{x,table1},{y,table2}] Here Out is a N-times-1 dimensional array/list and N is the ...
2
votes
3answers
70 views

Changing iteration variable in Do loop

I want to generate random positive hermitian matrices, I start with such a code ...
2
votes
2answers
137 views

Better to use loop or function when populated list?

In the past I have pretty much stuck to expression building when using Mathematica, but recently I have been writing a procedure to populate an array, which I guess are called "lists" in Mathematica. ...
2
votes
1answer
399 views

Problem with Label in a Do loop, “nolabel”

Can someone tell me why this code doesn't work and how to fix it ? Thanks. ...
2
votes
1answer
63 views

Populating function inputs from lists [closed]

I have several lists containing variables, e.g. list1={x1,x2,x3}, list2={y1,y2,y3}, etc. I would like to define a function in those variables, i.e. ...
2
votes
1answer
48 views

Looping from a Set of Undetermined Size [closed]

For a problem I am working on, I need to create sets all the prime factors of a large set of integers (at least on the order of 1$-$1,000,000 but more on the order of all integers 1 to $10^{12}$). ...
2
votes
2answers
75 views

What is wrong with my procedural function to find all prime $\leq x$? [closed]

I want to create a simple function that prints all prime numbers from 1 to x. I have the following code, which doesn't cause any errors from the kernel but it doesn't return anything: ...
2
votes
1answer
428 views

Iterating FindRoot to solve a differential equation

I am new to Mathematica style programming, better at traditional programming. I would like to know if I can create a table of results using FindRoot, where the ...
2
votes
2answers
74 views

Disappearing variable in parallel computations

I have a problem with parallel computations. When I try to evaluate formula like this: Do[m[i]=2,{i,1,10}] and then I try to call for instance ...
2
votes
1answer
71 views

Numerical Fredholm integration using loop

I have a code line having two Do loops, one within other. In certain situation arising within inner loop, I want the outer Do ...
2
votes
1answer
88 views

Run a do loop from a high iterator value to lower [closed]

I wanna have a do loop that runs from the a high number to a smaller one. That is, if we have Do[Print[n],{n,1,4}] I would get 1,2,3,4 in the output. I rather ...
2
votes
2answers
310 views

Solving Bethe ansatz equations

I'm very new to Mathematica. I want to solve the Bethe ansatz equations for the so-called "Hofstadter-problem". These equations take the form: $$z^2_{l}=q^{Q} \prod^{Q-1}_{\substack{m=1\\m \neq l}}\...
2
votes
1answer
34 views

Loop with indexed function not defined for some index values [duplicate]

If anyone cares, this is supposed to be a very simple implementation of a Metropolis algorithm for a uniform distribution. I want to plot the variance of the sampled values as a function of an ...
2
votes
1answer
67 views

Code for finding multiple roots only finding two

I've an issue with my code. I am basically trying to get the n-th root of a function. My code finds two roots correctly, but for others, which I think should be distinct, I get the two roots repeated ...
2
votes
2answers
152 views

Looping over two variables [closed]

Suppose I have $t=4, 2, 5$ and $v=5, 7, 4, 8, 0, 5$ and $equ=A v/t$. I would like to calculate $equ$ at a single value of $t$ by using all $v$ values and then move to next value of $t$. How can I ...
2
votes
1answer
34 views

how to run a specific cell in a notebook from another notebook

I have a long Mathematica code in a notebook, denoted by NB1. Since it is a very long code, and I need only some of the outputs from NB1 to be used in another ...
2
votes
1answer
77 views

Confused why my program won't define this

I legitimately think the problem is that pwgenerator isn't getting defined, but im' not sure why. Any ideas? On mine, when i run this to define it, pwgenerator stays blue for some reason, and it didn'...
2
votes
1answer
324 views

Fourier transform along only one dimension of 2D array

I'm a relative newbie to Mathematica Before you flag me as duplicate, note that I have read and studied "Fourier transform over a custom dimension" as best I can. I cannot understand the answer, it ...
2
votes
2answers
813 views

For loop error: Tag Times is Protected [duplicate]

i want to use For loop into my analytical physics theory but my For loop is not working.? so please help me .here is my simple (for example code) ...