While loop

  • Thread starter balogun
  • Start date
  • #1
14
0

Homework Statement




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.


Homework Equations





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

Homework Statement





Homework Equations





The Attempt at a Solution

 

Answers and Replies

  • #2
CEL
656
0

Homework Statement




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.


Homework Equations





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

Homework Statement





Homework Equations





The Attempt at a Solution

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
 

Related Threads on While loop

  • Last Post
Replies
2
Views
644
  • Last Post
Replies
7
Views
1K
  • Last Post
Replies
15
Views
5K
  • Last Post
Replies
5
Views
5K
  • Last Post
Replies
2
Views
2K
  • Last Post
Replies
1
Views
2K
Replies
0
Views
686
Replies
5
Views
4K
Replies
4
Views
855
Replies
1
Views
1K
Top