Debugging an Infinite Loop in MATLAB

AI Thread Summary
The discussion focuses on debugging an infinite loop in MATLAB related to calculating sound pressure level (SPL). The user is attempting to write a while loop that increases effective pressure (p) by a factor of 2 until SPL exceeds 100 dB, but their code causes MATLAB to crash due to an infinite loop. The issue arises because the SPL calculation is incorrectly set as a constant rather than updating with each iteration. A corrected version of the code is provided, emphasizing the need to use the logarithm base 10 for SPL calculations. Properly implementing the loop will allow the program to function without crashing.
balogun
Messages
14
Reaction score
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
 
Physics news on Phys.org
balogun said:

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
 
Back
Top