Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Matlab - I am having problems with iteration (loops)

  1. Jan 11, 2007 #1
    1. The problem statement, all variables and given/known data

    Hi. I sort of doubt that anyone will reply to this post since it is a matlab problem, but ill give it a shot.

    I am trying to write a matlab function that will graph the logistic map.

    2. Relevant equations

    x(n+1) = r*xn*(1-xn) (1)

    The inputs are r, x0 and N.

    I want the function to store the sequence x1, x2, . . . , xN given by (1) as the components of a vector x and plots them with respect to n.

    3. The attempt at a solution

    function [x_i+1]=logistic_map(r,x0,N)
    % [x]=logistic_map(r,x0,N)
    % the initial condition is x0
    % x depends on scalars r, x0, N
    % the function computes xn+1 = r*xn(1-xn)

    for i = 1:N
    x_i+1 = r*x0*(1-x0)

  2. jcsd
  3. Jan 12, 2007 #2
    where is (1), and what exactly is the program not doing that you want it to? I have some matlab experience so i might be able to help. What are the test numbers you are using?
    One problem i noticed is that you have an operator in the output value you might want to rename the output variable to something more simple. (without +=-/*...) that is the first problem that appears out of it.

    also from what i can see the for loop is Unnecessary because your values are never changing. your graph will just be a line.
    Last edited: Jan 12, 2007
  4. Jan 12, 2007 #3


    User Avatar
    Science Advisor
    Gold Member

    You need to define x as an array variable and then use an index for it. Since your notation is non-standard it's hard for me to determine what you are trying to accomplish; the following is my best guess.

    function [x_i+1]=logistic_map(r,x0,N)
    % [x]=logistic_map(r,x0,N)
    % the initial condition is x0
    % x depends on scalars r, x0, N
    % the function computes x(n+1) = r*x(n)*(1-x(n))

    x = zeros(1,N); % Not necessary but it shows you the dimensionality of x
    x(1) = x0;
    for i = 1:N-1
    x(i+1) = r*x(i)*(1-x(i));

    This is unlikely to be very satisfactory to you because it resembles a factorial function and will overflow any computer for fairly small values of N.

    Also it looks like you have a lot to learn about programming and Matlab syntax. I highly recommend you do the online tutorials, and pick up one of the many books on Matlab for engineering and physics available in college bookstores or Amazon.
  5. Jan 14, 2007 #4

    I tried your code, but it din't seem to work.....

    Ill look at it a bit closer in the morning and see if i could untangle is problem.
  6. Jan 15, 2007 #5
    It would be helpful is you posted the errors you are getting.

    But I would guess the problems are:
    1.) Change xi+1 to x in the function definition line, because you can't use +-/* in variable names for obvious reasons
    2.) change plot(1:N,x) to plot([1:N],x)
  7. Jan 15, 2007 #6


    User Avatar

    yeah - just change the function line.

    plot(1:N,x) will work fine, as will simply plot(x)
  8. Jan 15, 2007 #7
    Thank you LeBrad for your help.
    I should have posted my errors and problems as I went along...i'll make sure I do so in the future.

    Thank you J77 for your help.

    So this is what I had in my editor window:

    function [x]=logistic_map1(r,x0,N)
    % [x]=logistic_map(r,x0,N)
    % the initial condition is x0
    % x depends on scalars r, x0, N
    % the function computes x(n+1) = r*x(n)*(1-x(n))

    x = zeros(1,N); % Not necessary but it shows you the dimensionality of x
    x(1) = x0;
    for i = 1:N-1
    x(i+1) = r*x(i)*(1-x(i));

    Then in my command window:

    I simply defined my variables x0, r and N.

    Then i typed in function [x]=logistic_map1(r,x0,N)

    and got a graph :smile:

    Thanks guys, I apprecaite your help
  9. Jan 16, 2007 #8
    Hi... I am back for more...

    1. The problem statement, all variables and given/known data

    I would like to creat a loop in matlab for a messy equation and plot it.

    2. Relevant equations

    x(n+1) = (6*2^n)*(sqrt(x^2(n) + 1) -1)/(xn) where n = 1,2,...

    (NOTE: x^2n mean the nth x^2)

    I know the above equation is kind of messy...but i just can't seem to get latex to work - so please bear with me.

    the initial condition is x(1) = 1/sqrt(3)

    3. The attempt at a solution

    I followed the outline for what we did above.

    Thus, in my editor window, I had:

    function [p]=circle(x1)

    x(2) = x1;

    for i = 2:10
    p(i+1) = (6*2^i)*(sqrt(x(i)^2+1)-1)/x(i);


    Then in the command window, I would define x1 = sqrt(3)

    and when I ran [p]=circle(x1) , I got an error.

    >> [p]=circle(x1)
    ??? Attempted to access x(3); index out of bounds because numel(x)=2.

    Error in ==> circle at 7
    p(i+1) = (6*2^i)*(sqrt(x(i)^2+1)-1)/x(i);

    Believe me, I can't find anything wrong with my equation? I looked at it a million times.

    Could someone help me please?
  10. Jan 17, 2007 #9


    User Avatar

    Why start at x(2)?

    You need to initialise at x(1).

    Also the output of the map should be x, not p (also in function output it should be x), and the loop should run from 1 to N-1 as in previous example.
  11. Jan 17, 2007 #10

    Thank you.

    It worksmuch more better now, however, I still keep getting an error.

    I made new changes according to what you said:

    function [x]=circle(x1)

    x(2) = x1;

    for i = 1:36
    x(i+1) = (6*2^i)*(sqrt((x(i))^2+1)-1)/x(i);


    I define x1=1/qurt(3) in the command window.

    Thn when I run the programme, I get :

    >> [x]=circle(x1)
    Warning: Divide by zero.
    > In circle at 7
    x =

    0 NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN

    And a blank graph.

    Is their something wrong with my equation?
  12. Jan 17, 2007 #11


    User Avatar

    You still have initialised at x(2) - what you want is x(1)=x1;

    Also, the error comes from x being 0 - the x(1)=x1 should fix this. The only way for this is if x(1)=0 - which gives x(2)=NAN (ie. divide by x(1)=0) - ie. a correct initialisation should fix it :smile:
  13. Jan 17, 2007 #12
    why would you initialise x2 and then start your counter at i=1? your essentially overwriteing x2..
    2 ways to fix that solution...one posted above.

    if you are new to matlab...two things you should at least learn to do ...(1) fprintf(1,...) or disp(...) or leave out the semi-colon.
    like in any programming environment....if you get an error trace your steps on PAPER & PEN...its always your friend.
  14. Jan 17, 2007 #13
    Thank you once again, that helped.

    I initialised at x2 because i didn't think the counter was supposed to include x1. Now i see it does....

    I will take your advice. I allready did a bit of disp(...) and it is a very good tool...i have never heard of fprint(1...) yet.

    Thank you all for taking the time to help me...i appreciate it.
  15. Jan 17, 2007 #14
    matlab is like linux...u can type help functionname...and the function name that i'm speaking of is fprintf...it allows you to format easier than disp(...) and the one without the ";" eg...you can write everything on one line ....
    fprintf("x: %d",x); rather than having it display on multiple lines..though it depends on the of your matrix.
  16. Jan 17, 2007 #15
    That sounds more complicated...but you know more than me. I will definitley invest some time into learning fprint.

    For my last question, is their a way that I can store my results for x in a vector instead of plotting it?

    Or simply just get the resluts of each x(i)...hence

    x1= 1/sqrt(3)

    and so on?
  17. Jan 17, 2007 #16
    Nevermind...I see it...the more i fool arond with this...the better i get.
  18. Jan 17, 2007 #17
    not sure what you mean...but matlab is made to control martices/vectors.... if your talking abotu allocating memory just do x=size(n,m); and it will preallocate this size for you to use. If you do x(i)=x(i-1)....without preallocating matlab will keep rebuilding the size of the matrix which will slow donw your computer.

    lastly if your asking if you can return a vector x from your function (which is probalby what you mean) then yes you can....

    the line "function x=fx(...)" will allow you to do so
    set which ever vector you want inside your function...
    then at the very end just say x = vector.
    However it looks like you were preallocating the x which is better...it will automatically return x.

    when you want x outside this function simply type (say in command prompt)
  19. Jan 17, 2007 #18

    I meant...I wanted to return my function in a row vecotr...

    but yea...like you said...everything in matlab is in vectors....

    I got confused because it kept returning someting like this:

    Colume 1 - 8

    blah blah blah

    Colume 9 - 16

    Blah blah blah

    So i just hit the " ' " after my function and got


    And now i am satisfied :)
  20. Jan 18, 2007 #19


    User Avatar

    Just a follow-up:

    If you don't assign values to the start of an array, Matlab puts them at zero.

    Specifically, when you wrote:


    Matlab created the array [0 1/sqrt(3)].

    Also, you may like to put the command keyboard in your code, when you reach this point you go into debugging mode, ie. you get a command line, plus access to all variables up to that point, you can then check their values to make sure they're as you want them; type return to continue running the code.
  21. Dec 6, 2010 #20
    I have also found a silly problem with Matlab loop
    try the following simple program :
    clear all
    for s=0:0.1:1
    it will get into trouble when s reaches 0.6

    would any body please check and let me know the result
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook