Questions tagged [procedural-programming]

302 questions
Filter by
Sorted by
Tagged with
31 views

why does parallelDo ignore these 2 lines of code? [closed]

I have this loop ...
50 views

How do I apply sow and reap in this loop? [closed]

Basically I want to make a graph about the intensity of elp over a line during a time interval. However elp and ...
86 views

Optimizing operations between vectors

Basically I am generating a grid in space (so a vector where v[]=position 1 in x) and a random number of particles in space (so a vector where p[]= position of particle 1 on space). Afterwards, ...
57 views

How can I make ParallelDo faster than Do?

I am trying to optimize my code as much as possible. So first I used a compile and now I am trying to parallelize the code in order to make it faster because in the end I will need to make a loop that ...
97 views

How to use for loop in Mathematica?

in most of my job I have to repeat something for different part of data, for example making plot, usually the only thing need to change is index. Below is the example of the code which I have to ...
35 views

Concatenate string with the value of each element of a vector/list

I have a vector/list t = {4, 5, 6}. I want to output each element (its value) of t in a string of characters. For example, the ...
45 views

Why each iteration takes longer than previous one when using VariationalD in this loop?

I have a function $f$, and I would like to modify it iteratively to approach another one called $a$ (not because I think this is the best way to do anything, but for experimentation). For this, I use <...
74 views

Could anyone please help with effectively defining a vector-valued function of tons of variables so that ...
449 views

What is the most efficient way to define a Loop until function?

Mathematica has the While function which is very convenient when we need to process some code until the attainment of a certain state (identified with a condition). ...
89 views

An example code for doing a loop [closed]

I would like a code that works for doing loops as in the toy example below. This would help me to understand how to work with loops in more general contexts as I am still a beginner in Mathematica. ...
68 views

Parallelization in While[] loop when testing if a condition is met

Well, I am trying to run the following code: ...
131 views

Do loop so that I do not get double terms?

I have a Do loop where I generate quantum state equations <111||111>,<111||112>...<232||232>. This is 144 equations in total but I want both bra and kets to be seen as the same so ...
65 views

The use of NestList

After read Michael answer, code use NestList to compute and feed in numerical integration. I want to learn this idea into my code. In the ...
49 views

Loops Not Working- Kernel Crashes and Values Incorrectly Updated

I haven't used Mathematica in a while so any help is very greatly appreciated. I am trying to run a program with nested for loops that runs two function evaluations in the inner most part of the loops....
91 views

How to realize NDSolve by NSolve with the implicit constraint equations?

Daniel post the amazing code to realize numerical integrators, I think it is useful to the kind of one-step explicit equation like x[k+1]-x[k]== constant_number. In ...
45 views

How to compose an algorithm to create new array (based on array data), if I need a reference to an array being created, no for loop, in mathematica?

I have an array with either 1 or 0. From this, I am trying to create a new array with intervals. I have some base interval, one step, we can say 1 second here. If there is 0, I skip, if there is 1 I ...
51 views

How to use Mathematica to return value from for-loop [duplicate]

I tried to use Mathematica to implement random generator. The pseudo code I use is as following: ...
41 views

Evaluation self notebook with loop

I have a large notebook with multiple sections. I would like to run the entire notebook with one variable change kquads. At the end of the notebook, I added one loop ...
56 views

"With" use in a (implicit) nested loop

Symbolic code: Do[g[longcat[i,k],longcat[i,k]],{i,1,f[k]},{k,1,m}] Longcat is loooong and occurs several times in my expression g...
97 views

Binary permutation list code in Mathematica

Given some natural number $N$, I am interested in the set of all binary permutations of length $N$ (with the intention of storing in lists depending on how many $1$'s appear in each permutation). My ...
56 views

matrix problem: evaluation of column values which depend on a variable

I do not use Mathematica for a long time and I have a question about a matrix problem. I add here the code: ...
64 views

How to clear all things inside a loop?

It happens to me a lot when I write a loop, I have to Clear all functions, variables, definitions and etc to be sure my calculations is done correctly. namely as a ...
82 views

Building a Matrix as a piecewise-function of the indices without for loops [closed]

I'm very new to Mathematica and this site. I've read that building matrices with For loops is slow, and I have experienced this myself on the current project. I'm trying to wrap my head around ...
95 views

How to implement If criteria for the NestList-Function

I have a function which bisects a list in two parts recursively. Unfortunately I have to select a number of iterations for the NesList-function which is just an ...
335 views

Python recursive bisection algorithm to Mathematica

I've been sitting on this problem for a while and i am kind of stuck. I am pretty new to Mathematica and have been trying to implement a recusive Algorithm, which splits/bisects a list of items in ...
127 views

Cannot assign variable value inside Do Loop

This is more of a conceptual problem. Suppose I have three lists. x1 = {1, 2, 9, 10}; x2 = {5, 6, 7}; x3 = {9, 10, 13}; Now, I wish to keep only the first two ...
44 views

Increment FindMinimum range

I have a function SQ[b,zQ,zh] where I want to find at which zQ it is a minimum. The parameters are ...
54 views

view linebreak symbol

As you see in the above example, In and In looks just the same, but the outputs are different. The fact is that there is no linebreak symbol in In, but there is one linebreak symbol in ...
265 views

...
30 views

How to create ParallelDo loop with n dimensions of evaluation?

For example if I have n=2 i get ParallelDo[something, {v, c}, {v, c}] and if I have n=3 I get ...
43 views

Check an input matrix M contains any nonzero diagonal matrix element or not

Is there a way to easily check an input matrix M contains any nonzero diagonal matrix element or not? For example, if the M contains any nonzero diagonal matrix element, Output: True. if the M ...
72 views

Costly function inside nested Modules [closed]

The function initDataTable simply reset all data to Black first, then set the bottom right quarter to ...
63 views

How can I store vectors in a matrix

I am triying to generate realizations of a Gaussian random process using the KL expansion. For that, I need to multiply an eigenvector for an eigenvalue and a random variable. I have tried ...
109 views

Repeating and Sequencing Procedures with two Do-loops

I have a Do loop where I'm looking for the value of certain parameters that respect the conditions inside an If, and with those values, a run another Do to make another calculation of an expression ...
44 views

How to get a loop for a differential equation system?

I would like to know how can I obtained a loop of this system ...
48 views

Replacing the print display in a for loop

I am running a For loop as follows: table = {}; For[n = 0, n < 10, n++, {table = Append[table, n], Print[table]}] I was wondering if there was a convenient way ...
63 views

Matrix or loops or any ideas for help me

I have input data as below: ...
89 views

How to change a Do loop to a For loop? [closed]

I'm a rookie in the loops. I would like to use a For loop to get a solution like using a Do loop. ...
72 views

How to change a Do loop to a while loop?

I'm a rookie in the loops. I would like to use a While loop to get a solution like using a Do loop. ...
56 views

Assiging Plot Range to Variables Works For X but Not Theta

Observe the following Mathematica code: ...
48 views

Create list of functions, domains, and iterate over (compare to Python code)

In Mathematica, I want to do a mass export of many different animated plots of specific functions with different domains. Because I don't necessarily want the plots to always have exactly the same ...
222 views

how can i get the following figure using the for loop

how can i get the following figure using the for loop. n=5 For[i=1,i<5,i++,For[j=1,j<n,j++,A=(i+j)]] A//MatrixForm The code I found is above but not working....
50 views

Build a list from events

Say I have a list of elements of type {name, direction, time} where direction can be in or <...
40 views

Skip time taking loop values to take input next value after certain time

I am fitting using NonlinearModelFit, where I am trying to input guess values for parameters (g and xp1) from the for a loop. For a certain value of g or xp1, the loop takes infinitely long to compute ...
112 views

Generating a lattice by $𝑁^{𝑡ℎ}$ iteration of unit cell

This question is in continuation of the my previous question that was not clearly presented by me at all. I have a unit cell, and I would like to generate a lattice but in iterative order. For ...
81 views

Generate piecewise function using for loop

I want to specify a piecewise function by writing f[x_]:=Piecewise[piecewiseComponents], and use a loop to specify the components. I have tried ...
56 views

227 views

Large matrix inversion

I'm still learning Mathematica, and I'm trying to write a program that needs to invert a large (nxn, n=160 or possibly larger than this) matrix with numerical entries, multiply with to another matrix, ...
i am new to mathimathica and i just can do basic things with it. i have a file.txt that contains roots of two polynomials $p(x),q(x)$. both polynomials have the same degre and first n numbers are ...