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
0
votes
1answer
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 ...
1
vote
0answers
14 views

How to define the following hirota operator and simplify complex expressions with it? [duplicate]

How to define the following Hirota operator? I want to define one such hirota operator. When a complex expression is given, it can be reduced to a form containing only hirota operators through ...
0
votes
0answers
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\}$$ ...
2
votes
0answers
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 ...
2
votes
1answer
84 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 <...
2
votes
1answer
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 ...
0
votes
0answers
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: ...
3
votes
2answers
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[1], f[3], f[x]} ] Out[]= {2, 4, Switch[x, 1, 2, 3, 4]} ...
6
votes
1answer
52 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 ...
1
vote
0answers
36 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 ...
2
votes
1answer
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 ...
18
votes
1answer
131 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 ...
1
vote
1answer
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 ...
5
votes
2answers
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 ...
1
vote
1answer
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, ...
1
vote
1answer
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: ...
2
votes
1answer
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 ...
1
vote
2answers
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 ...
2
votes
1answer
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 ...
1
vote
0answers
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! ...
0
votes
1answer
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 ...
1
vote
1answer
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 ...
8
votes
1answer
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. ...
1
vote
0answers
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 ...
4
votes
1answer
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[12] returns ...
0
votes
1answer
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. ...
4
votes
1answer
110 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 ...
1
vote
0answers
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 (...
1
vote
1answer
31 views

Applying functions to members of members… of lists of lists…, etc

Suppose that I have a list of depth $k$, and I want to apply a function $f$ to all elements at level $n$ - e.g. $f,\{\{\{a,b\},\{b,c\}\},\{\{c,d\}\}\},3\mapsto\{\{\{f(a),f(b)\},\{f(b),f(c)\}\},\{\{f(c)...
1
vote
1answer
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. ...
4
votes
1answer
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 ...
3
votes
1answer
95 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 ...
5
votes
2answers
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 ...
4
votes
2answers
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 ...
1
vote
1answer
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: ...
1
vote
1answer
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.? ...
0
votes
0answers
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 ...
1
vote
1answer
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_] ...
1
vote
1answer
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 ...
0
votes
1answer
39 views

How to define a function through so that it has an integral of a fixed value?

I have a quite complex function of two-variables, let's say $\alpha(a,b)$ that has a parameter $c$ and I want to set this $c$ so that $\int_{-\infty}^{\infty} \alpha(a,b) \: \mathrm{d}a \: \mathrm{d}b ...
0
votes
2answers
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 ...
1
vote
0answers
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 ...
0
votes
0answers
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 ...
1
vote
1answer
44 views
2
votes
1answer
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 ...
0
votes
2answers
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 ...
3
votes
1answer
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 ...
2
votes
1answer
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: ...
4
votes
1answer
109 views

Right syntax to launch multiple kernels on SLURM cluster

I have access to a SLURM based cluster where I allocate 2 nodes, each with 12 processors. I am looking for a way to launch all 24 kernels to utilize the resources to the maximum for parallel ...
1
vote
1answer
35 views

define function as a result of gradient returns unexpected result

This is probably very simple problem, but what am i doing wrong? c[x_, y_] := x*y; Grad[c[x, y], {x, y}] d[x_, y_] := Grad[c[x, y], {x, y}]; d[1, 1] Returns: <...

1
2 3 4 5
36