# Can I write a function that describes a hysteron?

Is there a way to write a "function", or as the answer below refers it better as a memory, that has the following output:

k=0 if the last time x was outside the range (a,b), it was x<a. And k=1 if the last time x was outside the range (a,b) it was x>b. I want to increase/decrease x in steps and record the output for each step, but I need to define that function that remembers the history of the input first, and I am not sure how to do it.

Here is a graphical representation of it taken from Wikipedia:

• Ummm.... $a$ and $b$ or $\alpha$ and $\beta$??? Commented Nov 14, 2023 at 2:26
• I am a bit rusty on math but does this pass the vertical line test to be called a function in the first place? If you want to have a state-machine that chooses between two functions, then add more context to the post. e.g. the two functions can be: Plot[{UnitStep[x - 1], UnitStep[x + 1]}, {x, -5, 5}, Exclusions -> None, PlotStyle -> {{Thick, Blue}, {Thick, Red}}]
– Syed
Commented Nov 14, 2023 at 2:33
• Can you write this in Mathematica? Very likely, you can. But the implementation depends on how you want to use this function, so we need more details as already mentioned by @Syed. Commented Nov 14, 2023 at 11:31
• As an example of a primitive implementation: $lastX = -Infinity; {a, b} = {-2, 3}; y[x_] := Module[{y = HeavisideTheta[x - If[$lastX < a, b, a]]}, \$lastX = x; y] Commented Nov 14, 2023 at 11:40
• I should have not called it a function, since in the mathematical sense, I believe this one is not. Calling it a memory as @Domen did is the better term Commented Nov 14, 2023 at 18:14

To define a memory, you can define a variable and, for safety, put it in a separate name space, like e.g. nsk.

Further, you did not specify what k should be if x==a or x==b and what K shloud be the first time. I assume that k==0 for x<=a and k==1 for a>=b and k==0 the first time:

nsk=0;
y[x_] := Module[{k}, k = nsk;
Which[x <= a, nsk = 0, x >= b, nsk = 1, a < x < b, k]]


We can test this first by increasing x:

a = 2; b = 5;
nsk = 0;
d = Table[{x, y[x]}, {x, 0, 10, 0.5}];
ListPlot[d]


Or by decreasing x:

nsk = 0;
d = Table[{x, y[x]}, {x, 10, 0,-0.5}];
ListPlot[d]


y[x_]:= Which[x >= b, 1, x <= a, 0, a < x < b, k ]
`
• If I understand correctly, this is not exactly what the OP wants. They want the output of the function to – somehow – depend on the "previous" inputs (namely, that there is a hysteresis) ... Commented Nov 14, 2023 at 11:30