1. Not finding help here? Sign up for a free 30min tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

While loop

  1. Aug 29, 2007 #1
    1. The problem statement, all variables and given/known data


    I am having a bit of a problem trying to understand while loop.

    I was given a question lke this

    Write a while loop to evaluate the spl of effective pressure p starting from pref and increasing by a factor of 2 at a time.The program should stop when the spl exceeds 100 db.The output should clearly show p and spl at each step

    pref=20x10-6 spl=20log10(p/pref)



    But it keeps making matlab crash by giving me an infinite loop.


    2. Relevant equations



    3. The attempt at a solution

    My code was like this

    pref=2/1000000;
    p=pref;
    spl=20*log(10);
    while spl<100;
    spl=20*log(10)*(p/pref)
    p=p^2
    end
    1. The problem statement, all variables and given/known data



    2. Relevant equations



    3. The attempt at a solution
     
  2. jcsd
  3. Aug 29, 2007 #2

    CEL

    User Avatar

    Of course you have an infinite loop. Your stopping parameter [B}spl[/B] is constant and equal to 20 times the natural logarithm of 10, when it should be 20 times the logarithm in the base 10 of p/pref.
    Your code should be:

    pref=2/1000000;
    p=pref;
    spl=20*log10(p/pref);
    while spl<100;
    spl=20*log10(p/pref)
    p=p^2
    end
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?



Similar Discussions: While loop
  1. Matlab while loop help (Replies: 15)

  2. C++- while-loop (Replies: 7)

Loading...