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

10
votes
2answers
342 views

How do I get Mathematica to return a function call unevaluated?

How do I get Mathematica to return a function call (conditionally) unevaluated? I expect this may use the slightly-mysterious Hold function. As a toy example, ...
7
votes
0answers
67 views

Maximum number of literal values for SetDelayed?

Under what circumstances should I expect Mathematica to turn a delayed assignment into an immediate assignment? I have put together a minimal example (available here) which demonstrates this behavior. ...
1
vote
1answer
75 views

Requiring NumericQ in Pure Function

Potential duplicate, but doesn't seem to solve my problem: Using patterns in pure functions I have a function that returns an InterpolatingFunction, and it works ...
1
vote
0answers
61 views

How can I add options to functions [closed]

One wish take easy advantage of the packages/codes buildt through our engagement with mathematica. Through building packages I see that I have made several functions which are simple and closely ...
0
votes
1answer
57 views

function of derivative of a function

I know a similar question is answered somewhere, but it doesn't work for my case for some reason. I have the following problem in Mathematica. I want to compute f[h[x]]= x^2 + D[h[x],x]. Here, h[x] ...
0
votes
1answer
33 views

Defining arbitrary order derivatives function [duplicate]

I have a function $\phi(t)$ and I want to define a function $\phi D(t,r):=\phi^{(r)}(t)$, in other words evaluate the $r$'th derivative of $\phi$ at $t$. The naive code that I have is: ...
0
votes
1answer
45 views

Generating list of functions from list of expressions

I know how to create a function from concrete expression like Function[{x, y}, x^2 + y^2] But what if I have a list of expressions list1 and I know the set of ...
5
votes
3answers
104 views

Explicit use of Evaluate not behaving the way I expect it to

I discovered a strange behavior of the Evaluate function. Case 1: ...
0
votes
2answers
104 views

How to assign special colors to the output of DensityPlot?

I have created the following function for plotting ...
4
votes
3answers
412 views

Function with zero or one arguments

Is there a neat way to define a function with a single optional argument that defaults to an empty sequence? For example, suppose I want to define a wrapper for ...
4
votes
4answers
200 views

Speed up replace all

I have large list of rules, I would like to use list/.rule, but assume that rule is a set of rules rule 1, rule2 rule...I want ...
2
votes
2answers
92 views

Handling a sequence of unevaluated arguments

I have the following piece of code: ...
0
votes
1answer
76 views

Define a function that returns a function with a variable number of arguments

Previously, I've defined a function with a variable number of arguments with the following method. ...
0
votes
1answer
129 views

Speeding up a function

This is un updated version of a previous (now deleted) post of mine. I need to marginalize a distribution. I was suggested to use the code in this thread. This works just fine, but the problem is ...
2
votes
5answers
257 views

Returning all positions for all different occurences of elements in a list

Is there way to construct oneliner as pure function(s), so that I enter mylist only on one place - on the end of line. And that function return the same result as ...
0
votes
0answers
72 views

How to save the results in an identical Excel file (.xls) in each iteration of a Do loop

Assume the following code (Below code is just an example to clearly illustrate my problem): ...
0
votes
1answer
50 views

Change input variable in a function [duplicate]

I'd like to write a function, that would take a matrix as an input parameter and would change it by multiplying one of its rows by a factor. I try to do it in the following way: ...
1
vote
0answers
23 views

Minimizing roots of a transcendental equations [duplicate]

I'm trying to use the solution of FindRoot with parameters for further calculations. Annoyingly enough it always gives me trouble with replacing the parameters. As an example I'm attaching a simple ...
2
votes
1answer
141 views

Area between Contours in ContourPlot

I feel slightly foolish for asking this because I am so close, but I'm having trouble, so I will anyway. I asked this question two days ago regarding finding the lengths of contours. Now, I'd like to ...
1
vote
1answer
120 views

Plot of a periodic triangular wave

I'm trying to graph the zig-zag function at the top of this page: http://www.math.ubc.ca/~feldman/m321/nondiffble.pdf I have a little bit of experience with Mathematica but not a ton. Any ...
4
votes
1answer
158 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
83 views

Define a function that takes ragged array [closed]

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
90 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
105 views

A problem with set values

The following code is just an example to clearly explain my problem. Assume that : ...
0
votes
0answers
52 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 ...
1
vote
3answers
118 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
1answer
85 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
1answer
127 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 ...
3
votes
0answers
77 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
57 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 ...
0
votes
2answers
69 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
103 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 ...
0
votes
2answers
130 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 ...
1
vote
1answer
82 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 ...
4
votes
3answers
237 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
182 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
113 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
76 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. ...
1
vote
1answer
77 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: ...
0
votes
1answer
47 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 ...
1
vote
1answer
121 views
1
vote
1answer
103 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: ...
0
votes
3answers
132 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
79 views

Normalize equation in NDSolve?

I have numerically solved and plotted the solution to the Mathieu equation using NDSolve successfully, as below: ...
0
votes
1answer
100 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: ...
1
vote
0answers
55 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
103 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
815 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 ...
3
votes
3answers
318 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: ...