1
$\begingroup$

I am trying to plot a piecewise function that I can define recursively, where the nodes are endogenous as well. Basically, $f(l)=a^{t}$ when $l \in \left( \frac{\mu}{\alpha^{t-1}(1-\alpha)+\mu(1-\alpha^{t-1})};\frac{\mu}{\alpha^{t}(1-\alpha)+\mu(1-\alpha^{t})} \right]$. $t=1,2,...N$, both $\alpha$ and $\mu$ $\in (0,1)$ and I need to plot this for $l \in \left[\frac{\mu}{1-\alpha},1\right)$

I can of course write it down manually bit by bit and then assign value t=1, but I would like the program to do it for me, for every t=1,2,3... so that I can plot everything for l going to 1. Is there a way? Thanks a lot in advance!

a = 0.3;
mu = 0.2;
t = 1;
f[l_] = Piecewise[{{1, 
     l <= mu/(a^(t - 1) (1 - a) + mu (1 - a^(t - 1)))}, {a^t, 
     mu/(a^(t - 1) (1 - a) + mu (1 - a^(t - 1))) < l <= mu/(
      a^t (1 - a) + mu (1 - a^t))}, {a^(t + 1), 
     mu/(a^t (1 - a) + mu (1 - a^t)) < l <= mu/(
      a^(t + 1) (1 - a) + mu (1 - a^(t + 1)))}, {a^(t + 2), 
     mu/(a^(t + 1) (1 - a) + mu (1 - a^(t + 1))) < l <= mu/(
      a^(t + 2) (1 - a) + mu (1 - a^(t + 2)))}, {a^(t + 3), 
     mu/(a^(t + 2) (1 - a) + mu (1 - a^(t + 2))) < l <= mu/(
      a^(t + 3) (1 - a) + mu (1 - a^(t + 3)))}}];
Plot[f[l], {l, mu/(1-a), mu/(a^(t + 3) (1 - a) + mu (1 - a^(t + 3)))}, 
 AxesLabel -> Automatic]
$\endgroup$
1
$\begingroup$

You can write it like this:

Lhi[i_, a_, mu_, t_] := mu/((1-a)a^(-1+t+i)+(1-a^(-1+t+i))mu)

pw[l_, a_, mu_, t_, n_] := 
 Piecewise[
  MapIndexed[
   If[First[#2]==1, {1,l<=#1[[2]]},{a^(First[#2]-2+t),#1[[1]]<l<=#1[[2]]}]&, 
   Partition[Table[Lhi[i,a,mu,t], {i,-1,n}],2,1]]
  ]

With[{a = 0.3, mu = 0.2, t = 1, n = 4},
 Plot[pw[l, a, mu, t, n], {l, mu/(1 - a), 
   mu/(a^(t+3)(1-a) + mu(1-a^(t+3)))}, 
  AxesLabel -> Automatic]
 ]
$\endgroup$
1
  • $\begingroup$ This works perfectly! Thanks really really a lot! $\endgroup$
    – Federico
    Aug 17 '20 at 16:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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