Questions tagged [function-construction]

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.

Filter by
Sorted by
Tagged with
1
vote
1answer
43 views

How can I find the minimum and maximum output of a function and specify the valid ranges and steping of each variable?

I am a software developer, and it has been far too long since the last time I've needed to do any math this complicated. I've forgotten a lot of the proper terminology, which has made finding answers ...
3
votes
2answers
77 views

Construct a function that maps strings to functions

The main question is the last 5 lines (including codes) of this article. A[x_, y_] := Evaluate[ToExpression["x^2+y^2"]] A[3,4] Then the output becomes ...
0
votes
2answers
41 views

Using MinValue with NDSolve

A simplified example looks like this. The basic idea is to maximize fHelper by adjusting \[Alpha] for each ...
1
vote
1answer
39 views

Having trouble when using a piecewise function

I am trying to define a function where H1[x_]= a previously derived value if 0 < x <100 and H1[x_]=x if x>100 so I have used the following piecewise function: H1[x_]:=Piecewise[{{Evaluate[y[x] /....
2
votes
2answers
51 views

Hiding variable names in function definitions

I have a problem where Table and similar functions' substitution variable name can conflict with names in my function it self. Here's a minimal example ...
0
votes
1answer
84 views

Automating a large operation by making a function

I would like to automate the following operation. To be transparent I shall put my code into separate chunks and explain the process. Suppose I have the following: ...
0
votes
0answers
26 views

Defining a function from an evaluated function stored in a variable

I have the evaluation of a function b, stored in appo. Now I want to define a new function f ...
0
votes
1answer
40 views

Sequence of functions iteratively defined

I would like to iteratively define a vector of functions (called copies in the following) depending on a parameter s and a ...
2
votes
1answer
40 views

Narrowing the range of an interpolating function while keeping integral the same?

Consider an interpolating function derived from some data, e.g. ...
0
votes
1answer
48 views

Creation of a Function

I am trying to create two functions but I am having trouble. The first consists of converting degrees Fahrenheit to Kelvin using this formula (F - 32)*(5/9) + 273 ...
0
votes
0answers
58 views

Implicit function definition and variable change

in my code I solve a non-linear system of ODE to obtain the positive functions RadEnDens and PhiEnDens, everything as a function ...
28
votes
1answer
3k views

How does Return work?

It is not completely clear to me how Return[] works. The documentation says: Return[expr] returns the value expr from a ...
1
vote
2answers
72 views

fitting a monotonic function?

Let's say I have a few points and derivatives given for a function and I'd like to find the best fit through the data, which is monotonic. For example, if I take a polynomial ansatz, I get ...
2
votes
0answers
70 views

Lambda calculus reduction in Mathematica

Are there any built-in functions for reducing lambda terms in Mathematica? If not, how might one do it? I've heard of Function, which is good for $\beta$-reduction, ...
4
votes
1answer
33 views

Computing Higher Order Tensor of Variable Rank

The following code takes a vector x of variable length, computes the outer product of the vector with itself to form the matrix $\rho$ of dimension $2^n \times 2^n$....
0
votes
2answers
127 views

Specifying that function argument is positive

Struggling with something basic. Suppose when defining f[x_] the outcome depends on Sign[x]. When calling this function, how do I tell Mathematica the sign of the argument? My attempt: ...
4
votes
1answer
48 views

Inverse of vector-valued function

Having a differentiable function $f : \mathbb{R}^2 \longrightarrow \mathbb{R}^2$ such that $\det D(f)$ (Jacobi determinant) does not vanish. How can we get the inverse function $f^{-1} : \mathbb{R}^2 \...
1
vote
3answers
92 views

Creating recursive functions using `SetDelayed` and `MapThread`

EDIT: I have slightly modified the question to include recursive function. I want to create some recursive functions using SetDelayed within ...
1
vote
1answer
30 views

Cut a list into two sublists containing alternating chunks? [duplicate]

Consider a list such as list=Range[20] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20 } I would like to have a function (e.g. ...
5
votes
3answers
67 views

Approximate strictly positive solution to a linear set of equations?

Consider a positive matrix M and a positive vector b, e.g. ...
4
votes
2answers
76 views

Define a curried anonymous function

I want to define a function like f[x_][y_][z_] := x + 2y + 3z but anonymously. The ways to do this that I've discovered so far are (1) define a normal function ...
16
votes
2answers
289 views

Improving Performance of an XY Monte Carlo

I normally write my Monte Carlo codes in Fortran for speed, but I was doing some quick and dirty work and wrote one in Mathematica for the XY model on a square lattice (see Kosterlitz-Thouless ...
2
votes
1answer
55 views

Operate on functions with arbitrarily many arguments

Is it possible to define a function that can operate on another function with arbitrarily many arguments? For example, I want to do error propagation using partial derivatives. It is not clear to me ...
1
vote
1answer
38 views

Mapping function into elements of Sum

I'm trying to set an attribute that's valid for a generic sum, where n and f are arbitrary. de[A_ + B_] := de[A] + de[B] de@Sum[f[i], {i, n}] Is this possible? ...
1
vote
1answer
78 views

How to use Manipulate in filter design to plot the sum two raised-cosines?

I would like to be able to add a raised cosine to a "standard" raised cosine. With a time delay which I could manipulate (so the shape of the standard cosine function is altered, meaning the function ...
6
votes
2answers
140 views

Collect data points in a list into weekly groups?

Consider data of a format such as ...
1
vote
2answers
44 views

Restrictions as string to expression in region plots

I'm trying to build a function that generate a string of restrictons to pass on to a region plot with ToExpression, but I think it fails to pass the plot variables. ...
1
vote
2answers
198 views

Evaluating ReplaceAll inside SetDelayed, first

Consider the following (simplified for the purpose of example) definitions: f[x_] := y /. y -> x g[x_] := x Where for example, the definition of ...
3
votes
1answer
53 views

Implementing Simpson's rule with two variables

For one of my lab project I need to integrate a given function (in this example, f1, using Simpson's ruls. When only one variable ...
3
votes
2answers
123 views

Automatically bound histogram regions?

Given a histogram of some data, such as e.g. in the following sample https://www.codepile.net/pile/NLlNgP60 ...
0
votes
1answer
34 views

Functions with Variable Numbers of Arguments: Outer Vector Product

I am trying to write a function that takes a column vector of variable dimension as an input and returns the outer product matrix as an output. What I have tried: ...
1
vote
2answers
119 views

Constructing an I-graph

I am attempting to build I-graphs in MMA, i.e. the non-Generalized Petersen graphs, and I found some code from Eric Weisstein, but this is now outdated and doesn't run. I have attempted to switch the ...
3
votes
4answers
1k views

How to define conditional function with Mathematica?

How to define conditional function $f(x,y)$ with the following result. If $x=1$ and $y=0$ then $f=77$, if $x=0$ and $y=1$, then $f=66$, if $x$ and $y$ are all other integer values then $f=0$. How to ...
1
vote
1answer
40 views

Higher Order Tensor of Variable Rank

I would like to write a function, that matricizes a higher order tensor according to the following rule: Let $\mathcal{A} \in \mathbb{C}^{I_{1} \times I_{2} \times \ldots \times I_{N}}$ be a tensor ...
0
votes
0answers
44 views

Function works outside For loop but not inside, and loop fails spectacularly

I'm building two lists, each to carry a different representation of the same set of objects. One list, list2, is constructed first; then the second list, ...
0
votes
1answer
40 views

WriteLine[] not working once put into a function

I wrote a function that takes two strings, an integer, and an OutputStream, it's supposed to write to. The function is: ...
1
vote
0answers
26 views

How to measure the Euclidean distance between each evaluation point and the closest reference point in a 2-D problem? [closed]

I would like to measure the euclidean distance between each evaluation point and the closest reference point in a 2-D problem. Attached my reference and evaluation points to have a look at the inputs....
1
vote
0answers
34 views

How to convert output from Reduce into a piecewise function?

Reduce often provides a much fuller solution than Solve. But it's always in the form of a true statement rather than functions ...
3
votes
2answers
86 views

Function with string explanation for its arguments with equal signs [duplicate]

I want to write a function using string as explanation for its arguments so that the function becomes intuitive for new users. For example, I want to write a function to compute BMI (just for ...
3
votes
1answer
123 views

Exponential generating function

I need to find the $m^\text{th}$ term for the following expression: $$ \left.\frac{\partial^m}{\partial t^m}e^{a t^2}\right|_{t=0}$$ I computed first few terms and used mathematica "...
2
votes
1answer
89 views
6
votes
5answers
401 views

What is the the proper way to define a function from an expression?

Say after some long computation we get an expression expr=x^2 We do not know what the value of expr beforehand. Now we want ...
6
votes
1answer
59 views

How to obtain the function of a plotted Bezier curve?

I have some control points and I could just plot the Bezier curve, but I also need to have the function! How can I get the exact function of the plotted Bezier curve?
1
vote
2answers
73 views

Understanding how to loop over different variables in Mathematica

I'm using Mathematica to generate some data, and I'm having some difficulty understanding how my intuition from "regular programming" should transfer over. The code that I'm writing does the following:...
0
votes
1answer
34 views

Defining a function that modifies a list [closed]

I have the a list A and I want to define a function f that takes out the $i$-th element from the list ...
1
vote
1answer
109 views
4
votes
3answers
719 views

Function that detects repetitions

I have a large list of 2-tuples and I need a function that lists first values that have at least one repeated second value. Small example: ...
0
votes
2answers
39 views

Test if function of multiple variables is positive or negative given variable constraints

I have a function of multiple variables (5 to be exact). I would like to know if this function is positive or negative given certain constraints on each of the 5 variables. Here is a simplified ...
3
votes
1answer
65 views

elegant definition of piecewise linear functions (spline)

As far as I understand, the Spline object is the result of some approximation or interpolation, it is not for directly defining functions. That's a pity, as that ...