Questions tagged [procedural-programming]

Questions about Mathematica's procedural programming paradigm.

Filter by
Sorted by
Tagged with
2 votes
6 answers
439 views

All solutions that satisfy $ x_{1}^{5}+x_{2}^{5}+x_{3}^{5}+x_{4}^{5}-x_{5}^{5}=0 $

I want to find a combination that satisfies all the solutions of the following formula. $$ x_{1}^{5}+x_{2}^{5}+x_{3}^{5}+x_{4}^{5}-x_{5}^{5}=0 $$ $x_{1}$, $x_{2}$, $x_{3}$, $x_{4}$, and $x_{5}$ are ...
Vancheers's user avatar
  • 525
0 votes
1 answer
107 views

Performance improvements using Table and Compile

I'm converting a code from MatLab to Mathematica. It takes MatLab less than a second to compute it while its more than ten minutes in Mathematica. I was hoping to improve the performance. Here's a ...
Dotman's user avatar
  • 376
1 vote
0 answers
39 views

Parallelization with procedural functions, e.g. `AddTo`, `PreIncrement`, and so on

I have a problem that procedural functions don't work with parallelization, e.g. AddTo and PreIncrement. At first, I thought it ...
Taiki Bessho's user avatar
0 votes
1 answer
47 views

How make a function NestUntilList that accepts multiple arguments?

Recently the looping construct Until was added to Mathematica in 13.1, based on the ResourceFunction Until created by Jon Mcloone. I want to make a generalization named NestUntilList. NestUntilList is ...
Peter Burbery's user avatar
0 votes
0 answers
28 views

Stack Solve solutions in a loop [duplicate]

I want to solve the following equation in a loop: ...
Syn1110's user avatar
  • 51
0 votes
2 answers
119 views

Real-time function graph display

I would like to see how the graph of the function en[B, i, j, k, p] changes depending on i, j...
Mam Mam's user avatar
  • 1,731
0 votes
0 answers
69 views

Do loop Implementation

I want to generalise the following algorithm to a loop overall possible subsets of the original set. the algorithm has the following form: ver={X41,X42,X43,X44} and ...
Faktor 2's user avatar
2 votes
1 answer
61 views

comparing lists of unequal lenghts

Suppose I have three (nested) lists A , B and Cmat where ...
geom's user avatar
  • 649
4 votes
3 answers
402 views

A loop in which three indices are changed

How could this piece of code be written in the Table, or as simply as possible? ...
Mam Mam's user avatar
  • 1,731
0 votes
1 answer
91 views

Do Loop Complication [closed]

...
supragyan priyadarshinee's user avatar
0 votes
1 answer
89 views

Can I use Table or something else instead of While in this case?

I have a very long code that take an extremely large amount of time. I have been using the While loop, but with each iteration, it gets slower and slower, so I know there has to be a better way of ...
juv95's user avatar
  • 175
0 votes
0 answers
50 views

Solving a set of an arbitrary number of coupled differential equations

I have to solve a system of b coupled ordinary differential equations of the following type: ...
Atelap's user avatar
  • 1
4 votes
2 answers
117 views

How to discard the repeating elements of a list [closed]

Lets say there is a random list of expressions : a={Sin[x], Sin[3 x], Sin[4 x], Sin[x], Sin[4 x], Sin[x], Sin [5 x], Sin[3 x], Sin[5 x]} Plot[{a}, {x, 0, 2 Pi}] ...
a019's user avatar
  • 811
2 votes
2 answers
128 views

The way of converting a string to a variable in a loop?

I have 60 txt files which I want to import them into Mathematica using a Do loop. Each file contains some numbers (more precisely, a list of numbers). I want to put ...
Wisdom's user avatar
  • 1,248
1 vote
1 answer
64 views

Jacobian General::ivar issue

I have a differential equation labelled by eq0 and I want to use finite-difference method to solve it. First, I have to set up the system of equations ...
mathemania's user avatar
1 vote
1 answer
108 views

My ParallelDo does not work

I have to do a lot of calculations that take lot of time, and using a Do loop is simply too long. It is the first time I am using ...
Eurabio's user avatar
  • 33
2 votes
0 answers
112 views

The fastest and most scalable way to write a nested loop

The problem is to find the weights {w1,w2,w3,w4} that satisfy the condition. Which is the fastest and most scalable way (e.g. for 10000 weights) to write this ...
John Matis's user avatar
1 vote
2 answers
124 views

Trying to apply numerical method with Do

I am trying the following scheme to learn to implement an iteration using Do loop. I tried to implement the same method using for loop but with same errors. I, therefore, turned to the Do loop to ...
Skj's user avatar
  • 19
0 votes
1 answer
51 views

Sorting a list by increasing order algorythm [closed]

Im new to loops on mathematica,was wondering how could I create a function to loop through a list which finds the index of the smallest value of that list, makes a sublist from index 1 to that ...
SkySystem's user avatar
3 votes
2 answers
84 views

How to NIntegrate multiple ranges of data at the same time with an output that can be plotted?

I am trying to use the NIntegrate function to find the difference in the area beneath two 2 dimensional splines (how much area of the river bed was lost or gained from one year to the next) but, I ...
Andrew Bowsher's user avatar
2 votes
2 answers
460 views

How can I make 10 variables from a for loop?

I used a for loop to make 4 variables: For[i = 0, i < 4, i++,bi=0] But when I write b0 or ...
Ahmed's user avatar
  • 359
-1 votes
1 answer
74 views

Right writing of While loop

Help me pls write While loop correctly. I have next code: ...
Mam Mam's user avatar
  • 1,731
0 votes
1 answer
89 views

Length of a For loop [closed]

How to find the length of a For loop? Here is a simple example: For[i = 0, i < 4, i++, Print[i]] 0 1 2 3 How to find that there are in total 4 outputs using ...
SciJewel's user avatar
  • 319
5 votes
2 answers
278 views

Perform same operation along dimension of an array

If I want to add the $2\times 2$ matrix b along the first dimension of a that has $n\times 2\times 2$ dimensions I can do this ...
granular bastard's user avatar
0 votes
0 answers
44 views

How to "freeze" a parallel program

I wrote a program, parallelized it (using ParallelDo[], SetSharedVariable[] etc.), and enjoyed the wonderful speedup. ...
user7427029's user avatar
2 votes
0 answers
47 views

How to make my own external language cell evaluator

We can type > and select python to insert python external language cell into notebook. Then we can write python code inside it and evaluate it by pressing ...
azerbajdzan's user avatar
  • 7,094
4 votes
1 answer
171 views

Why won't my random walk work in a Mathematica Do loop? [closed]

This produces a nice random walk plot: ...
cybervigilante's user avatar
3 votes
1 answer
42 views

Keep Reap / Sow results after breaking out of a loop [closed]

I am doing a long calculation where I create a list using Reap and Sow. However, sometimes this calculation gets too wild and I ...
Philipp's user avatar
  • 51
0 votes
0 answers
119 views

How to Loop initial conditions to solve a coupled differential equation using nested for?

I need to solve the following pair of coupled differential equations and find the plots for the ground state radial wave function, the potential and the energy eigenvalue for the ground state. $$V''(r)...
Varun Samj's user avatar
2 votes
1 answer
133 views

How to create a loop in Mathematica?

Yup, im a newbie, you guessed it. But I have been having fun solving Project Euler problems with Mathematica. I am on problem #8: The four adjacent digits in the 1000-digit number that have the ...
SugarFoot's user avatar
3 votes
3 answers
166 views

Quit the loop if encounter error message

First define length-component error (personal term) : This is an error when we want to get n-th component of a list if n is bigger then the length of the list. For example, ...
imida k's user avatar
  • 4,037
2 votes
2 answers
215 views

While loop not working problem [closed]

I am new to Mathematica ,and I am trying to implement this while loop which adds all odd Fibonacci numbers less than or equal to 2 million but it keeps outputting 0 for sum. Any solutions for that? ...
Brotos's user avatar
  • 31
2 votes
1 answer
285 views

Why is this Do-Loop taking so much time?

I have this simple Do Loop that, for some reason, takes a really long time for $h<\frac{1}{15}$. Why is this so? Is there a more efficient way in programming such a recursion? ...
korni1990's user avatar
  • 307
2 votes
1 answer
162 views

How to use same cell for printing in a loop?

I want to manually tag images. For example, we prepare a list of images like imgList = Table[Rasterize[i, ImageSize -> 50], {i, {a, b, c}}] it gives Now I made ...
matheorem's user avatar
  • 16.9k
6 votes
1 answer
916 views

Save loop output in a data file

I started working on Mathematica today. I'm trying to save the result of a loop in a file as a column, very simple to do, but hard for someone who doesn't know Mathematica syntax. Here is my code: <...
nosumable's user avatar
2 votes
1 answer
187 views

Loop over multiple variables with readable code

I've seen similar posts to this, but not with as many variables. I need to loop over 8 variables, with only two values each(1 and 2), so I can loop over the 16 possible input and output states, and ...
Lion Frangoulis's user avatar
2 votes
2 answers
275 views

Storing the data at the end of the For loop

I have a For loop ...
mrt31's user avatar
  • 123
0 votes
0 answers
56 views

Algorithm consisting of modules returning multiple errors

I'm trying to write a genetic algorithm that will take a list of lists of reals in interval [0,1], my "starting population", "score" them according to a fitness function, append ...
J0ta's user avatar
  • 65
0 votes
0 answers
92 views

While loop to solve a diffusion problem

Crossposted at Wolfram Community: https://community.wolfram.com/groups/-/m/t/2506329 I am trying to use a While loop to solve for a diffusion problem using the power iteration method. The code is ...
joe a's user avatar
  • 1
3 votes
1 answer
105 views

Trying to add up values into a matrix

first post here. I'm currently completely stuck and frustrated with mathematica. In retrospective, it might had been a mistake to use it for this, I'm actually considering to somehow export the ...
Hewe's user avatar
  • 33
1 vote
1 answer
106 views

Monitoring Table or Do with Print

Imagine I want to track the progress of constructing a complex Table or Do object by temporarily printing the iteration number. ...
sam wolfe's user avatar
  • 4,187
4 votes
1 answer
164 views

Efficient iteration of matrix multiplication into a table

Suppose I have a list of matrices $(A,B,C)$ and a vector $v$. I want to construct a table whose elements are sequential applications of these matrices to $v$, i.e., the first entry will be $Cv$, the ...
miggle's user avatar
  • 647
2 votes
2 answers
471 views

How to put a FOR loop in a WHILE loop

I have a strange matrix like this one in the picture Now, my goal is to change all the 1s with random complex numbers of modulus 1, but if the element $a_{12}=z$, then $a_{21}= \bar{z}$, so the ...
fcoulomb's user avatar
  • 135
3 votes
2 answers
105 views

Check certain expression using a while loop to run through all posibilities in a range

Well, I have written the following code (using the fast square root test found in this answer): ...
Jan Eerland's user avatar
  • 1,941
2 votes
1 answer
48 views

How to convert a While into a NestWhileList

How do you rewrite this While statement with NestWhileList? I know you can easily get this done with ...
Sanmuten's user avatar
2 votes
2 answers
189 views

Calculating successive derivatives of a monomial list using a Do loop

I've got the following monomial list: monlist={c x^3, x^5, 4}; I need to compute the successive derivatives of that list of monomials such that when the derivative ...
E. Chan-López's user avatar
1 vote
1 answer
238 views

How to calculate Jensen-Shannon divergence?

How to write the function (or any other methods) to calculate Jensen-Shannon divergence (JSD) for two (p and q) discrete probability distributions? I need to calculate JSD for too many different (over ...
Parviz's user avatar
  • 383
0 votes
1 answer
60 views

Choosing variable values from a list using Do and If Loops [closed]

...
MK Luwang's user avatar
2 votes
2 answers
209 views

How to put Break command within a Do loop over 2 variables

Suppose I have the code Do[If[f[i,j]==1,{output[i]=j,Break},Continue],{i,1,5},{j,1,1000}] Where $f$ is just a function. I want the code to find the first $j$ that ...
Gabi23's user avatar
  • 295
1 vote
3 answers
112 views

Optimizing operations between vectors

Basically I am generating a grid in space (so a vector where v[[1]]=position 1 in x) and a random number of particles in space (so a vector where p[[1]]= position of particle 1 on space). Afterwards, ...
divica's user avatar
  • 21

1
2 3 4 5
7