Stack Exchange Network

Stack Exchange network consists of 175 Q&A communities including Stack Overflow, the largest, most trusted online community for developers to learn, share their knowledge, and build their careers.

Visit Stack Exchange

Questions tagged [procedural-programming]

Questions about Mathematica's procedural programming paradigm.

0
votes
1answer
53 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
0answers
40 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
2answers
89 views
0
votes
1answer
40 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
3answers
122 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
1answer
88 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
1answer
126 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
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. ...
0
votes
0answers
27 views

Running a test over a list to alter list

I'm quite new to this. I have a nested list that represent solutions to an equation. I would like to create a new list that only contains one 'representative' of each element. As an example, imagine I ...
3
votes
2answers
84 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
2answers
42 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
1answer
31 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
2answers
80 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
1answer
47 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
3answers
60 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
1answer
54 views

How to make a table for loops?

I have a code given by ...
1
vote
1answer
85 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
1answer
46 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
1answer
60 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
2answers
73 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: ...
3
votes
1answer
111 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
1answer
27 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
1answer
24 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 ...
0
votes
1answer
184 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
1answer
95 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
1answer
131 views

Why can't I iterate?

I encounter a problem when computing the following equations: ...
2
votes
4answers
156 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
3answers
184 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
1answer
156 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
1answer
42 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 ((...
9
votes
2answers
152 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: ...
5
votes
1answer
452 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, ...
0
votes
2answers
43 views

Expressing values from an evaluated If function as a list

For[i = 0, i < 4, i++, Print[i]] gives the result: 0 1 2 3 by the definition of the If[] function. Given a much longer ...
3
votes
2answers
105 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 ...
0
votes
2answers
67 views

A list of operators in the For cycle

I'm pretty sure that analogous questions have been asked here a zillion of times, but... I think it is pretty straightforward from the code what I expect it to give: ...
1
vote
0answers
72 views

Using While properly

I am trying to make While[] work with function results and evaluate them at the same time. ...
0
votes
1answer
94 views

Randomizing Initial Conditions of NDSolve Differential Equation Phase Space Plots with Looping Structures

I am looking for a way to randomize the initial conditions on the numerical approximation for the angular displacement of a plane pendulum. I have been trying for hours, and believe a For loop is ...
1
vote
1answer
121 views

Golden section search method that terminate after $n$ iterations

I am attempting to code a program that executes the golden section search method and I would like to terminate the computations after $n$ iterations, where the user decides what $n$ is. My code works ...
0
votes
2answers
55 views

Loop doesn't hold the value

My program doesn't hold the value in a variable as I want it. I want to attach the calculated value as a list in a variable. What is wrong here? ...
0
votes
1answer
54 views

Do-loop with ParametricNDSolveValue not giving expected results

I have written code that using a Do-loop. In the loop I am changing the value of x, v, l and R, and looking at the computed ...
3
votes
1answer
260 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: ...
0
votes
0answers
49 views

Using Map function with NDSolve

I would like to solve a set of differential equations but vary one parameter, KieP, between 900 and 1100. The solution for a given parameter has many peaks. I would then like to interpolate between ...
1
vote
0answers
56 views
0
votes
4answers
129 views

How to use If and Goto in Table loop? [closed]

I have the following code, j=0; Table[{Label[1], j = j + 1, i, If[j == 2, Goto[1], Continue]}, {i, 1,10}] which does not work; I get ...
4
votes
2answers
245 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 ...
0
votes
2answers
75 views

Nested For loops simplified

In order to avoid a deep set of nested For loops, I came up with this method to do it with one For loop. The range of each level's For loop counting variable k1, k2, k3,... is 0 to b, and n is the ...
1
vote
1answer
54 views

Iterative loop for a given list

Suppose I have the following list: ...
0
votes
1answer
42 views

Function of a function using SphericalPlot3D

If anyone has any idea how to turn all of that plotting stuff (hcpii4plot) into a function which takes hcpii4angfunc[θ, ϕ] as an ...
2
votes
1answer
87 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 ...