數值方法 2008, Applied Mathematics NDHU1  Looping for loop while loop  Break and return  Recursion Lecture 2 Iteration.

Slides:



Advertisements
Similar presentations
Introduction to C Programming
Advertisements

How SAS implements structured programming constructs
數值方法 2008, Applied Mathematics NDHU 1 Nonlinear systems Newton’s method The steepest descent method.
Dr. Yang, Qingxiong (with slides borrowed from Dr. Yuen, Joe) LT4: Control Flow - Loop CS2311 Computer Programming.
Recursion.
ITC 240: Web Application Programming
Branching Constructs Review l what are branching constructs? what type of branching constructs have we studied? l what is nested if? l what is multiway.
Computer Science 1620 Loops.
1 10/20/08CS150 Introduction to Computer Science 1 do/while and Nested Loops Section 5.5 & 5.11.
Chapter 5: Control Structures II (Repetition)
General Computer Science for Engineers CISC 106 Lecture 10 Roger Craig Computer and Information Sciences 3/06/2009.
Loops – While, Do, For Repetition Statements Introduction to Arrays
ECE122 L11: For loops and Arrays March 8, 2007 ECE 122 Engineering Problem Solving with Java Lecture 11 For Loops and Arrays.
Loops Repetition Statements. Repetition statements allow us to execute a statement multiple times Often they are referred to as loops Like conditional.
© 2004 Pearson Addison-Wesley. All rights reserved5-1 Iterations/ Loops The while Statement Other Repetition Statements.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 6 Repetition Statements.
Loops are MATLAB constructs that permit us to execute a sequence of statements more than once. There are two basic forms of loop constructs: i. while.
數值方法 2008, Applied Mathematics NDHU1  An iterative approach for root finding  Newton method Lecture 3II Root Finding.
數值方法 2008 Applied Mathematics, NDHU1  Bisection method for root finding  Binary search  fzero Lecture 4.
數值方法 2008 Applied Mathematics, NDHU1  Bisection method for root finding  Binary search  fzero Lecture 4.
Chapter 5: Control Structures II (Repetition)
CHAPTER 5: CONTROL STRUCTURES II INSTRUCTOR: MOHAMMAD MOJADDAM.
EGR 2261 Unit 5 Control Structures II: Repetition  Read Malik, Chapter 5.  Homework #5 and Lab #5 due next week.  Quiz next week.
REVIEW 2 Exam History of Computers 1. CPU stands for _______________________. a. Counter productive units b. Central processing unit c. Copper.
Copyright 2009 by Pearson Education Building Java Programs Chapter 2 Lecture 2-2: The for Loop reading: 2.3 self-check: exercises: 2-14 videos: Ch.
Algorithmic Recursion. Recursion Alongside the algorithm, recursion is one of the most important and fundamental concepts in computer science as well.
1 Inference Rules and Proofs (Z); Program Specification and Verification Inference Rules and Proofs (Z); Program Specification and Verification.
Lecture 4. RAM Model, Space and Time Complexity
Lecture 8: Choosing the Correct Loop. do … while Repetition Statement Similar to the while statement Condition for repetition only tested after the body.
Vectors and Matrices In MATLAB a vector can be defined as row vector or as a column vector. A vector of length n can be visualized as matrix of size 1xn.
Copyright © Nancy Acemian 2004 For Loops-Break-Continue COMP For loop is a counter controlled loop. For loop is a pretest loop. Used when number.
Chapter 5: Control Structures II (Repetition). Objectives In this chapter, you will: – Learn about repetition (looping) control structures – Learn how.
Control Structures II (Repetition). Objectives In this chapter you will: Learn about repetition (looping) control structures Explore how to construct.
Saeed Ghanbartehrani Summer 2015 Lecture Notes #5: Programming Structures IE 212: Computational Methods for Industrial Engineering.
Lecture 4: Calculating by Iterating. The while Repetition Statement Repetition structure Programmer specifies an action to be repeated while some condition.
Advanced Program Design. Review  Step 1: Problem analysis and specification –Specification description of the problem’s inputs and output –Analysis generalize.
ITERATIVE STATEMENTS. Definition Iterative statements (loops) allow a set of instruction to be executed or performed several until condition are met.
Matlab Programming for Engineers
Chapter 8: MuPAD Programming I Conditional Control and Loops MATLAB for Scientist and Engineers Using Symbolic Toolbox.
軟體實作與計算實驗 1  While-loop flow chart  Decimal to binary representations Lecture 6 While-Loop programming.
數值方法 2008 Applied Mathematics, NDHU1  Lagrange polynomial  Polynomial interpolation Lecture 4II.
1 do-while Statement 2 Do-While Statement Is a looping control structure in which the loop condition is tested after each iteration of the loop. SYNTAX.
5 While-Statements © 2010 David A Watt, University of Glasgow Accelerated Programming 2 Part I: Python Programming.
數值方法 2008, Applied Mathematics NDHU1 Chaos time series.
Think Possibility 1 Iterative Constructs ITERATION / LOOPS C provides three loop structures: the for-loop, the while-loop, and the do-while-loop. Each.
1 Looping Dale/Weems/Headington. 2 KA/JS/P Warning l Save your work often! l In the Khan Academy, JavaScript environment, infinite loops will lock up.
The do - while Loop Venkatesh Ramamoorthy 02-March-2005.
Programming With Java ICS201 University Of Ha’il1 Chapter 11 Recursion.
9. ITERATIONS AND LOOP STRUCTURES Rocky K. C. Chang October 18, 2015 (Adapted from John Zelle’s slides)
Fourth Quarter.  Involves loops or cycles ◦ Loops: means that a process may be repeated as long as certain condition remains true or remains false. ◦
Lecture 7 – Repetition (Loop) FTMK, UTeM – Sem /2014.
數值方法, Applied Mathematics NDHU 1 Linear system. 數值方法, Applied Mathematics NDHU 2 Linear system.
Looping Increment/Decrement Switch. Flow of Control Iteration/Switch Statements.
軟體實作與計算實驗 1  While loop  Positive integer square root  Decimal to binary transition Lecture 6 While Loop.
CSE123 - Lecture 4 Structured Programming- Loops.
Beginning Programming for Engineers Matlab Conditional Computation.
CONTENTS Loop Statements Parts of a loop Types of Loops Nested Loops
CS 170 – INTRO TO SCIENTIFIC AND ENGINEERING PROGRAMMING.
IST 210: PHP Logic IST 210: Organization of Data IST2101.
Evaluate the expression.
REPETITION CONTROL STRUCTURE
Chapter 3: Decisions and Loops
Branching Constructs Review
Looping.
Repeating Instructions And Advance collection
Chapter 6 Decision Making and Looping
Chapter 8: More on the Repetition Structure
Vectors and Matrices In MATLAB a vector can be defined as row vector or as a column vector. A vector of length n can be visualized as matrix of size 1xn.
Flow of Control.
Programming Fundamental
Presentation transcript:

數值方法 2008, Applied Mathematics NDHU1  Looping for loop while loop  Break and return  Recursion Lecture 2 Iteration

數值方法 2008, Applied Mathematics NDHU2 for-loop Source codes Evaluate n=input('input an integer:'); v=0; for i=1:n v=v+4*i^3+3*i^2+2*i+1; end fprintf('The answer is %d\n',v);

數值方法 2008, Applied Mathematics NDHU3 For-Looping for i=1:n v=v+4*i^3+3*i^2+2*i+1; end Iteratively executing same body instructions Increasing or decreasing control index

數值方法 2008, Applied Mathematics NDHU4 Increasing index v=v+4*i^3+3*i^2+2*i+1 F T i =1 i=i+1 for i=1:n v=v+4*i^3+3*i^2+2*i+1; end

數值方法 2008, Applied Mathematics NDHU5 Decreasing index v=v+4*i^3+3*i^2+2*i+1 F T i =n i=i-1 for i=n:-1:1 v=v+4*i^3+3*i^2+2*i+1; end

數值方法 2008, Applied Mathematics NDHU6 Representation of polynomials representation

數值方法 2008, Applied Mathematics NDHU7 roots roots([1 -2]) ans = 2

數值方法 2008, Applied Mathematics NDHU8 Addition of two polynomials

數值方法 2008, Applied Mathematics NDHU9

10 add_poly Source codes m=length(a);n=length(b); c=zeros(1,max(m,n)); size_c=length(c); for i=1:m c(size_c-i+1)=c(size_c-i+1)+a(m-i+1); end for i=1:n c(size_c-i+1)=c(size_c-i+1)+b(n-i+1); end ii=min(find(c~=0)); c=c(ii:size_c);

數值方法 2008, Applied Mathematics NDHU11 add_poly >> a=[-1 2 3]; b=[1 2]; add_poly(a,b) ans = >> a=[-1 2 3]; b=[1 -2 3]; >> add_poly(a,b) ans = 6

數值方法 2008, Applied Mathematics NDHU12 Nested loop Loops within loops Ex. source codessource codes : multiplication of two matrices

數值方法 2008, Applied Mathematics NDHU13 Nested loop [na ma]=size(A); [nb mb]=size(B); for i=1:na for j=1:mb C(i,j)=A(i,:)*B(:,j); end outer inner The inner loop body is executed na x mb times Nested loops are usually time consuming

數值方法 2008, Applied Mathematics NDHU14 for i=1:na for j=1:nb C(i,j)=A(i,:)*B(:,j); end Nested loop

數值方法 2008, Applied Mathematics NDHU15 Speeding-up Eliminate nested loops Rewrite loops or nested loops to equivalent loop-free codes Ex. C=A*B

數值方法 2008, Applied Mathematics NDHU16 while-loop statements false true n >1 while n>1 s=[mod(n,2) s]; n=(n-mod(n,2))/2; end Body statements are executed repeatedly until the halting condition holds

數值方法 2008, Applied Mathematics NDHU17 while-loop Halting condition: a logical expression Loop body while n>1 s=[mod(n,2) s]; n=(n-mod(n,2))/2; end

數值方法 2008, Applied Mathematics NDHU18 dec2bin function s=dec2bin(n) % n: a positive integer % s: a vector for binary representation of n s=[]; while n>1 s=[mod(n,2) s]; n=(n-mod(n,2))/2; end if n==1 s=[n s]; end return Source codes

數值方法 2008, Applied Mathematics NDHU19 Decimal to binary Find binary representation of a decimal number Input: a decimal number Output: binary representation

數值方法 2008, Applied Mathematics NDHU20 dec2bin >> dec2bin(12) ans = >> ceil(log2(12)) ans = 4

數值方法 2008, Applied Mathematics NDHU21 dec2bin >> dec2bin(88) ans = >> ceil(log2(88)) ans = 7

數值方法 2008, Applied Mathematics NDHU22 Recursive function

數值方法 2008, Applied Mathematics NDHU23 Fibonacci number

數值方法 2008, Applied Mathematics NDHU24 Fibonacci number A tiling with squares whose sides are successive Fibonacci numbers in length

數值方法 2008, Applied Mathematics NDHU25 fib function s=fib(n) % % n: a nonnegative integer % fib(1) and fib(0) returns 1 and 0 % fib(n) equals fib(n-1)+fib(n-2) for n>1 if n<=1 s=n; return end s=fib(n-1)+fib(n-2); return source codes

數值方法 2008, Applied Mathematics NDHU26 return Execution of a matlab function always exits whenever ‘return’ is executed There are two exit points in fib function if n<=1 s=n; return end s=fib(n-1)+fib(n-2); return

數值方法 2008, Applied Mathematics NDHU27 Recursive function call A function calls itself during its execution Call fib(n) with n > 1 fib calls itself at line 10

數值方法 2008, Applied Mathematics NDHU28 Calling Tree of fib(4) Fib(4) Fib(3) Fib(2) Fib(1) Fib(0) Fib(1) Fib(2) Fib(1) Fib(0)

數值方法 2008, Applied Mathematics NDHU29 Exercise exercise 2exercise