# Matlab - I am having problems with iteration (loops)

1. Jan 11, 2007

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)
end

plot(1:N,x_i+1)

2. Jan 12, 2007

### trajan22

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
3. Jan 12, 2007

### marcusl

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));
end
plot(1:N,x)

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.

4. Jan 14, 2007

marcusl;

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.

5. Jan 15, 2007

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)

6. Jan 15, 2007

### J77

yeah - just change the function line.

plot(1:N,x) will work fine, as will simply plot(x)

7. Jan 15, 2007

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));
end
plot(1:N,x)

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

Thanks guys, I apprecaite your help

8. Jan 16, 2007

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);
end

plot(x)

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.

9. Jan 17, 2007

### J77

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.

10. Jan 17, 2007

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);
end

plot(x)

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?

11. Jan 17, 2007

### J77

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

12. Jan 17, 2007

### neurocomp2003

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.

13. Jan 17, 2007

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.

14. Jan 17, 2007

### neurocomp2003

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.

15. Jan 17, 2007

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)
x2=...
x3=...
x4=...
x5=...
x6=...

and so on?

16. Jan 17, 2007

Nevermind...I see it...the more i fool arond with this...the better i get.

17. Jan 17, 2007

### neurocomp2003

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)
"x=function(...)"

18. Jan 17, 2007

Oh...

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

blah
blah
blah
Blah
blah
blah

And now i am satisfied :)

19. Jan 18, 2007

### J77

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:

x(2)=1/sqrt(3);

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.

20. Dec 6, 2010

### mikaili

I have also found a silly problem with Matlab loop
try the following simple program :
clear all
for s=0:0.1:1
V(10*s+1)=s;
end
it will get into trouble when s reaches 0.6

would any body please check and let me know the result