# 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,752 questions
Filter by
Sorted by
Tagged with
65 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 <...
107 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 ...
152 views

### Automatic Piecewise Approximation

I would like to create the following function: It should approximate a function func[x] by piecewise polynomial interpolations, splitting the approximate function ...
2k views

### Enforcing correct variable bindings and avoiding renamings for conflicting variables in nested scoping constructs

Using global variables the following turns an "expression" into a Function: expr = 2 x; Function[x, Evaluate[expr]] Of course ...
33 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 ...
63 views

### Cache a function in mathematica

I have a function of one variable $f(x): \mathbb{R}\to \mathbb{C}$ which is very difficult to evaluate. Single evaluations may take hours, and I need to use the function at a large variety of points. ...
23 views

### How to define a function applicable to all type of numbers? [closed]

We can define a generic function like: Clear[f]; f[x_] := x^2 + 1; With this definition, we can calculate: ...
50 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 ...
102 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[1], f[3], f[x]} ] Out[]= {2, 4, Switch[x, 1, 2, 3, 4]} ...
438 views

### Define a New function like Log

I want some help here. I want to define a function called Lg[z] that has the property that Lg[a/b] is always converted automatically to the form Lg[a] - Lg[b] , when a and b are polynomials of some ...
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 ...
35 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

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 ...
3k views

### Creating the Nabla operator (also known as Del operator) as an operator

How can I define the nabla operator (also known as Del operator) as a an operator, acting on everything to the right of the operator! Also taking \[Del]^2 would ...
129 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 ...
108 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 ...
653 views

### Pure function inside another pure function

I have the following working expression: In[15]:= Length[Select[IntegerPartitions[10],First[#1]==5&]] Out[15]= 7 But, instead of using the constant 5 I want ...
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: ...
127 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: ...
67 views

### Derivative not evaluating when function defined

I have the following code ExD = Sum[D[y[n, t], t], {n, 0, 1}]; ExS = Sum[y[n, t], {n, 0, 1}]; σ[0] = 1; σ'[0] = 1; y[0, t_] = σ[0] + t σ'[0] ExD ExS Note that ...
1k views

### How to check the style and number of arguments like the built-in functions?

Given that I have a function sqrt[x] that I must check the style and number of argument x. I can do like this: ...
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 ...
816 views

### How to check the validity of an option value

I have a user-defined function, func, which takes the following form: func[arg1, arg2, options] Here is the actual code ...
96 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 ...
43 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 ...
39 views

66 views

### Output of Two Functions as a Single Function

I have two functions: One of which outputs a number and the other outputs a picture. What I would like to do is create a single function that would output both the number and the picture. As a MWE ...
94 views

### Mathematica unable to evaluate function which it can write explicitly in simple form

I have a function $\psi(x,n)$ in mathematica which is quite complicated. However, plugging in $n=0$ gives a simple form. Mathematica knows this (see below), however it is still unable to even evaluate ...
82 views

### Breaking a Kinked line into $(n-1)$ segments of equal Euclidean distance?

I'm wondering if anyone has a nice solution for breaking a kinked line into $(n-1)$ segments of equal euclidean distance? Here is a MWE of my attempt. I think it does the job, and I'm sure the code ...
119 views

### Making tables and update calculated values

I'm trying to make tables like in the image to calculate max and min of the expressions. Whenever I change the max and min of a and ...
99 views

### How to plot root of the complex equations

I want to plot the roots of the complex equation of a1. I have tried the following code but this is not giving me any result. I need bistable plot. How it is possible.? ...
43 views

### Code a function to obtain an output table from an input dataset taking into account some rules

Good morning, I have an input dataset in the following form: ...
37 views

### When (and how) is it allowed to first “use” a function before you “define” it?

In this little program I first use function f (first two lines) and only then define it (third line) Manipulate[f[a], {a, 0, 2}] f[3] f[x_] := x^2 Question: why ...
32 views

### How to convert Piecewise function to Interpolation function?

In the following piece of code I am trying to convert a Piecewise function to a simple interpolation function f[t_] ...
40 views

### Casual user - trying to understand how to define a recursive population growth formula

I'd like to play around with the sequence X n+1 = R xn (1-x) where the n+1 and n are subscripts I tried writing x[n_] = R x[n - 1] (1 - x) as I didn't know ...
71 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 ...
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 ...