# 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.

1,756 questions
Filter by
Sorted by
Tagged with
13 views

### How to create a special function that fills rows based on conditions for a matrix

I have two matrices p=({1,0},{2,0},{3,0}) AND q= ({3,0},{2,0},{1.2,0},{4,0},{2.5,0}). How do I generate a code such that the values in the first column of matrix q which lie between two succesive row ...
31 views

### Function of list [duplicate]

Suppose we define a generic function f[x_]:=x^2 and pass an argument to it: x={1,2,3}; f[x]. Does this operation imply that a ...
35 views

### Compile using intermediary function [duplicate]

If I do this aux1 = Compile[{{x, _Real}}, x^2]; aux1[10.] I get the expected 100. However, in my working case I have ...
30 views

### Is there a way to get a list of all the dependants of a function in Mathematica? [duplicate]

Suppose we have a function of the general form $$f=func(x_1,x_2,x_3,...,x_n)$$ I am looking for a way to get this function as input and output the list of all dependants, $$\{x_1,x_2,x_3,...,x_n\}$$ ...
29 views

### How to handle Units when Integrating?

I have some functions defined, which are to use units. Now another function is used to integrate over those, with the integration variable also quantified and the input to said function. I better just ...
85 views

### Why does this boolean computation get stuck?

I want to build a boolean function which takes three one-parametric functions. I really don't know how to write a function for this, but this seems to be working for me as long as the parameter is <...
41 views

### Using Solve[] in Module

Clear[x, y]; function2[eqn3_, eqn4_] := Module[{x, y, sol}, sol = Solve[{eqn3, eqn4}, {x, y}]; {x, y} /. sol] function2[y == 3 x, y == x^2 - 4] I want it to ...
104 views

### How to keep Switch default case as original function?

For example of below Switch function. In[]:= Module[{f}, f[x_] := Switch[x, 1, 2, 3, 4]; {f, f, f[x]} ] Out[]= {2, 4, Switch[x, 1, 2, 3, 4]} ...
53 views

### What is the difference between OptionsPattern and a sequence of Rule in a pattern?

I'm experimenting with options. In my first tests, I wrote something like that to capture the options given to a function: f[opts___Rule]:= {opts} I later ...
40 views

### Plot showing up blank when I define expression outside of Manipulate [closed]

I obtain an expression through various steps using the differentiate and solve tools, and then I would like to plot it using Manipulate. However, due to how the expression was obtained I would like to ...
58 views

### equality of algebraic expressions in Mathematica [closed]

Is there an inbuilt function to test equality of symbolic expressions in Mathematica? For example, I want to declare a function g that test whether a certain function f of x and y is identically 0 or ...
133 views

### Why does OperatorApplied by default reverse the arguments?

Perhaps this is just one of those Mathematica curiosities, but I'd like to know why by default OperatorApplied[f][x][y] === f[y,x] versus the more usual way one ...
59 views

### Constructing a List of Functions

I would like to construct a list of Functions that can be used like: F[z][[i]] Where one can choose between list elements with ...
109 views

### Define an $n$-variable function with $n$ undefined?

I would like to define an $n$-variable function with $n$ undefined. For example, I want something like this: $$f(x_1, \, \dots, \, x_n) = x_1 + \dots + x_n$$ And afterwards I want to take its ...
42 views

### Converting Timing to TimeObject

What's the proper way to get Timing and post-process it for example to convert to a TimeObject as a defined function? In line, ...
55 views

### Map patterns to values

Suppose I have a list of named patterns: listofPatterns = {a_Integer, b_Real, c_?StringQ} I would like to have a mapping from a pattern to a certain value like: ...
434 views

### Implementing “do this, then return that” in function definitions

In Python, I often use local variables in defining functions, like so: def f(x): k = initialk < 'do something to k' > return(k) I would like to do ...
97 views

### Dice Rolling Simulation [closed]

I'm simulating a sequence of dice rolls by choosing a random integer between 1 and 6. I need to use FoldList to generate a function that returns a list which describes how the cumulative total varies ...
44 views

### How to efficiently generate a symbolic function/model with prescribed calibration routine?

Imagine you have a model m[A] depending on some matrix variable A, for simplicity $A \in \mathbb{R}^{3 \times 3}$. The model is ...
73 views

### Issue with Solve, NSolve, FindRoot

I have this code that I've been trying to get working for some time. I asked some questions before which helped but still struggling! ...
34 views

### Issue with variables/functions in solving equations

I've been posting some questions all around issues with defining and using variables/functions in Mathematica. Based on the answers/comments I got I still cannot make this simple code work... Why is ...
68 views

### Setting the value of a global variable from inside a function

I am trying to set the value of a global variable X from inside a function. I want the value of this variable to change when the function is called. The function ...
158 views

### How can I use a loop with an iterator in order to evaluate a function?

I'm trying to evaluate a function, that depends on two variables, in a specific set of values. I will write an example, but my system is more complicated. ...
33 views

### Plot is passing a bizarrely shifted x_min value to the user-specified Ticks function

The documentation for Mathematica's Ticks option says that you can specify the ticks for an axis by passing a function rather than a plain list description. The ...
121 views

### Why does D ignore conditions specified on a custom function?

I define a function with a condition like so. ClearAll[foo]; foo[expr_] /; FreeQ[expr, bar] := expr Such that foo returns ...
45 views

### Using the (long) out of a function to define another function [duplicate]

It might be a trivial question but I haven't been able to find solution myself. Suppose that I have a calculation whose rather long output that is dependent in some variable e.g. ...
111 views

### Using module to define a function that calls another function: Should i use set-delayed (:=) or set (=)

Suppose that I have a function f[x_,y_,z_]=x*y*z and now suppose I want to consider a specific application where z is a ...
41 views

### Solving a system of trigonometric equation embedded inside module for multiple times

\begin{align*} u_{1}&=c_{1}cos(\beta x)+c_{2}sin(\beta x)\nonumber\\ u_{2}&=c_{3}cos(\beta (x-z_{1}))+c_{4}sin(\beta (x-z_{1}))\nonumber\\ u_{3}&=c_{5}cos(\beta (x-z_{2}))+c_{6}sin(\beta (...
31 views

73 views

### how to rewrite conditions as conditions&& True

I have a result comes from Reduce a <= -2 || (1 < a < 2 && (x >= 1/3 (4 + 4 a))) || a >= 2 it has a ...
61 views

### How to iterate a function

I have written a custom function called f1[r_] and I have tried iterating this function using Table. I work nicely for the first ...
19 views

### Hold form of a function until its parameter is defined

I'd like my function to evaluate only once it's parameter has been defined. If the parameter is undefined, I would like the function to hold until it becomes defined. As a trivial example, consider ...
44 views

### Need help understanding the function in linked answer (that symbolically inverts a piecewise function)

This Answer provides the following code ...
185 views

### Need help solving ODE with ParametricNDSolveValue solver

I am trying to apply @bbgodfrey method for finding the right initial guess for the following problem but I get an error message and I don't understand where it comes from. I would also like to ...
63 views

### How can I index a function with the type of dataset used

I have the following code which serves my purpose for a single matrix input. For one or two matrices, the operation can be done manually, but I have 65 matrices and ...
45 views

### Making a recursive function

Suppose I have the following functions: a[x_] := x b[x_] := x (a[x]) - a[x] + 1 c[x_] := x (b[x]) - b[x] + 1 d[x_] := x (c[x]) - c[x] + 1 as you can see each ...
40 views

### Nested maximum function not working

I am trying to maximize a function which is itself a maximized function. For example, see the below. Can someone explain this error: ...