Drexel dragonThe Math ForumDonate to the Math Forum



Search All of the Math Forum:

Views expressed in these public forums are not endorsed by Drexel University or The Math Forum.


Math Forum » Discussions » Software » comp.soft-sys.matlab

Topic: Trouble with the while loop
Replies: 2   Last Post: Mar 4, 2013 10:45 AM

Advanced Search

Back to Topic List Back to Topic List Jump to Tree View Jump to Tree View   Messages: [ Previous | Next ]
Steven Lord

Posts: 17,944
Registered: 12/7/04
Re: Trouble with the while loop
Posted: Mar 4, 2013 10:45 AM
  Click to see the message monospaced in plain text Plain Text   Click to reply to this topic Reply



"Ishan " <ishanpatel1992@gmail.com> wrote in message
news:kgu4vo$4t5$1@newscl01ah.mathworks.com...
> Still a Novice at matlab, trying to figure out what's wrong with my while
> loop everytime I run it, matlab crashes. Here's my code:
> omegax=0;omegay=0;omegaz=1;x=0;y=0;z=1; rand(1000);
> omega=[omegax,omegay,omegaz];v=[x,y,z];w=1;alpha=0.9;
> while 0<=z && z<=5; r=-log(rand);
> v=v+r*omega;
> end


I let this run for 10 minutes on my machine and it didn't crash. If by crash
you really mean you received a crash report log and MATLAB disappeared and
you can reproduce that behavior please send the log along with this code to
Technical Support. If by crash you mean simply stops responding that's
because this is an infinite loop as TideMan identified.

Because of the values of your omega* variables, only z is actually updated
by this code. So I'd update this to be:

z = 1;
while 0 <= z && z <= 5
z = z - log(rand);
end

A second look indicates this can be simplified even more. Because RAND
returns a value between 0 and 1 log(rand) will always be negative so you
don't really need to check for 0 <= z; z starts and 1 and can only increase.

z = 1;
while z <= 5
z = z - log(rand);
end

--
Steve Lord
slord@mathworks.com
To contact Technical Support use the Contact Us link on
http://www.mathworks.com




Point your RSS reader here for a feed of the latest messages in this topic.

[Privacy Policy] [Terms of Use]

© Drexel University 1994-2014. All Rights Reserved.
The Math Forum is a research and educational enterprise of the Drexel University School of Education.