COMPGROUPS.NET | Search | Post Question | Groups | Stream | About | Register

### Iteration question

• Email
• Follow

```Hello I am just beginning to learn Matlab and have a question that most people probably consider simple. the logistic equation x(n+1) = p*x(n)*(1-x(n)) with a value of p=.8 and x(1)=.5

i need to iterate the equation from x(1) to x(50) and store a column vector x which contains all 50 iterations. here was my attempt:

p=.8;
x=.5;
for n=1:49;
x(n+1)=p*x(n)*(1-x(n))
end

i thought that this would create a 50x1 row vector and then i'd transpose it, but the value x remains a 1x1 vector with value .5

any input would be greatly appreciated, thank you!!
```
 0

See related articles to this posting

```"Robert " <RobertStevenson8@yahoo.com> wrote in message <i88n55\$gq0\$1@fred.mathworks.com>...
> Hello I am just beginning to learn Matlab and have a question that most people probably consider simple. the logistic equation x(n+1) = p*x(n)*(1-x(n)) with a value of p=.8 and x(1)=.5
>
> i need to iterate the equation from x(1) to x(50) and store a column vector x which contains all 50 iterations. here was my attempt:
>
> p=.8;
> x=.5;
> for n=1:49;
>      x(n+1)=p*x(n)*(1-x(n))
> end
>
> i thought that this would create a 50x1 row vector and then i'd transpose it, but the value x remains a 1x1 vector with value .5
>
> any input would be greatly appreciated, thank you!!

i got it working, nevermind thanks
```
 0

```Robert, to begin with, what is x(n)? Matlab doesn't know what x(1),x(2),x(3)..x(n) is because x(n) is a function and you have not yet defined it.

"Robert " <RobertStevenson8@yahoo.com> wrote in message <i88n55\$gq0\$1@fred.mathworks.com>...
> Hello I am just beginning to learn Matlab and have a question that most people probably consider simple. the logistic equation x(n+1) = p*x(n)*(1-x(n)) with a value of p=.8 and x(1)=.5
>
> i need to iterate the equation from x(1) to x(50) and store a column vector x which contains all 50 iterations. here was my attempt:
>
> p=.8;
> x=.5;
> for n=1:49;
>      x(n+1)=p*x(n)*(1-x(n))
> end
>
> i thought that this would create a 50x1 row vector and then i'd transpose it, but the value x remains a 1x1 vector with value .5
>
> any input would be greatly appreciated, thank you!!
```
 0

```
"josh " <n.n@hotmail.com> wrote in message
news:i88olu\$nfp\$1@fred.mathworks.com...
> Robert, to begin with, what is x(n)? Matlab doesn't know what
> x(1),x(2),x(3)..x(n) is because x(n) is a function and you have not yet
> defined it.

x(n) is the nth element of the vector x.  Note that on the second line of
code that Robert posted, he defines x to be 0.5.

> "Robert " <RobertStevenson8@yahoo.com> wrote in message
> <i88n55\$gq0\$1@fred.mathworks.com>...
>> Hello I am just beginning to learn Matlab and have a question that most
>> people probably consider simple. the logistic equation x(n+1) =
>> p*x(n)*(1-x(n)) with a value of p=.8 and x(1)=.5
>>
>> i need to iterate the equation from x(1) to x(50) and store a column
>> vector x which contains all 50 iterations. here was my attempt:
>>
>> p=.8;
>> x=.5;
>> for n=1:49;
>>      x(n+1)=p*x(n)*(1-x(n))

You should "preallocate" x before the loop starts, so that you don't grow x
by 1 element each time through the loop.  That requires a lot of memory
shuffling (not so much in this case, where the loop body doesn't execute
very often, but if you tried to run a loop like this from 1 to 1000000 it

You know that x should be 50 elements long when this loop finishes
executing, so just make it 50 elements long (with the first one being 0.5)
before the loop.

numIterations = 50;
p = 0.8;
x = zeros(numIterations, 1);
x(1) = 0.5;
for n = 1:numIterations-1
x(n+1) = p*x(n)*(1-x(n));
end

This also has the benefit that you don't need to transpose your x
afterwards.

--
Steve Lord
slord@mathworks.com
comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ
http://www.mathworks.com

```
 0

3 Replies
249 Views

Similar Articles

12/8/2013 1:41:47 AM
page loaded in 134541 ms. (0)

Similar Artilces:

Question.....?
; from Yahoo, but some ISPs don't do this correctly. Hotmail is bad for that. 4. the recipients email host blocks email from Yahoo. Some places will block the big free email sites from emailing their users. 5. you typed the address wrong (doubtful if it happened multiple times). 6. aliens from another planet have taken over the internet (who knows? ;) ) Sign up for another email account somewhere completely different from Yahoo and see if that address can get through. As for the original question about tracking emails, it's possible IF your recipient can view HTML formatted emails

if / || question
If I had an if statement like this: if (foo() || bar()) { ... Would bar() be executed if foo() returned true? copx wrote: > If I had an if statement like this: > > if (foo() || bar()) { > ... > > Would bar() be executed if foo() returned true? > No. -- Ian Collins. "Ian Collins" <ian-news@hotmail.com> schrieb im Newsbeitrag news:51g3u8F1j9btfU14@mid.individual.net... > copx wrote: >> If I had an if statement like this: >> >> if (foo() || bar()) { >> ... >> >> Would bar() be executed if foo() returned tru

Question...
I'm trying to read a MAT File and assign the content to mxArray in C++. Does anyone know how I can assign the full content of the MAT-File to mxArray? Sample code will be very helpfull. Thanks

??????? Question
What does this mean: global X = []; wher x is some variable....I mean what does [] mean Thansk X is a global variable which is being initialized to an empty 2D array. mary wrote: > What does this mean: > > global X = []; > > wher x is some variable....I mean what does [] mean > > Thansk Praetorian wrote: > > > X is a global variable which is being initialized to an empty 2D Why 2 D array? Duane Hanselman wrote: > mary wrote: >> >> What does this mean: >> >> global X = []; >> >> wher x is some variable....I mean wh

doCmd.goto question
when you use either "docmd.goto" or ".setfocus" to move to a particular field in a form, the field is highlighted in reverse text (black background/white lettering). Instead, I would like the curser to be sitting in the field waiting for input without the highlighting (as if you had just moved the curser into the field by clicking on the field). How can this be done? Achieva, Look in the Help for SelLength, SelStart, SelText When you setfocus the field is selected. To deselect you could add a little code: FocusField.SelLength=0 The setting for FocusField.SelSt

irb question
Hi, Iam starting to learn ruby, today is my first day actually working with terminal and things are going pretty good. I just have a quick question, when I go into irb the book I am reading ("Ruby novice to professional" from apress ) tells me that there should be irb(main):001:0> each line, but for me all I have is >> all the ruby commands work fine. I just don't like not having the line numbers. This is probably very simple but please help a newbie out. Thanks for your time. -- Posted via http://www.ruby-forum.com/. -----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Am 09.10.2010 14:46, schrieb Darren Nickerson: > Hi, > Iam starting to learn ruby, today is my first day actually working with > terminal and things are going pretty good. I just have a quick question, > when I go into irb the book I am reading ("Ruby novice to professional" > from apress ) tells me that there should be irb(main):001:0> each line, > but for me all I have is >> > all the ruby commands work fine. I just don't like not having the line > numbers. This is probably very simple but please help a newbie out. >

A maths question
Not exactly related to graphics, but as graphics require good mathematics understanding I thought I would see if someone here may be able to help. Here is description of my problem. An object is moving in a straight line. The object can thrust to speed up and thrust in reverse to slowdown. The object is travelling between two known points, A & B, distance d apart. +----------------------+ A d B The basic movement is as follows Ft = thrust A = acceleration Vi = Initial velocity Vf = final velocity Cf = friction constant Ff = friction force F = force on object so... Ff = - Vi * Cf F = Ft + Ff = Ft - Vi * Cf A = F / Mass Vf = Vi + A * timeStep My question is : If I know my initial velocity, distance left to go and final velocity, how do I calculate the thrust ? Thanks in advance, Sam sam wrote: > Ff = - Vi * Cf > F = Ft + Ff = Ft - Vi * Cf > A = F / Mass > Vf = Vi + A * timeStep > > My question is : > If I know my initial velocity, distance left to go and final velocity, > how do I calculate the thrust ? By plugging all your equations together and solving for Ft. (Vf - Vi) / timeStep * Mass + Vi * Cf = Ft