Results 1 to 2 of 2

Math Help - Matlab If Statements

  1. #1
    Member Maccaman's Avatar
    Joined
    Sep 2008
    Posts
    85

    Matlab If Statements

    Hi All,
    I'm running a population model in Matlab (using an mfile) and Im calculating populations due to certain external conditions using a few for loops. Anyway, if the population reaches 25000 the species will collapse (i.e. become extinct no matter how the external conditions change), and I want my for loop to stop if that population reaches 25000 or less, but I dont know how.

    Here is my code:

    rainarray = [100,38,100,104, 167,167,165,79,91,77,134,192,235,159,211,257,204,3 00,187.84,99,163,97,228,208,83,44,99,163,97,228,20 8,83,44,112,191,202,137,150,158,20];
    R = 1.1323;
    n0=200000; % Defines the initial population as 200,000
    n = zeros(1,100); % Preallocates space
    t=1:100; % Sets time intervals i.e. every year for 100 years
    h=0.2
    for i = 2:100
    z= randperm(numel(rainarray)); % Generates random arrangement of indices of rainarray
    rain = rainarray(z(1:1)); % Selects one random rainfall figure from rainarray
    K = 5532*rain; % Defines the Carrying Capacity
    n(:,1) = n0; % Sets the initial population as n0 (see above)
    n(:,i) = (R*n(:, i-1)*K)/(K+(R-1)*n(:,i-1))*(1-h); % Beverton-Holt model for a loop with harvest rate
    if n(:,i) <= 25000 % Sets lower threshold
    THIS IS WHERE I NEED SOME CODE SO THE FOR LOOP (i.e. for i = 2:100) COMPLETELY STOPS AND DOESN"T KEEP CALCULATING THE OTHER
    POPULATION LEVELS
    end

    figure(1)
    plot(t,n,'b');
    end



    Does anyone know such a command to end that loop at that point when n becomes <= 25000?

    Thanks in advance for any help
    Follow Math Help Forum on Facebook and Google+

  2. #2
    Grand Panjandrum
    Joined
    Nov 2005
    From
    someplace
    Posts
    14,972
    Thanks
    4
    Quote Originally Posted by Maccaman View Post
    Hi All,
    I'm running a population model in Matlab (using an mfile) and Im calculating populations due to certain external conditions using a few for loops. Anyway, if the population reaches 25000 the species will collapse (i.e. become extinct no matter how the external conditions change), and I want my for loop to stop if that population reaches 25000 or less, but I dont know how.

    :
    :
    :

    Does anyone know such a command to end that loop at that point when n becomes <= 25000?

    Thanks in advance for any help
    help break

    CB
    Follow Math Help Forum on Facebook and Google+

Similar Math Help Forum Discussions

  1. if then statements
    Posted in the Discrete Math Forum
    Replies: 3
    Last Post: October 24th 2010, 11:13 AM
  2. Matlab Split vectors,if statements, and "for loop"
    Posted in the Math Software Forum
    Replies: 5
    Last Post: February 27th 2010, 03:30 AM
  3. Statements
    Posted in the Discrete Math Forum
    Replies: 2
    Last Post: October 23rd 2009, 10:48 AM
  4. Loop Statements
    Posted in the Math Topics Forum
    Replies: 3
    Last Post: April 9th 2009, 06:29 AM
  5. T or F statements
    Posted in the Discrete Math Forum
    Replies: 9
    Last Post: March 29th 2009, 04:36 PM

Search Tags


/mathhelpforum @mathhelpforum