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
2
votes
1answer
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 <...
4
votes
1answer
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 ...
8
votes
1answer
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 ...
47
votes
5answers
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 ...
2
votes
1answer
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 ...
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. ...
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: ...
6
votes
1answer
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 ...
3
votes
2answers
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]} ...
11
votes
1answer
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 ...
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 ...
1
vote
0answers
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 ...
2
votes
1answer
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 ...
7
votes
3answers
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 ...
18
votes
1answer
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 ...
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 ...
6
votes
4answers
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 ...
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: ...
0
votes
1answer
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: ...
0
votes
1answer
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 ...
14
votes
4answers
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: ...
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 ...
14
votes
3answers
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 ...
1
vote
2answers
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 ...
2
votes
1answer
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 ...
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 ...
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
66 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
2answers
600 views

Polynomial expansion of operator

I am new to Mathematica, I am trying to generate the polynomial function of a operator. So for example, the operator $L $ is $\frac{\partial f}{\partial x}+\frac{\partial f}{\partial y} $, and I want ...
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)...
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
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 ...
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
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.? ...
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: ...
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
2answers
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 ...
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 ...
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 ...
5
votes
2answers
98 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 ...

1
2 3 4 5
36