[solved] while/for statement — what's wrong in a code?

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

[solved] while/for statement — what's wrong in a code?

alhaim
This post was updated on .
0 down vote favorite
       

Hi everybody. This is my Octave code

    for K= 1:10
    while ( p < 1 )
        ceil(log2(K))  +  1/(1-(1-p)^K) %function
        p = p + sens;
        K
    endwhile;
 endfor

 K

and here is an output:

ans =  10.000
K =  1
ans =  5.0000
K =  1
ans =  3.3333
K =  1
ans =  2.5000
K =  1
ans =  2
K =  1
ans =  1.6667
K =  1
ans =  1.4286
K =  1
ans =  1.2500
K =  1
ans =  1.1111
K =  1
ans =  1
K =  1
K =  10

So, as you can see -- in inner while statement value of K is fixed to 1. What I am supposed to do to vary this value between 1 and 10. Why it is not working? I have no idea why this inner while statement is proceed only once?
Reply | Threaded
Open this post in threaded view
|

Re: while/for statement — what's wrong in a code?

Liam Groener-2

On Apr 30, 2011, at 2:53 AM, alhaim wrote:

>
> Hi everybody. This is my Octave code
>
>    for K= 1:10
>    while ( p < 1 )
>        ceil(log2(K))  +  1/(1-(1-p)^K) %function
>        p = p + sens;
>        K
>    endwhile;
> endfor
>
> K
>
> and here is an output:
>
> ans =  10.000
> K =  1
> ans =  5.0000
> K =  1
> ans =  3.3333
> K =  1
> ans =  2.5000
> K =  1
> ans =  2
> K =  1
> ans =  1.6667
> K =  1
> ans =  1.4286
> K =  1
> ans =  1.2500
> K =  1
> ans =  1.1111
> K =  1
> ans =  1
> K =  1
> K =  10
>
> So, as you can see -- in inner while statement value of K is fixed to 1.
> What I am supposed to do to vary this value between 1 and 10. Why it is not


Before the start of the for loop initialize p to 0 or whatever? The way you've written it, p will always be greater than 1 for all K's above 1.
_______________________________________________
Help-octave mailing list
[hidden email]
https://mailman.cae.wisc.edu/listinfo/help-octave
Reply | Threaded
Open this post in threaded view
|

Re: while/for statement — what's wrong in a code?

Liam Groener-2

On Apr 30, 2011, at 3:15 AM, Liam Groener wrote:

>
> Before the start of the for loop initialize p to 0 or whatever? The way you've written it, p will always be greater than 1 for all K's above 1.
Oop's, that should be initialize p before the while loop, not the for loop.
_______________________________________________
Help-octave mailing list
[hidden email]
https://mailman.cae.wisc.edu/listinfo/help-octave
Reply | Threaded
Open this post in threaded view
|

Re: while/for statement — what's wrong in a code?

alhaim
In reply to this post by Liam Groener-2
I should have mention it before -- p is set to 0.01 at the beginning.

The whole code goes like this:


sens = 0.1;
p = 0.1;

%K = 1;

%ceil(log_2 K)+ 1/[1-(1-p)^K]
for K= 1:10
        while ( p < 1 )
                ceil(log2(K))  +  1/(1-(1-p)^K)
  p = p + sens;
  K
  endwhile;
 endfor
 
 K
Reply | Threaded
Open this post in threaded view
|

Re: while/for statement — what's wrong in a code?

Liam Groener-2
Sent from my iPad
On Apr 30, 2011, at 3:21 AM, alhaim <[hidden email]> wrote:

> I should have mention it before -- p is set to 0.01 at the beginning.
>
> The whole code goes like this:
>
>
> sens = 0.1;    
> p = 0.1;    
>
> %K = 1;    
>
> %ceil(log_2 K)+ 1/[1-(1-p)^K]
> for K= 1:10
>    while ( p < 1 )
>        ceil(log2(K))  +  1/(1-(1-p)^K)
>        p = p + sens;
>        K
>    endwhile;
> endfor
>
> K--
> View this message in context: http://octave.1599824.n4.nabble.com/while-for-statement-what-s-wrong-in-a-code-tp3485616p3485642.html
> Sent from the Octave - General mailing list archive at Nabble.com.
> _______________________________________________
> Help-octave mailing list
> [hidden email]
> https://mailman.cae.wisc.edu/listinfo/help-octave
Yep, but the p=0.1 should be the first statement in the for loop, just before the while statement. Otherwise p will always be greater than 1 for K greater than 1.
_______________________________________________
Help-octave mailing list
[hidden email]
https://mailman.cae.wisc.edu/listinfo/help-octave
Reply | Threaded
Open this post in threaded view
|

Re: while/for statement — what's wrong in a code?

alhaim
You're right. Thanks.