$f_1(x)=1.2x(1-x), f_2(x)=2.8x(1-x)$ on $\mathbb R$, Now from a starting point $x_0=0.25$,an the iterated function system $x_{n+1}=f_{\omega_n}(x_n), n=0,1,2,\dots$ where $\omega_n$ are i.i.d discrete random variable taking values in $\{1,2\}$. Could anyone help me to create a Matlab code for this system to generate the random points $x_n$?
I was trying this assuming probability of appearing the first function is $0.65$ and for the second function is $0.35$.
x0=0.25;
r_weights= [0.65; 1];
f1=@(x) 1.2* x*(1-x);
f2=@(x) 2.8*x*(1-x);
x = zeros(n,1);
y = zeros(n,1);
n=500000;
for i=1:n-1
r = rand;
choice = find(cumweight >r, 1 );
switch (choice)
case 1
f1(i+1)=f1(x0);
case 2
f2(i+1)=f2(x0);
end
end
I didn't try running your code or the corrections I'm proposing below, but the main points are:
1- You have defined function handles, but I would suggest defining functions separately for readability and modularity.
2- Use
xn(or similar) instead ofx0for your looping variable.