Debugging an Infinite Loop in MATLAB

Join the discussion
Ask a follow-up here, or get your own question answered by working scientists, mathematicians and engineers — people, not an autocomplete.
Real named experts · corrections over time · the nuance an AI answer skips
1 reply · 3K views
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