Start learning today, and be successful in your academic & professional career. Start Today!

Loading video...

Enter your Sign on user name and password.

Start learning today, and be successful in your academic & professional career. Start Today!

Loading video...

- 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.

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−A^{2}}]^{[1/2]}

iii. Let L=NA/2

iv. Let U=[L/(√{1−[(A^{2})/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. Let A=1 and N=6;

b. Repeat 10 times, say:

i. Replace N by 2N

ii. Replace A by [2−√{4−A

iii. Let L=NA/2

iv. Let U=[L/(√{1−[(A

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

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 1^{2} + 2^{2} + 3^{2} + 4^{2} + ...... 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] );

terms = 0;

while sum < = 1000

terms = terms + 1;

sum = sum + terms

end

disp( [terms, sum] );

terms = 0;

sum = 0;

for i = 1:1000;

if sum < = 1000

sum = sum + i

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

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

I. Introduction | ||
---|---|---|

MATLAB Essentials, Part 1 | 8:36 | |

MATLAB Essentials, Part 2 | 7:39 | |

II. MATLAB Fundamentals | ||

Introduction to Programs | 6:42 | |

Arrays & Matrices | 7:06 | |

Operator, Expression & Statements | 13:26 | |

Repeating With For | 14:21 | |

Decisions, Part 1 | 14:35 | |

Decisions, Part 2 | 8:53 | |

Last Notes on MATLAB Introduction | 9:47 | |

III. Logical Vectors | ||

Logical Vectors, Part 1 | 11:23 | |

Logical Vectors, Part 2 | 11:40 | |

IV. Program Design | ||

Program Design & Algorithm Development | 19:36 | |

V. Graphics | ||

Graphics, Part 1 | 9:14 | |

Graphics, Part 2 | 17:20 | |

Graphics, Part 3 | 18:37 | |

VI. Loops | ||

Loops | 10:38 | |

While | 17:01 | |

VII. Project | ||

Projectile Problem | 16:33 | |

VIII. Function M-Files | ||

Function M-Files, Part 1 | 12:55 | |

Function M-Files, Part 2 | 14:14 | |

Function M-Files, Part 3 | 11:54 | |

IX. Graphics Continued | ||

More on Graphics | 11:13 | |

Graphical User Interface, Part 1 | 7:25 | |

Graphical User Interface, Part 2 | 9:33 | |

X. Dynamical Systems | ||

Dynamical Systems | 12:45 | |

XI. Simulation | ||

Examples of Simulation | 10:45 | |

XII. Numerical Methods | ||

Numerical Methods | 22:20 | |

XIII. Simulink | ||

Simulink | 9:37 |

## Start Learning Now

Our free lessons will get you started (Adobe Flash

Sign up for Educator.com^{®}required).Get immediate access to our entire library.

## Membership Overview

Unlimited access to our entire library of courses.Learn at your own pace... anytime, anywhere!