0
$\begingroup$

I have the following equation to solve $$a[n+1] = a[n] + 6 - 100\cdot \theta(a[n]-100)$$ where $\theta(x)$ is the Heaviside step function. So I tried the following

RSolve[{a[n + 1] == a[n] + 6 - 100*UnitStep[a[n] - 100], a[0] == 0}, a[n], n]

and it did not work.

Is there a way to solve a recurrent equation with step function in Mathematica?

I use 13.2 version.

$\endgroup$

2 Answers 2

1
$\begingroup$

Using a different approach than using RSolve

Clear["Global`*"]

a[0] = 0;

a[n_] := a[n] = a[n - 1] + 6 - 100*UnitStep[a[n - 1] - 100]

After the initial 0, each block of 50 values are identical sets of three runs

seq = a /@ Range[100];

blocks = Partition[Split[seq, #2 > #1 &], 3]

(* {{{6, 12, 18, 24, 30, 36, 42, 48, 54, 60, 66, 72, 78, 84, 90, 96, 102}, {8, 
   14, 20, 26, 32, 38, 44, 50, 56, 62, 68, 74, 80, 86, 92, 98, 104}, {10, 16, 
   22, 28, 34, 40, 46, 52, 58, 64, 70, 76, 82, 88, 94, 100}}, {{6, 12, 18, 24,
    30, 36, 42, 48, 54, 60, 66, 72, 78, 84, 90, 96, 102}, {8, 14, 20, 26, 32, 
   38, 44, 50, 56, 62, 68, 74, 80, 86, 92, 98, 104}, {10, 16, 22, 28, 34, 40, 
   46, 52, 58, 64, 70, 76, 82, 88, 94, 100}}} *)

SameQ @@ blocks

(* True *)

The Length of the three runs are

Length /@ blocks[[1]]

(* {17, 17, 16} *)

The sequences are

FindSequenceFunction[#, n] & /@ blocks[[1]]

(* {6 n, 2 (1 + 3 n), 2 (2 + 3 n)} *)

The definition of a is then

Clear[a]

a[0] = 0;

a[m_Integer?Positive] = Module[{n = Mod[m - 1, 50] + 1},
  Piecewise[{
     {6 n, n < 18},
     {2 (1 + 3 (n - 17)), n < 35},
     {2 (2 + 3 (n - 34)), n < 51}}] // Simplify]

enter image description here

Verifying,

(a /@ Range[50]) === (a /@ Range[51, 100]) ===
 (a /@ Range[101, 150]) === Flatten[blocks[[1]]]

(* True *)
$\endgroup$
1
  • $\begingroup$ Thank you, this perfectly solves the given equation. But I am wondering, are there some more general ways to do it? Because the next step I had in mind is to solve a system of coupled equations like: a[n + 1] == a[n] + 6*b[n] - 100*UnitStep[a[n] - 100] and b[n + 1] == b[n] + UnitStep[a[n] - 100]. I've tried to modify your solution by defining bunction b[n_] := b[n] =b[n-1]+ UnitStep[a[n - 1] - 100], but, apparently, it does not work this way. $\endgroup$ Feb 14, 2023 at 21:19
0
$\begingroup$

If you are happy with a recursive solution:

a[0] = 0;
a[n_] := a[n] = a[n - 1] + 6 - 100*UnitStep[a[n - 1] - 100];
ListPlot[Table[a[i], {i, 50}]]

enter image description here

$\endgroup$
4
  • $\begingroup$ It does not look like a right solution. I suppose, the solution should follow sawtooth pattern. Wolfram Alpha solution $\endgroup$ Feb 14, 2023 at 20:12
  • $\begingroup$ Sorry, I over looked the a[n] in the UnitStep. IIt is fixed now. $\endgroup$ Feb 14, 2023 at 20:15
  • $\begingroup$ I suppose, you meant sol[n_]:= If[6*n<100,6 n, -2 (-800 + 47 n)]]. But still, the solution is correct for n<=16, but than goes down forever instead of sawtooth. $\endgroup$ Feb 14, 2023 at 21:04
  • $\begingroup$ I think a recursive solution is simpler. I changed my answer. $\endgroup$ Feb 14, 2023 at 21:23

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.