2
$\begingroup$

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

enter image description here

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: enter image description here

$\endgroup$
5
  • $\begingroup$ Ummm.... $a$ and $b$ or $\alpha$ and $\beta$??? $\endgroup$ Commented Nov 14, 2023 at 2:26
  • 1
    $\begingroup$ 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}}] $\endgroup$
    – Syed
    Commented Nov 14, 2023 at 2:33
  • $\begingroup$ 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. $\endgroup$
    – Domen
    Commented Nov 14, 2023 at 11:31
  • 1
    $\begingroup$ 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] $\endgroup$
    – Domen
    Commented Nov 14, 2023 at 11:40
  • $\begingroup$ 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 $\endgroup$
    – juv95
    Commented Nov 14, 2023 at 18:14

2 Answers 2

6
$\begingroup$

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

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:

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

We can test this first by increasing x:

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

enter image description here

Or by decreasing x:

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

enter image description here

$\endgroup$
0
$\begingroup$
y[x_]:= Which[x >= b, 1, x <= a, 0, a < x < b, k ]
$\endgroup$
1
  • 1
    $\begingroup$ 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) ... $\endgroup$
    – Domen
    Commented Nov 14, 2023 at 11:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.