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

  • Thread starter Thread starter rad0786
  • Start date Start date
  • Tags Tags
    Loops Matlab
AI Thread Summary
The discussion revolves around troubleshooting MATLAB code for implementing the logistic map and a separate messy equation. Users initially struggle with variable initialization and proper loop structure, leading to errors such as "index out of bounds" and "divide by zero." Key advice includes ensuring that the first element of the array is initialized correctly and using standard variable names without operators. The conversation also touches on the importance of preallocating arrays for efficiency and debugging techniques like using the "keyboard" command to inspect variable states. Overall, participants share solutions and tips to improve MATLAB coding practices, enhancing their understanding of the language.
rad0786
Messages
187
Reaction score
0

Homework Statement



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.



Homework 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.


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)
 
Physics news on Phys.org
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:
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.
 
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.
 
rad0786 said:
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.

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)
 
yeah - just change the function line.

plot(1:N,x) will work fine, as will simply plot(x)
 
LeBrad said:
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)

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.


J77 said:
yeah - just change the function line.

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


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 :smile:

Thanks guys, I apprecaite your help
 
Hi... I am back for more...


Homework Statement



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



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



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.

Could someone help me please?
 
rad0786 said:

The Attempt at a Solution



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);
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
J77 said:
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.


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
rad0786 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?
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:
 
  • #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.
 
  • #13
J77 said:
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:

Thank you once again, that helped.

neurocomp2003 said:
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.

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
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
neurocomp2003 said:
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.

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
Nevermind...I see it...the more i fool arond with this...the better i get.
 
  • #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)
"x=function(...)"
 
  • #18
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 something 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
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
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 anybody please check and let me know the result
 

Similar threads

Replies
18
Views
4K
Replies
1
Views
2K
Replies
8
Views
2K
Replies
10
Views
3K
Replies
2
Views
3K
Replies
1
Views
2K
Replies
3
Views
4K
Back
Top