Questions on writing non-trivial programs in Mathematica. Do not use this tag for questions on plotting/graphics or for questions on doing mathematics with Mathematica, where the focus is more on the math than the program.

learn more… | top users | synonyms

2
votes
4answers
134 views

multiple sequential definitions, defining own set function

I want to define a function for doing multiple definitions, where I apply the same function f to all values, and definitions are done not at the same time but after ...
2
votes
2answers
178 views

How to unprotect GeneratingFunction

I'm trying to add custom downvalues for GeneratingFunction, and to do so I thought I just had to Unprotect it. However, that ...
2
votes
1answer
1k views

Executing a .m file from the Linux command line

I have some code in a file named Demo-3.m, It can be evaluated in Mathematica 9. The final expression in the file is ...
2
votes
3answers
87 views

How to compare and remove x-coordinates (and the associated y-coordinates) that are less than previous x-coordinates?

I am using Mathematica version 9.0 and am trying to compare and remove x-coordinates, and the associated y-coordinate, that are less than previous x-coordinates. Below is an example using sample data. ...
2
votes
2answers
68 views

Sum broken into two sub-sums according to a condition on the terms

I have a sum in mind, but I want to break it into two sums and get a separate total for each of the two. All cases where the square root is accurate, i.e., exact All cases where the square root is ...
2
votes
3answers
115 views

Problem with the output of a function I program

(This is the function that I programmed) Zm2[Z_, nk_] := (zm2 = 0; For[i = 0, i < Length[Z], i++, zm2 += nk[[i]] *Z[[i]]* Z[[i]];]; Return[zm2];); ...
2
votes
2answers
107 views

Creating a Cayley's table of square

I'm interested in creating a Cayley's table of square using Mathematica. I'm not a programmer but occasionally resort to using Mathematica to simplify my work insofar as the programming doesn't take ...
2
votes
2answers
3k views

Product of Fibonacci numbers using For/Do/While loops

I want to find the product of the first 20 Fibonacci numbers (the fibonorial). I want implementations using While, Do, and ...
2
votes
2answers
779 views

defining recursively a function with multiple if conditions

I am trying to recursively define a function which satisfies the following system of equations and which depends on two parameters $n$ and $l$, $$ \begin{align} A(x, n, l) &= F[ A(x,n-1,l) ]\\ ...
2
votes
1answer
845 views

Optimizing a function involving NDSolve results in error messages

I want to minimize a function f with NMinimize but an error occurs that says: ...
2
votes
2answers
154 views

Recursive Euclidean algorithm in Mathematica

Can anyone explain to me how do I use a recursion, if I don't know the limit? For example, I need the remainder $r$ of the Euclidean algorithm for $\gcd(a,b)$ which equals $0$. I figured out that the ...
2
votes
1answer
188 views
2
votes
1answer
102 views

Map and Scan ignore HoldFirst

I am trying to write a simple function that takes a list of data and outputs it to a file. I'd like the name of the file to be the same as the variable that holds the list. It seems that giving a ...
2
votes
1answer
74 views

Clean & efficient bounding box non-intersection test

I would like a quick test to tell me that segments $ab$ and $cd$ cannot intersect in the plane. If the maximum $x$-coordinate of $a$ and $b$ is less than the minimum $x$-coordinate of $c$ and $d$, ...
2
votes
1answer
72 views

Can I use Switch with an inequality? [closed]

I'd like to have something like: Switch[x, 10, a = 3; b = 7, >10, a = 5; c = 6, _, a = 7; d = 12, b = 1]; I can't seem to get this to work.
2
votes
2answers
134 views

How to plot points with error circles on map with latitude/longitude grid

I am currently working on a project to model the location of icebergs in the northwest atlantic. When an iceberg is sighted, an error circle is plotted around it (with a bivariate normal probability ...
2
votes
1answer
230 views

What is the correct way to break a long expression across multiple lines?

This is probably a very simple question and I am probably not using the right keywords in my search; but I have a long expression that I would like to break across multiple lines to improve ...
2
votes
1answer
104 views

Please explain this while-loop

Code: ...
2
votes
1answer
402 views

Converting a nested for loop from MATLAB

In MATLAB I have the following series of nested for loops that update a matrix. (It's not my code, but someone else's that I'm trying to replicate - I don't know ...
2
votes
1answer
290 views

Call Functions From File Without Modifying Context (Sandbox)

Let's say I have file named test.m containing test[arg1_] := ( Print[arg1];) What is the best technique for calling test like ...
2
votes
1answer
250 views

Is it possible to treat starting values as variables?

I have a problem where I am using FindRoot a gazillion times over a grid of parameters. I need to allow the starting values to vary a bit with the parameters to get it to converge. Here is a simple ...
2
votes
1answer
161 views

Rookie mistake in defining a function with Modules?

I can't figure out what's wrong with this piece of code. I'm trying to define a function that counts the occurrences of x, y and ...
2
votes
1answer
126 views

Import a file only if it contains a certain pattern

Let's say I have three files containing: File 1: x y z 1 4 6 File 2: x y z 2 5 9 File 3: x y z 5 4 8 My goal is to import the data from a file only if in ...
2
votes
1answer
229 views

Fighting Less::nord at the root: Any unintended consequences?

After being plagued too much by Less::nord and companions, I've decided to try to fight it at the root: The comparison operators. I noticed that typically, the bad ...
2
votes
1answer
153 views

How to determine whether an expression can act as a “good” function?

It's not easy to see whether an expression can act as a "good" function. A good function refers to an operater that transforms symbolic entities like 1+x^2 or ...
2
votes
1answer
143 views

How to speed up my code about Divisors?

This code is about PE496 In[21]:= ParallelSum[n*Count[Divisors[n^2], a_ /; n < a < 2 n], {n, 1, 10^5}] // AbsoluteTiming Out[21]= {5.987342,10724527832} I ...
2
votes
1answer
77 views

How to package a function for export?

I often post code for functions on Stack-Exchange, and then later find that they are unexecutable - that people can't use them because I mistakenly omitted some private functions or values that are ...
2
votes
1answer
91 views

Probability evaluation fails with equals condition

The following evaluation fails (the result is just the copy of the input) $$\text{Probability}[0<x\leq a | x-y=t,\{x\ {ExponentialDistribution}[\lambda ],y\ {ExponentialDistribution}[\lambda ...
2
votes
1answer
90 views

CreateDocument options needed to suppress the default notebook menu

What are the options needed in Mathematica 10 to suppress the menu on top of any notebook created with CreateDocument? For example, the Messages window as well as the windows generated by ...
2
votes
1answer
159 views

How can I rewrite my code to make it simpler?

Two months ago,I written a function IRobot to caculate the $I_{xx},I_{yy},I_{zz}$ and $I_{xy},I_{xz},I_{yz}$,shown as below: ...
2
votes
1answer
867 views

Calculate mean square displacements for different particles from a excel file

I have the particle movement data in the following format: ...
2
votes
1answer
237 views

Combining two notebooks

I have a notebook which contain all the variables and defined functions. I want to do two tasks. As an example, I want to find the density and energy which have equations in terms of above mentioned ...
2
votes
1answer
96 views

PaneSelector and options in a Module

Consider the following snippet of code: ...
2
votes
1answer
71 views

Block works when directly used but not when used in a function

I want to calculate a derivative such as: $\delta f=\sum_{i=A,B} \sum_{k=1,2,3} \frac{\partial f}{\partial a_k^i}$. Since $ a_k^i$ are assigned values other places in the code, I have used ...
2
votes
1answer
814 views

Mathematica TCP socket client toward Trading Platform

I'd like to connect Mathematica with my Italian electronic trading platform. To connect I need to open a TCP socket client towards the trading platform and send a string command. The high-level ...
2
votes
1answer
300 views

Range of stability for iterative map

Using linear stability analysis, I would like to compute the range of stability of the fixed points and the 2-cycles of the following iterative map: $x_n = x_{n-1}^{2} - 3\mu$. Setting $x = x^{2} - ...
2
votes
1answer
452 views

Cournot Equilibrium

I'm trying to determine reaction functions of a Cournot equilibrium for $n$ firms using the same optimal condition, using the fixed point method. This is the method I'm using: ...
2
votes
1answer
85 views

Implicit derivatives in one line. How do I do it?

I have this little script to derive implicitly $PrePrint = # /. {D[y_, x_, NonConstants -> {y_}] :> y'[x]} &; Example: ...
2
votes
1answer
60 views

Evaluate recursive funtion as a whole before computing a recursive step

I'll try to make a basic example of my problem with $n$ being an array of elements, ex. $\{1,2,2,3,3,3\}$: If i have a sum which needs a computed value i would like to compute it beforehand and store ...
2
votes
1answer
72 views

Are there methods to serialize and deserialize large calculation runs? [closed]

I have a fairly large calculation to map an electric field over large areas (square kilometers). This runs for a fairly long time. Are there ways to serialize the current state of a running ...
2
votes
1answer
139 views

Need help looping a notebook evaluation

I'm a new user and would like to find the minimum value of soln1[30] obtained after repeating the following evaluation (below) for 1000 cycles (for instance). I can obtain sequential values of ...
2
votes
1answer
172 views

Discard functions with wrong symmetry?

I have functions of x, where x is the polar angle of spherical coordinates, i.e. ...
2
votes
1answer
283 views

Can Mathematica help a theorem to be alive?

We are used to do limits by means of $\epsilon, \delta$. This is the first step an student encounters limits. Using $\epsilon$ and $\delta$ leads him to be familiar to Logic. I have taught limits and ...
2
votes
1answer
133 views

Multiply out product of sums

For a specific quantum mechanical problem I need to multiply out operators in order to calculate a trace by hand. For example I need a Hamiltonian squared with $H^2$. The Hamiltonian contains of a few ...
2
votes
2answers
82 views

Need help updating an old Mathematica worksheet

I found a notebook that symbolically applies the multiple time scales method to any differential equations. I don't like just using other people's code so I am going through the author's work line by ...
2
votes
2answers
190 views

Solving a dynamic program via recursion

I'm trying to use recursion to solve a joint inventory/ dynamic pricing problem as in monahan, petruzzi and zhao 2004. I tried to solve for y[t] and ...
2
votes
1answer
171 views

Piecewise function with user-input interval

I would like define a function g[data,bins,x] which is a piecewise constant function taking value data[[i]] in the range ...
2
votes
1answer
72 views

Evaluate only one from a set of procedures

Let's say I've got a list of procedures: {Print[1];, Print[2];, Print[3];} And I want to only evaluate 3rd one: ...
2
votes
1answer
200 views

Differential operator involving permutations

I would like to construct an operator in Mathematica of the following form, $$D_i=\partial_i +\sum_{i \neq j}^n\frac{1}{x_i-x_j}p_{ij}, \quad i=1,...,n$$ where $p_{ij}$ is a (symmetric) permutation ...
2
votes
1answer
247 views

Optimal image quality using Rasterize Function

Based on previous posts about plotting two y axes on the same x axis, I wrote this function: ...