Sign In | Subscribe
Start learning today, and be successful in your academic & professional career. Start Today!
Loading video...
This is a quick preview of the lesson. For full access, please Log In or Sign up.
For more information, please see full course syllabus of MATLAB
  • Discussion

  • Study Guides

  • Practice Questions

  • Download Lecture Slides

  • Table of Contents

  • Related Books

Bookmark and Share

Start Learning Now

Our free lessons will get you started (Adobe Flash® required).
Get immediate access to our entire library.

Sign up for Educator.com

Membership Overview

  • Unlimited access to our entire library of courses.
  • Search and jump to exactly what you want to learn.
  • *Ask questions and get answers from the community and our teachers!
  • Practice questions with step-by-step solutions.
  • Download lesson files for programming and software training practice.
  • Track your course viewing progress.
  • Download lecture slides for taking notes.
  • Learn at your own pace... anytime, anywhere!

While

  • In this session we are going to talk more about while and examples of its application to solve problems. We will also learn about break and continue. Any loop structure you are likely to encounter in scientific programming can be coded with either ‘pure’ for or while loops, as illustrated by the examples in this chapter. However, as a concession to intellectual laziness I feel obliged to mention in passing the break and continue statements.
  • If there are a number of different conditions to stop a while loop you may be tempted to use a for with the number of repetitions set to some accepted cut-off value (or even Inf) but enclosing if statements which break out of the for when the various conditions are met. Why is this not regarded as the best programming style? The reason is simply that when you read the code months later you will have to wade through the whole loop to find all the conditions to end it, rather than see them all paraded at the start of the loop in the while clause.
  • At the end of this session we are going to have an example of graphical user interface (GUI), made based on what we have learned.

While

The following method of computing π is due to Archimedes:
a. Let A=1 and N=6;
b. Repeat 10 times, say:
i. Replace N by 2N
ii. Replace A by [2−√{4−A2}][1/2]
iii. Let L=NA/2
iv. Let  U=[L/(√{1−[(A2)/2]})]
v. Let P=(U+L)/2(estimated π)
vi. Let E=(U-L)/2(estimated error)
vii. Print N, P, E
c. Stop

Write a program to do that.
a = 1;
n = 6;
for i = 1 : 10
n = 2 * n;
a = sqrt(2 - sqrt(4 - a * a));
l = n * a / 2;
u = l / sqrt(1 - a * a / 2);
p = (u + l) / 2;
e = (u - l) / 2;
disp( [n, p, e] );
end
Write two programs, first using for and logical vectors and another one using while. The program should compute the sum of the series 12 + 22 + 32 + 42 + ......   such that the sum is as large as possible without exceeding 1000, the programs should display how many terms are used in the sum.
sum = 0;
terms = 0;
while sum  < = 1000
terms = terms + 1;
sum = sum + terms 2;
end
disp( [terms, sum] );

terms = 0;
sum = 0;

for i = 1:1000;
    if sum  < = 1000
        sum = sum + i 2;
        terms = terms + 1;
    end
end
disp( [terms, sum] );

*These practice questions are only helpful when you work on them offline on a piece of paper and then use the solution steps function to check your answer.

Answer

While

Lecture Slides are screen-captured images of important points in the lecture. Students can download and print out these lecture slide images to do practice problems as well as take notes while watching the lecture.

  • Intro 0:00
  • Doubling Time of an Investment 0:33
    • Create Steps
    • Translate Into Code
    • Conditions and Loops
    • Determinate For Loop Unavailability
  • Prime Numbers 5:27
    • Create Steps
    • Compute in MATLAB
  • Break and Continue 12:05
  • Menu 13:37
    • Menu Window
    • Do This and Do That
    • Value of K