1. Limited time only! Sign up for a free 30min personal tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Homework Help: MATLAB looping question

  1. Apr 29, 2010 #1
    Hi guys, sorry if i didn't follow the template but its more of a quick query than proper laid out question, but it is part of my coursework...anyway here goes;

    I'm having a bit of trouble with an m-file in matlab, i posted earlier but i appreciate that post was too vague for a response.

    basically i have an m-file which goes like this

    function [y]=logmap(lam, x)
    %calculate population size, y, next year using:
    %lam: effective growth rate of population (between 0-4)
    x: current size of population
    using the logistic mapping formula

    y=lam*x*(1-x)


    but what I really need is for it to loop, so that y goes back in to it as the x value and then just keeps repeating it.
    does anyone know the commands to make it do this, the "for" and "while" loops don't seem to have the right inputs for the situation, also i need to define a value, n, which is the amount of times i want it to loop.

    can someone please give me a hand?
    thanks
    elliottmarter
     
  2. jcsd
  3. Apr 29, 2010 #2
    Code (Text):
    function [y]=logmap(lam, x)
    %calculate population size, y, next year using:
    %lam: effective growth rate of population (between 0-4)
    x: current size of population
    using the logistic mapping formula

    % Number of loops
    n = 100;


    % Loop n times
    for ii = 1:n
        x = lam*x*(1-x)
    end

    y = x;
    end
    Let me know if this helps
     
    Last edited: Apr 29, 2010
  4. Apr 30, 2010 #3
    this worked fine, but i managed a different solution before i saw your post, thanks all the same BerryBoy :)
     
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook