Debugging an Infinite Loop in MATLAB

In summary, the code provided for the while loop to evaluate spl of effective pressure p is incorrect as the stopping parameter is constant instead of being dependent on the value of p. The correct code would be to use the logarithm in base 10 for spl and update its value within the while loop.
  • #1
balogun
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
 
Physics news on Phys.org
  • #2
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
 
  • #3



Hello, it seems like your while loop is not incrementing the value of p properly. In each iteration, the value of p should be multiplied by 2 (since the problem asks for an increase by a factor of 2 at each step). Your current code is squaring p in each iteration, which will quickly lead to a very large value of p and cause an infinite loop.

Try modifying your code to:

pref = 2/1000000;
p = pref;
spl = 20*log10(p/pref);

while spl < 100
p = p*2;
spl = 20*log10(p/pref);
end

This should properly increment the value of p and stop the loop once the spl exceeds 100 dB. Also, make sure to include a semicolon at the end of the line where you calculate the value of spl, otherwise it will print out the value at each iteration and make the output messy.

I hope this helps with debugging your code. Keep in mind that while loops can be tricky and it's important to carefully check your code for any potential errors. Good luck with your assignment!
 

Related to Debugging an Infinite Loop in MATLAB

1. How do I know if my code is stuck in an infinite loop?

To determine if your code is stuck in an infinite loop, you can use a built-in function in MATLAB called "isinf". This function checks if a variable is equal to positive or negative infinity. If your code is stuck in an infinite loop, the variable will never reach a finite value and the "isinf" function will return a logical value of "true".

2. What are some common causes of infinite loops in MATLAB?

Infinite loops in MATLAB can be caused by a variety of reasons, such as forgetting to increment a loop counter, using the wrong logical operator in a conditional statement, or using a recursive function without a proper base case. It can also occur when a user accidentally enters an incorrect input, causing the loop to never terminate.

3. How can I break out of an infinite loop in MATLAB?

To break out of an infinite loop in MATLAB, you can use the "Ctrl+C" keyboard shortcut. This will stop the execution of your code and allow you to make any necessary changes to fix the infinite loop. You can also use the "break" statement within a loop to exit the loop when a certain condition is met.

4. Is there a way to prevent infinite loops from occurring in my code?

Yes, there are several ways to prevent infinite loops in MATLAB. One way is to always make sure to include a proper stopping condition in your loops, such as using a counter or checking for a specific value. You can also use the "isinf" function to check for infinite values and incorporate error handling in your code to catch any potential infinite loops.

5. Can I use debugging tools in MATLAB to help identify an infinite loop?

Yes, MATLAB has several built-in debugging tools that can help identify and fix an infinite loop. The "dbstop" function allows you to set breakpoints in your code, which will pause the execution of your code at a specific line. This can help you pinpoint where the infinite loop is occurring and make necessary changes. You can also use the "dbstack" function to view the current state of your program and the "dbclear" function to remove any breakpoints. Additionally, the "debug" mode in MATLAB allows you to step through your code line by line to track the flow of your program and identify any potential infinite loops.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
6K
  • Engineering and Comp Sci Homework Help
Replies
9
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
8
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
2K
  • Programming and Computer Science
Replies
1
Views
1K
Back
Top