Debugging an Infinite Loop in MATLAB

Click For Summary
SUMMARY

The discussion focuses on debugging an infinite loop in MATLAB related to calculating sound pressure level (SPL) using a while loop. The original code incorrectly sets the SPL variable as a constant, preventing it from updating during each iteration. The correct implementation requires using the logarithm base 10 function, specifically log10, to compute SPL dynamically based on the pressure variable p. The corrected code snippet is provided, ensuring that SPL is recalculated within the loop.

PREREQUISITES
  • Understanding of MATLAB programming syntax and structure
  • Familiarity with logarithmic functions, specifically log10
  • Knowledge of sound pressure level (SPL) calculations
  • Basic concepts of while loops and control flow in programming
NEXT STEPS
  • Review MATLAB documentation on while loops and control structures
  • Study logarithmic functions in MATLAB, focusing on log10 usage
  • Explore sound pressure level calculations and their applications in acoustics
  • Practice debugging techniques in MATLAB to identify and resolve infinite loops
USEFUL FOR

This discussion is beneficial for MATLAB programmers, acoustics engineers, and students learning about sound pressure level calculations and debugging techniques in programming.

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
 

Similar threads

Replies
2
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 1 ·
Replies
1
Views
7K
  • · Replies 9 ·
Replies
9
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 8 ·
Replies
8
Views
2K
Replies
5
Views
4K