For questions on writing functions (pure or using Set/SetDelayed) for any purpose, including the features that may be incorporated in those functions, such as options, patterns and conditions.

learn more… | top users | synonyms

1
vote
0answers
61 views

Find lengths of contours in a ContourPlot

I am trying to find the lengths of different contours in the following plot: It is a complicated piecewise function evaluated on the unit disk. I am hoping there is an easy, generalized way to ...
0
votes
1answer
74 views

A problem with set values

The following code is just an example to clearly explain my problem. Assume that : ...
0
votes
1answer
70 views

Define a function that takes ragged array [on hold]

newbie here. I want to define a function that takes a ragged array and return another one, but how. Say, I want to define a simple function behaves like this ...
0
votes
0answers
37 views

A problem with functions and Infinite expression 1/0 encountering

Following code (function) makes integrals and then replace f[x] with Cos functions and calculate the definite parametric ...
0
votes
1answer
102 views

iterative programming

i have 3 equations with 3 variables and two constants(to be substituted repeatedly). the value of the variables we got by applying nsolve on the system of 3 equations assuming some initial value for ...
1
vote
3answers
73 views

Initialize table in a Module but Break if criteria not met

I am initializing a table inside a function as follows: f[a1_, a2_, a3_] := Module[{RM = Table[a1+a2+a3+i1, {i1, 1, 10}]}, Plus@@RM] I am calling the function ...
0
votes
0answers
43 views

How can I use a For loop in a recursive function?

I am creating a simple radix2 FFT algorithm (based on Cooley-Turkey) by using a recursive function to obtain the fft. The function is supposed to work on a list (assuming the length is a power of ...
0
votes
1answer
78 views

How to produce functions [closed]

I want to produce n functions. For example, assume that by giving n=3, 3 definite integrals will be created, like the following: ...
0
votes
2answers
72 views

Constraining function found by NDSolve to stay positive

I am trying to constrain my variables in NDSolve so that they stay positive for the integration interval. Is is possible to do that? Using Assumptions as in ...
3
votes
0answers
63 views

Getting Arg[z] to go from $0$ to $2\pi$

I'm defining branch cut functions, and I'm using $arg(z)$ as a building block. So I just spent an hour at the whiteboard assuming that $arg(z)$ goes from $0$ to $2\pi$, and then I implement the code, ...
1
vote
2answers
50 views

How to define a function $f(x)$ such that $f(1.5)=1.5$, $f(1.)=1$, $f(0.)=0$, etc.?

How to define a function $f(x)$ such that $f(1.5)=1.5$, $f(1.)=1$, $f(0.)=0$, etc.? Namely, if $x$ is an integer with a decimal point, $f(x)$ returns the integer only, otherwise returns $x$. This ...
14
votes
1answer
273 views

How to represent a continuous monotonic phase of Airy functions?

Note: In this question I am concerned only with real-valued variables and functions. DLMF, §9.8 Airy Functions, Modulus and Phase, formula $9.8.4$ defines the phase of Airy functions: ...
0
votes
2answers
55 views

Determining whether or not an expression is an integer

I'd like to define a function that controls if a certain number is an integer, rational, algebraic and so far. First, I tried generating a list of those numbers: ...
6
votes
1answer
85 views

A question about two ways to use Default

Usually we define a function with default values by the syntax x_:default, but there is another technique to do this by registering a global default value with ...
1
vote
1answer
66 views

Need help defining my own rounding function

First of all: I'm a beginner with Mathematica. I'd like to define a function called Rounding which does the following: if for $x = n+a, n \in \mathbb{N}, a\in ...
3
votes
1answer
54 views

Problems using hand-made transformation functions

I experience difficulties when using custom transformation rules. Below I give two concrete problems. I am interested in fixing each of them and in understanding what is wrong with my attitude to the ...
2
votes
2answers
147 views

Defining a function that returns “undefined” if its value is outside of a specific range

I want to define a function f(x) which: $f(x)=x^3 +10x+ 5$ if $f(x) >0$ $f(x)=Undefined$ if $f(x) <0$ I have a idea: ...
1
vote
1answer
100 views

How to replace a list with the result list after each iteration [closed]

I have some initial values such as L1 list: L1={10,20,30,40,50}; I want to use the L1 ...
0
votes
1answer
55 views

Multiple Simulations of a Markov Chain

I am trying to create a simple series of Bernoulli bits whose probabilities follow a two-state Markov chain. ...
5
votes
3answers
328 views

How do I designate arguments in a nested map?

Say I have two lists, list1 = {a, b, c} list2 = {x, y, z} and I want to map a function f over them to produce ...
14
votes
5answers
735 views

Picking random items out of a list only once

I'm trying to create a function that randomly returns a value from a list but remembers the values that have been given before. At the end when the list is empty it should return an empty list. ...
1
vote
1answer
59 views

How to input pure function to package?

I would like to input a pure function into a custom written package, but am running into a variety of errors. The package I have created is as follows: ...
1
vote
1answer
99 views

Requesting explanation of FitModel code

I am struggling with understanding the code given in this original post I understand how the tests are conducted, both of them. However, I do not understand this part: ...
1
vote
1answer
117 views
0
votes
1answer
41 views

How to put some values from a list into a function in a Do loop

I want to program a code in MATHEMATICA in order to calculate some values in different iteration. For example, assume this list: list={1,2,3,4,5} Now assume that ...
0
votes
3answers
117 views

Clean-up Code / Reduce variables

I'm writing a playing card simulator of sorts, and everything works fine, but some Modules use a disgusting amount of variables. For Example: ...
0
votes
1answer
62 views

Normalize equation in NDSolve?

I have numerically solved and plotted the solution to the Mathieu equation using NDSolve successfully, as below: ...
8
votes
2answers
318 views

How to create custom Graphics primitive?

How to create custom Graphics primitive? It should have the following properties, resembling properties of built-in geometric figures, like ...
0
votes
1answer
67 views

Numerical Methods - minimum of a function

Problem: The length of the longest ladder that can negotiate the corner depicted in Figure 2 can be determined by computing the value of theta that minimizes the following function: ...
22
votes
6answers
414 views

Constructing symbol definitions for With

I would like to be able to define two arrays, one containing symbol names and one containing the values of those symbols, for use in constructs such as With. For ...
1
vote
0answers
42 views

Function behavior based on Head

I'm figuring how to implement a function behavior based on the Head of its argument. I mean ...
1
vote
2answers
96 views

Functions of two arguments

I am pretty weak on the notion of pure functions, etc. I found a clean solution to my problem so in a sense I've already answered my own question but I don't feel I'm on terra firma. I wanted to take ...
4
votes
4answers
465 views

“do … while” loop equivalent in Mathematica [closed]

Have I missed something or is there no built-in which mimics the behaviour of do ... while loop? I am looking for a construct that evaluates procedure once and ...
0
votes
0answers
37 views

Iterate each possible subset

Given a set of integers $S=\{2,5,8,14,18, \dots \}$, does Mathematica have a way to iterate each subset (i.e. all subsets in the power set of $S$) with a loop construct? The purpose is to sum all ...
3
votes
3answers
278 views

Alternatives pattern in a function definition

I thought that I remembered a way to write a function definition using "|", but can't find documentation for it. So for example, I'd want to define: ...
0
votes
1answer
80 views

Working with functions

I am required to write a function that takes a function as a parameter. ...
0
votes
1answer
35 views

Convert variable into function

Sorry if this is a duplicate, but I have a variable called H. H is some complicated function of ...
3
votes
2answers
187 views

Differential operator squared

I defined a one-dimensional momentum operator $\hat{p}=-i\hbar\frac{\partial}{\partial{x}}$ in Mathematica p = -I * h * D[#, x]& and I want to get the ...
0
votes
1answer
104 views

Patterns of function-arguments in expressions

I want a function that compares an expression with a specific pattern. The expression has always the following form: ...
1
vote
2answers
82 views

Summing Over a Variable Number of Indices

I am trying to figure out the best way to include a variable number of indexes of summation in a program. For example: Suppose I want to define a function $g[n,s]$ that returns the sum ...
0
votes
3answers
75 views

How to combine multiple functions into a single function, indexed bya new argument

I have a system of expressions (matrices) which correspond to increasing orders of precision in a theory. These expressions are quite complex (require about 40+ lines of code to produce) and are not ...
5
votes
2answers
318 views

Multiply each element of an array by another array?

I would like to multiply an array by each element of another array, and am having a lot of trouble finding a built-in or automated method of doing. Here is an example of what I'd like to achieve: ...
3
votes
1answer
153 views

Extracting function calls from an expression

I have an expression like this: expr = F[0, 1] + 2 F[0, 0] - 17*F[2, 0] and I want to transform it to a list consisting of all function calls with its arguments: ...
0
votes
1answer
38 views

Using Derivative with list in curly brackets

I'd like to make a formula for a sum containing Derivative where the number of arguments in the latter depends on an input parameter n. Also involved is a list ...
1
vote
1answer
48 views

How to define an n-variate empirical distribution function probability for any n?

I'm using Mathematica 9.0 to calculate the probability according to the empirical distribution function (EDF) of some sample data. Afterwords this is included in a maximization stage so I define this ...
1
vote
1answer
52 views

Defining a function with global parameters

What I would like to do in Mathematica is something like f[x_] := a x g[a_] := NIntegrate[f[x], {x, 0, 1}] But then of course I get ...
0
votes
2answers
54 views

Defining Piecewise Functions in Modules

In short, my question is the same as my previous one except that everything is now wrapped up in a module. The relevant code I'm working with is: ...
3
votes
0answers
51 views

How to combine DifferenceRoot objects for odd and even-indexed terms

I'm trying to analyze a certain infinite sequence $S$, indexed by positive integers starting from $1$. It can be split into two subsequences: $S^{odd}$ by removing all even-indexed elements and ...
0
votes
1answer
154 views

Please explain this code (contains Append, Do, Coefficient)

I am a graduate student using a past student's Mathematica program. Will someone please explain to me exactly what this piece of code is doing? Specifically the ...
2
votes
0answers
78 views

Function that assigns properties [duplicate]

I want to create a function that creates and assigns properties to a symbol. In theory this would behave similarly to the NonzeroValues property of ...