Skip to main content

Section 15.5 MATLAB while-end Loops

So what are while loops and what do they look like? The generic while-end loop is shown below:
while < conditional expression is true >
< execute code >
< * YOU BETTER MAKE THAT CONDITION FALSE! * >
while Loop Important Details
  • Similar to for loops, while loops start with the keyword while and must be completed with the end keyword.
  • It is vitally important that your while loop conditional expression is made false at some point during the execution of the loop. If it is not made false, you get an infinite loop.
You can think of while loops as over-eager dogs. They will run themselves to death unless you tell them to stop. The way that you tell a while loop to stop is to make sure that at some point in the loop, the initial conditional expression becomes false.
Figure 15.5.1. The while loop is the overenthusiastic sled dog of the loop world. Sled dogs do actually run themselves to death.
The flowchart for the generic while loop is shown below in figure 15.5.2:
Figure 15.5.2. A generic while loop flowchart
The beauty of while loops is that you can ensure that your code repeats itself until a specific condition is met. However, while loops should only be used in instances where you do not already know how many times the loop needs to be run.
Maclaurin Series Example - while Loop Edition
To see an instance where a while loop is necessary, let’s consider the case of the Maclaurin series expansion of \(sin(x)\) one last time. In this case, let’s flip the question a little bit. Let’s say that the new question is, you want to know how many terms of the Maclaurin series are necessary to get an estimation of \(sin(x)\) that is 99% accurate.
We know from the Maclaurin series that if we keep adding terms, the estimation will get more and more accurate. We also know that we need an infinite number of terms to make the answer infinitely accurate. But how many terms are necessary to get our estimation of \(sin(x)\) to be 99% accurate? It will depend on the \(x\) that the user specifies but it actually might not take as many terms as you might think it does.
Here are a few tips to get you started:
  • It is actually easier to use the error rather than accuracy. The formula for percent error is \(\frac{(true-approximate)}{true}*100\text{.}\) So 99% accurate would be less than 1% error.
  • Use the built-in MATLAB function sin() to get a β€œtrue” value for \(sin(x)\text{.}\) Even though it isn’t 100% accurate, it is close enough for our purposes.
  • Since you do not know how many terms it will take, you will need to use a while loop.
Try It!
I am not going to lie, I think that this is a pretty difficult problem. But we are here to work out our brains and difficult problems are our opportunity to show off our strength!
Try to complete the entire process: think, sketch, code, test, and repeat.
  • Think-what is this problem asking for? Do you have anything already completed that can help you complete this?
  • Sketch-Write out a flowchart or pseudocode for this problem and how you would solve it.
  • Code-Give it your best shot! Make sure that you repeat the think and sketch phases as much as you need to!
  • Test- Compare your MATLAB solution to your solution by hand (it won’t take as many terms as you think!). Try with different x values and make sure that it is robust and it works.
  • Repeat - repeat any steps necessary until you can get a working script. I know this is difficult! You can do it!

Checkpoint 15.5.3. What is your flowchart?

For this discussion, write out your pseudocode that you came up with to solve the Maclaurin series β€œnumber of terms” problem described directly above.
Think about how this planning translated into your code. WHat steps(if any) did you miss? Where can you improve in the planning or coding stage?
The code solution to the problem is shown below in example 15.5.4 Make sure that you give it an honest try before looking at the solution though! Do not skip your brain workouts, you will only cheat yourself.

Example 15.5.4. Maclaurin Solution(while loop).

Here is an interactive code box that functions identically to the MATLAB command window.
The num_terms variable will be an integer that is equal to the number of terms necessary to get better than 99% accuracy.

Checkpoint 15.5.5. Maclaurin Series Accuracy.

Using your script (or the solution shown in Figure 14.13), how many terms are necessary to achieve better than 99% accuracy when using the Maclaurin series expansion for \(\sin(x)\) when estimating \(x=1.874\) radians?
  • 2
  • Incorrect. Using only two terms does not provide sufficient accuracy.
  • 3
  • Incorrect. The approximation is improved, but it is not yet better than 99% accurate.
  • 4
  • Correct. Four terms are sufficient to achieve better than 99% accuracy for \(\sin(1.874)\text{.}\)
  • 5
  • Incorrect. While five terms would also work, fewer terms are required to exceed 99% accuracy.
You have attempted of activities on this page.