Matlab code for iterated function system

522 Views Asked by At

$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 
1

There are 1 best solutions below

4
On BEST ANSWER

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 of x0 for your looping variable.

xn = 0.25; 
r_weights = [0.65; 1]; 
n=500000;    
x = zeros(n,1); 
y = zeros(n,1);

for i=1:n-1
    r = rand;
    choice = find(r_weights >r, 1 );
    switch (choice)
        case 1
            x(i+1)=f1(xn);
        case 2 
            x(i+1)=f2(xn);

    end
 xn = x(i+1)
 end


function [y] = f1(x)
y= 1.2* x*(1-x); 
end

function [y] = f2(x)
y= 2.8*x*(1-x);
end