Finding quotient using while loop

  • Thread starter Thread starter DryRun
  • Start date Start date
  • Tags Tags
    Loop quotient
Click For Summary

Discussion Overview

The discussion revolves around a programming problem in MATLAB involving the iterative division of the number ##\pi## by 2 until the quotient is less than or equal to 0.01. Participants explore various coding approaches, troubleshoot errors, and refine their scripts to achieve the desired output.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Mathematical reasoning

Main Points Raised

  • One participant presents an initial script that attempts to calculate the largest quotient greater than 0.01 but encounters an "Out of memory" error due to the placement of the quotient calculation inside the loop.
  • Another participant suggests moving the quotient calculation outside the loop to avoid infinite processing.
  • A revised script is shared, but it still leads to MATLAB crashing, prompting a discussion about the necessity of a while loop and the structure of the code.
  • One participant identifies that the loop does not terminate because the quotient is reset to ##\pi/2## at the start of each iteration, leading to a similar issue in the revised loop.
  • A later version of the code correctly divides the quotient by 2 within the loop, but the participant struggles with displaying the output correctly using the `disp` function.
  • Another participant provides insight into the `disp` function, suggesting that the output needs to be formatted correctly to concatenate strings and numbers.

Areas of Agreement / Disagreement

Participants express varying opinions on the structure of the code and the use of loops, with some agreeing on the need for a while loop while others suggest alternative approaches. The discussion remains unresolved regarding the best method to display the final output.

Contextual Notes

Participants mention issues related to memory errors and the need for proper formatting in MATLAB, indicating potential limitations in their understanding of MATLAB functions and coding practices.

Who May Find This Useful

This discussion may be useful for individuals learning MATLAB programming, particularly those interested in iterative processes and output formatting in coding.

DryRun
Gold Member
Messages
837
Reaction score
4
Suppose that the number ##\pi## is divided by 2. The resulting quotient is divided by 2 again. This process is continued till the current quotient is less than or equal to 0.01. What is the largest quotient that is greater than 0.01?

Here is my attempt:

Code:
r = 1;
quo = 1;

while  (quo  >  0.01)  
 
quo  =  pi / 2;  
num(r) = quo;
r = r+1;
end 

disp('The largest quotient greater than 0.01 is: ' ,(num(r-1)) )

The processing takes about 20 or 30 seconds and then i get this error in the command window:

Out of memory. Type HELP MEMORY for your options.

Error in piquo (line 7)
num(r) = quo;
 
Physics news on Phys.org
sharks said:
Suppose that the number ##\pi## is divided by 2. The resulting quotient is divided by 2 again. This process is continued till the current quotient is less than or equal to 0.01. What is the largest quotient that is greater than 0.01?

Here is my attempt:

Code:
r = 1;
quo = 1;

while  (quo  >  0.01)  
 
quo  =  pi / 2;  
num(r) = quo;
r = r+1;
end 

disp('The largest quotient greater than 0.01 is: ' ,(num(r-1)) )

The processing takes about 20 or 30 seconds and then i get this error in the command window:

Out of memory. Type HELP MEMORY for your options.

Error in piquo (line 7)
num(r) = quo;

It might be better to take the quo=pi/2 statement outside of the while loop ...
 
Here is the modified script:

Code:
r = 1;
quo = 1;

while  (quo  >  0.01)  

num(r) = quo;
r = r+1;
end 

quo  =  pi / 2;  

disp('The largest quotient greater than 0.01 is: ' ,(num(r-1)) )

But Matlab just keeps processing, and then crashes.

Maybe you meant to use a nested while loop? I don't know how that would work.

I'm quite sure that i need to use a while loop in this problem, as i don't know how many iterations are required for the quotient to become less than 0.01. num(r) is an array that represents the index which contains the corresponding quotient, based on the number of times r that the while loop is executed.
 
sharks said:
Here is the modified script:

Code:
r = 1;
quo = 1;

while  (quo  >  0.01)  

num(r) = quo;
r = r+1;
end 

quo  =  pi / 2;  

disp('The largest quotient greater than 0.01 is: ' ,(num(r-1)) )

But Matlab just keeps processing, and then crashes.

Maybe you meant to use a nested while loop? I don't know how that would work.

I'm quite sure that i need to use a while loop in this problem, as i don't know how many iterations are required for the quotient to become less than 0.01. num(r) is an array that represents the index which contains the corresponding quotient, based on the number of times r that the while loop is executed.

No, what I meant was your loop kept setting quo to pi/2 as its first action, hence why it didn't terminate. You have a similar problem with your revised loop in that you never change quo.

Have a look at the attached Mathcad example of what I think you want to achieve.
 

Attachments

  • phys - 12 06 15 iteration 01.jpg
    phys - 12 06 15 iteration 01.jpg
    6.3 KB · Views: 625
Here is the revised code:

Code:
r = 1;
quo = pi;

while  (quo  >  0.01)  

quo  =  quo / 2;  
num(r) = quo;
r = r+1;
end 

disp(num(r-2))

I believe it's correct, as i get the answer: 0.0123

However, i cannot display the line: The largest quotient greater than 0.01 is: 0.0123

I tried this format but it didn't work: disp('The largest quotient greater than 0.01 is: ' ,num(r-2) )
 
sharks said:
Here is the revised code:

Code:
r = 1;
quo = pi;

while  (quo  >  0.01)  

quo  =  quo / 2;  
num(r) = quo;
r = r+1;
end 

disp(num(r-2))

I believe it's correct, as i get the answer: 0.0123

However, i cannot display the line: The largest quotient greater than 0.01 is: 0.0123

I tried this format but it didn't work: disp('The largest quotient greater than 0.01 is: ' ,num(r-2) )

I'm not a Matlab user, so don't take what I say as Gospel, but I don't think disp works like that. According to the on-line Help, you've got convert the number to a string and, for example, concatenate it with your string. See http://www.mathworks.co.uk/help/techdoc/ref/disp.html for details.

PS. The code looks better now and the result seems OK.
 
OK, thanks for your help, NemoReally.
 
sharks said:
OK, thanks for your help, NemoReally.
No worries.
 

Similar threads

  • · Replies 3 ·
Replies
3
Views
4K
  • · Replies 3 ·
Replies
3
Views
1K
  • · Replies 12 ·
Replies
12
Views
5K
  • · Replies 4 ·
Replies
4
Views
7K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 21 ·
Replies
21
Views
2K
Replies
4
Views
2K
  • · Replies 15 ·
Replies
15
Views
1K
  • · Replies 3 ·
Replies
3
Views
3K