Questions tagged [procedural-programming]
Questions about Mathematica's procedural programming paradigm.
360 questions
0
votes
1
answer
111
views
For-loop not indexing correctly [closed]
I have a question. Why does my result start with J = 2? I write another loop but it start 1 i confused-i need help
...
4
votes
1
answer
166
views
Defining list of new variables at once (in a loop?)
I have a list of solutions Subscript[x, 1][t], i = 1,...,n (n given).
I would like to use these variables to define new variables such as:
...
0
votes
1
answer
168
views
Find the position of all the prime numbers up to a given number
I want Mathematica code the does what the following pseudocode does.
...
2
votes
2
answers
580
views
How can I solve the Ramanujan problem by using Wolfram language? [closed]
The problem like this:
find all integers n, 1 ≤ n ≤ 10, 000 such that n is the sum of two cubes of positive integers in two different ways.
I want to solve the problem as a procedure in mathematica.
1
vote
1
answer
97
views
How to solve this with Nest?
Just get {10} from code below.
jason[x_] := Module[{}, {b = 1}; b = b*Nest[# + 1 &, 1, x]; b]
jason /@ Table[i, {i, 1, 10}]
This gets it right with Do
...
3
votes
1
answer
129
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:
...
0
votes
1
answer
130
views
Creating a loop that gives first value that is false/true
My question is, if there is a command/function that gives me the first value for which an expression becomes false?!
For a simulation of several rounds of the game battleships(ger. Schiffe versenken) ...
1
vote
2
answers
128
views
How to iteratively (or most efficiently) create such list?
Update
A very crude attemp
...
3
votes
2
answers
406
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:
...
1
vote
1
answer
423
views
How is the best way to integrate using loops
I'm using Mathematica, and I want to integrate a function f[wr] in wr using some method that works with a Table/Do/For in the variable wr.
I tried to use something like the Riemann's sum, to evaluate ...
2
votes
2
answers
293
views
How can I plot these straight lines in a picture?
How can I plot these straight lines in a picture?
...
5
votes
7
answers
1k
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.
...
2
votes
3
answers
231
views
Changing iteration variable in Do loop
I want to generate random positive hermitian matrices, I start with such a code
...
0
votes
1
answer
81
views
Why do I not see any results from my For-loop? [closed]
I am sure I am missing something trivial :-( Still any help is welcome.
...
2
votes
1
answer
132
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 ...
1
vote
0
answers
112
views
For- loop which can reuse updated values [closed]
Eout = E^x ;
For[kz = 1, kz < 4, kz++,
Print[Eout1[kz] = Eout];
Print[kz]]
The output is
E^x
1
E^x
2
E^x
3
But what I ...
3
votes
1
answer
476
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 ...
0
votes
0
answers
66
views
What techniques have you employed to avoid loop in Mathematica? [duplicate]
It appears to me that loop in mathematica is very slow (compared with matlab). Since many people still use mathematica to finish their work, there must be some techniques to bypass this problem.
...
1
vote
1
answer
2k
views
For loop for two variable at the same time
Can I use for loop for two variables as example show below at the same time?
for (int x = 0, y = 0; x < 10 && y < 10; x++, y++)
0
votes
0
answers
72
views
Running a complex notebook from a different master notebook?
I am trying to use the Dynamo notebook (https://www.ssc.wisc.edu/~whs/dynamo/dynamo_3S_v1.4.1.nb) in a loop over parameters. Basically, this software takes one matrix as input, solves ODEs and some ...
0
votes
1
answer
282
views
Return last value of While function
I want to make a function that performs a multiplication with Inner i number of times using a ...
1
vote
1
answer
232
views
Variable nesting depth inside table
I hope my title makes sense. The idea is the make a function in which a set of variables, $l_1$, $l_2$, etc. are accessible inside the innermost body of a nested (/folded) function. Each of these ...
0
votes
0
answers
209
views
How can I count the number of days (Monday, Tues, etc) in a year?
I am new to Mathematica and for fun am trying some of the challenges. The one I am working on now is:
...
0
votes
1
answer
475
views
0
votes
1
answer
928
views
Parallelizing nested for loops
I'm currently computing nested for loops.
I count up a few counting variables and compute a value for each set of counting variables, then that value is added to an array. It is a way of adding up ...
2
votes
3
answers
248
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 ...
1
vote
1
answer
267
views
How do I create multiple plots from an Excel spreadsheet?
I have imported an Excel spreadsheet with dimensions {row, col}= {92, 28} into Mathematica, I want to plot 27 datasets from the spreadsheet: column 1 with column 2, column 1 with column 3, ..., column ...
0
votes
1
answer
285
views
Mathematica crash every time when I run this loop [closed]
I have written a simulation for a percolation test. Then, I am using another notebook to control this simulation to generate 1000 results with a different random seed for each one of them.
The ...
2
votes
1
answer
107
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. ...
3
votes
2
answers
1k
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 ...
0
votes
2
answers
146
views
Running a loop to check for multiple congruences
I have the code for a loop to run. Right now it can check if a number is a square and at the same time check if it is congruent 0 mod 47. My questions is, how can I alter the code to see if it is ...
2
votes
1
answer
185
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 ...
0
votes
2
answers
138
views
List defined in Do not remembered outside Do
Apologies if this is a trivial question. It comes from an overall problem I am having with Mathematica. Specifically, within a Do I generated the elements of a (...
0
votes
1
answer
474
views
Using elements of list to solve a differential equation
I have created a list as shown in picture.
the digits inside f are values of a and q respectively.
I want to solve the Mathieu equation.....
1
vote
3
answers
912
views
How do I repeat the number of times a nested for loop does an iteration?
I am trying to write a nested for loop that prints the pairs 00 through 55, with each digit running from 0 to 5.
In other words, I want the output to appear as so: 00, 01, 02, 03, 04, 05, 10, 11, ......
0
votes
1
answer
115
views
How to make a table for loops?
I have a code given by
...
1
vote
1
answer
383
views
Why are there local variable conflicts in my recursive function definition?
Sorry to post a long piece of code. There are two places in here where I Print[] 'path2' when the count == 984, because for some reason, that list is getting ...
2
votes
1
answer
107
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}$). ...
1
vote
1
answer
114
views
Nested Do loops with externally defined iterators [closed]
I want to be able to define a set of iterators for nested Do loops, such as
...
2
votes
2
answers
130
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:
...
4
votes
1
answer
911
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.
0
votes
1
answer
112
views
Problem faced using user-defined mod function in loop
I want to make a program which tell whether a number perfect or not.So i tried like that:
...
0
votes
1
answer
56
views
Looping Constructs to create a set of varibale matrices
I am trying to create an $ n \times n $ matrix, which has cosine terms. I would like to select the values of the arguments of cosine terms from a random set of values that I have generated. My code ...
2
votes
3
answers
2k
views
Solve a system by Cramer's rule using loop
Consider the linear system AX=B,where A={{3, 0, 2}, {-3, 2, 2}, {2, -3, 3}},B={{3}, {-1}, {4}} and x=...
0
votes
1
answer
199
views
How to use a test function correctly to terminate a While loop?
I define a test function in order to stop my While loop:
...
0
votes
1
answer
174
views
Why can't I iterate?
I encounter a problem when computing the following equations:
...
2
votes
4
answers
323
views
How to generate repetitive graphs?
I need to create several graph for different values of a parameter $h$.
The code is the following
...
3
votes
3
answers
284
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 ...
1
vote
1
answer
280
views
While loop (or alternative) to generate list of random submatrices satisfying condition
I'm trying to generate a list of random submatrices with matrix norm 0.1, however I'm getting stuck; currently my code looks like this, where I run over a while loop.
...
0
votes
1
answer
84
views
How could I iterate the following equations?
It's my first time to use mathematica, and I hope to iterate the following equations.
Ci0=-Ci-1,0 ((i-1+q)p a-p q c)/((i+q) p-p q)
C0j=-C0,j-1 ((q p b-(j-1+p) q d)/((q p-(j+p) q)
Cij=-(Ci-1,j ((i-...