Conducting a while-loop in Matlab with arrays

I've got the following exercise:

In this exercise we are going to use a very simple model of the earth (land only) on which grass grows. The net rate of change in the fraction of the area of the earth covered by grass (A) is given by: dA/dt = A((1-A).G-D), where D is the death rate (a constant of 0.1 per 10 millon years). The growth rate of the grass is 0.4 per 10 millon years (G). Discretise this equation. Use the discretised equation to calculate A as a function of time. Every time step in the program corresponds to a period of 10 millon years. Run the model for 200 time units (is 2 billion years). Use a starting value for A of 0.001. Write to the screen the time at which the growth stabilizes (here defined as that the change over one timestep becomes smaller than 1% of the difference between A at the time under consideration compared to the initial value of A.

Now i got this script:

clear all

%Define variables

D=0.1;
G=0.4;
A=0.001;
dt=10E6; %timestep
timevector=[];
grassvector=[];
startloop=1;
endloop=200;

%Define the loop

for t=startloop:endloop
    A=A.*((((1-A).*G)-D)) + A;   
    grassvector(t)=A;
    timevector(t)=t*dt;
end

plot(timevector, grassvector)

This far, it seems to work fine. But I can't figure out the second part of the question. I thought it could be done with a while loop but Matlab keeps geving me errors.

clear all

D=0.1;
G=0.4;
A=0.001;
dt=10E6;
t=0;
timevector=[];
grassvector=[];

while A(t+1)-A(t) > 0.01(A(t)-A)
    t=(t+1)*dt;
    A=A.*((((1-A).*G)-D)) + A;  
    grassvector(t)=A;
    timevector(t)=t*dt;
end

Can someone help? Thanks!

Answers


I cannot exactly figure out what you are doing, but you might want something like this:

D=0.1;
G=0.4;
A=0.001;
dt=10E-6;             % should be a small value
t=0;                  % initial zero time, cannot be used as index in matlab
steps = 100;          % say you want to calculate up to 100 iterations
timevector=zeros(1,steps);
grassvector=zeros(1,steps);    
timevector(1,1) = t;  % initialize the vectors with initial values
grassvector(1,1) = A;
ii = 1;

while (abs(grassvector(1,ii+1) - grassvector(1,ii)) > 0.01 * (grassvector(1,ii))) && (ii < steps-1)
    t = (t+1)*dt;
    grassvector(1,ii+1) = grassvector(1,ii) * ((1-grassvector(1,ii))*G - D) + grassvector(1,ii);
    timevector(1,ii+1) = t*dt;
    ii = ii + 1;
end

In the condition of the while loop I guess you want to check the delta(A) to be more than a small value. Also you have to check to repeat up to the specified number of steps. Otherwise, you should handle the memory management in another way. Inside the loop also you can get rid of constant values of A and t and play directly with the vectors.


Need Your Help

Set TextView to display integers that change for a certain length of time

android textview

I would like to add a TextView to my layout that holds an integer value and continues to change it's value for some length of time. For example, the TextView could change its value for 10 seconds and

How to bind data from Controler to chartjs line chart to create it as dynamic?

javascript json asp.net-mvc-4 chart.js

i am using chart.js to create chart from the data i recive using linq query in my controler and return those values as json but so far i have tried this and isnt getting those values of the json, H...