Introduction to Computational Science (1999/2000)
CZ1103, Solutions to Tutorial II

1. Write down a simple three-species ecological model (in terms differential equations describing their population) that exhibits the following features:

Are there equilibrium solutions for the model? Write Matlab codes for solving the equations.

Ans:

Let the populations of A, B, C be PA, PB, and PC respectively. The simple ecological model can be described by the following equations

        d PA/dt = -dAPA+bAPAPB

        d PB/dt = -dBPAPB+bBPBPC

        d PC/dt = -dCPBPC+bCPC

To obtain equilibrium we must have d PA/dt=d PB/dt=d PC/dt=0. This is only possible if

        PB =dA/bA =bC/dC and dBPA = bBPC.

For arbitrary set of parameters (dA, bA, etc.), this is not possible.

To solve these equations using Matlab

        function dP = dp3(t,P)

        % assign parameter values

        dA=1.0;bA=1.5;dB=1.25; bB=1.0; dC=0.5; bC=0.1;
        dP = [-dA*P(1)+bA*P(1)*P(2);-dB*P(1)*P(2)+bB*P(2)*P(3);...
            -dC*P(2)*P(3)+bC*P(3)];

and call it by

        P0 = [1.0, 1.0, 1.0]; %initial population
        tspan = [0 10];
        [t, P] = ode45('dp3', tspan, P0);
        plot(t, P(:,1),'--', t, P(:,2),'+', t, P(:,3),'o')

2. Consider Logistic Map:  Show that a)  is a fixed point of the map, and b) when r>3, there is a 2-cycle solution, where x takes values  and  alternately.

Ans.:

        Let  (The map is written as ), we can then show that

         ---  is a fixed point

        

                

Similarly we can show that . Thus we have a 2-cycle solution with x taking values alternating between .

3. The following Matlab codes is used to generate the Mandelbrot fractal

        N_iter=50;
        N=200;
        M_set=zeros(N,N);
        x1=-1.5; x2=1.0;
        y1=-1.0; y2=1.0;
        for m=1:N
            x=x1+(x2-x1)*m/N;
            for n=1:N
                y=y1+(y2-y1)*n/N;
                k=0;
                z0=x+i*y; z=z0;
                while(k<N_iter & abs(z)<2.0)
                      z=z*z+z0;
                      k=k+1;
                 end
                  M_set(m,n)=k;
            end
        end
        image(M_set)
        axis equal

Describe the iteration process used in generating the Mandelbrot fractal.

Ans.:

The basic iteration is done on the complex plane where the complex variable is updated according to

    

For a given point (x, y), the iteration starts with , if the kth iteration is the first iteration when abs(z) is greater than 2, then the corresponding point is labeled by the color with index equal to k (N_iter is the maximum number of iteration used). The map is generated by scanning through the points contained in the rectangle .

4. One-dimensional Random Walk. Write Matlab codes to simulate a one-dimensional random walk and plot the result x(n) vs. n, where x(n) is the position of the walker at nth step.

Ans.:

        N=10000;
        D=1.0;
        p=2*D*rand(N,1)-D;
        walk=cumsum([0; p]);
        plot(0:N, walk)

5. Traffic flow model. Describe a simple model of traffic flow that can be simulated on a computer. For simplicity, use the following assumptions

Ans.:

The following simple model can be simulated on a computer:

  1. The expressway is represented by a one-dimensional lattice labeled from 0, 1, 2,.., to N from left to right. Cars enter at position 0 and leave at position N
  2. Entering the expressway: At each time step, a car enters the expressway with probability p if there is no car at position 0 at the current step. The velocity of the car just entering the expressway is assigned to be zero.
  3. How does velocity of car changes? The velocity of the car takes discrete value: 0, 1, 2, 3, ..., V, where V is the maximum velocity. A car with speed v=s at the current step will move at the next time step with a speed given by the lowest of the following quantities: (i) v=s+1 (acceleration), (ii) v=V (it cannot exceed the maximum velocity), or (iii) v equal to the number of vacant sites to its immediate right (to avoid collision with the next car).
  4. Leaving the expressway: a car will leave the expressway, if there is no car in front of it, and the number of vacant sites to the end of the expressway is less than the velocity of the car.
  5. Now this model is well-defined and a computer program can be written for simulating this model. There are active research being done by using various extended versions of this simple model.