lecture 6ii while loop

47
軟軟軟軟軟軟軟 1 Binary search While loop Root Finding Lecture 6II While Loop

Upload: nell-campbell

Post on 04-Jan-2016

39 views

Category:

Documents


3 download

DESCRIPTION

Lecture 6II While Loop. Binary search While loop Root Finding. An array of sorted integers. X is an array of n sorted integers X[i] < X[j], if i < j X=[1 3 5 7 9 11 13 15 17] e.x. X[3] = 5 < X[6] =11. Searching. Check whether integer s belongs X or not - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Lecture 6II While Loop

軟體實作與計算實驗 1

Binary search While loop

Root Finding

Lecture 6II While Loop

Page 2: Lecture 6II While Loop

軟體實作與計算實驗 2

An array of sorted integers

X is an array of n sorted integersX[i] < X[j], if i < jX=[1 3 5 7 9 11 13 15 17]e.x. X[3] = 5 < X[6] =11

Page 3: Lecture 6II While Loop

軟體實作與計算實驗 3

Searching

Check whether integer s belongs X or not

Sequential search: Compare s with X[1],X[2],…,X[n] one by one

Page 4: Lecture 6II While Loop

軟體實作與計算實驗 4

Input s,xn = length(x)i=1;

while X[i] < s

i=i+1;

X[i] == s

Y

fprintf(‘s in X’) fprintf(‘s not in X’)

Page 5: Lecture 6II While Loop

軟體實作與計算實驗 5

For large n, it is time expensive to compare s with elements in X one by one

Binary search can improve this drawback

Binary search is more efficient than sequential search

Page 6: Lecture 6II While Loop

軟體實作與計算實驗 6

An array of sorted elements

X is a vector of positive integersX[i] < X[j] if i < jInput sOutput i

i > 0, where x[i] == s i=0, when s ~= x[i] for all i

Page 7: Lecture 6II While Loop

軟體實作與計算實驗 7

Example

X=[1 3 5 7 9 11 13 15 17] and s=5Output : 3

Page 8: Lecture 6II While Loop

軟體實作與計算實驗 8

Example

X=[1 3 5 7 9 11 13 15 17] and s=4Output : 0

Page 9: Lecture 6II While Loop

軟體實作與計算實驗 9

An array of sorted natural numbers

N=10;X=ceil(rand(1,N)*10000);X=sort(X)

X =

Columns 1 through 7

1704 1834 2141 2633 6022 6050 6366

Columns 8 through 10

6596 6597 7537

Page 10: Lecture 6II While Loop

軟體實作與計算實驗 10

N=500;X=ceil(rand(1,N)*10000);X=sort(X)

a=1;b=length(X);

Page 11: Lecture 6II While Loop

軟體實作與計算實驗 11

Binary search cuts [a,b] into two sub-interval

Where c=floor((a+b)/2)

b]1,[c}{ 1]-c[a, c

Page 12: Lecture 6II While Loop

軟體實作與計算實驗 12

Three possible cases Case I

If s == X[c], halt

Case II If s < X[c], the answer should belong [a,c-1]

Case III If X[c] < s, the answer should belong [c+1,b]

Two operations Case II: Case III:

1-cb 1ca

Page 13: Lecture 6II While Loop

軟體實作與計算實驗 13

Example for case II

X=[1 3 5 7 9 11 13 15 17] and s=4a=1; b=9c= 5The location of s is within [a,c]By operation,

the searching range [a b] is [1 4]

1-cb

Page 14: Lecture 6II While Loop

軟體實作與計算實驗 14

Example for case III

X=[1 3 5 7 9 11 13 15 17] and s=13a=1; b=9c= 5The location of s is within [c,b]By operation

the searching range [a b] is [6 9]

1ca

Page 15: Lecture 6II While Loop

軟體實作與計算實驗 15

Example

X=[1 23 45 38 66 77 88 99 101 999] and s=66

a=1; b=5c= 3The location of s is within [c,b]By operation

the searching range [a b] is [3 5]

ca

Page 16: Lecture 6II While Loop

軟體實作與計算實驗 16

Input s,xb = length(x)a =1;c=floor((a+b)/2)

while ~(a>b |X(c) == s)

c=floor((a+b)/2)

s < X(c)

b=c-1

T

a=c+1

Page 17: Lecture 6II While Loop

軟體實作與計算實驗 17

Entry condition

Halting condition

(a>b | X(c) == s)

~(a>b | X(c) == s)

Page 18: Lecture 6II While Loop

軟體實作與計算實驗 18

fs = input('f(x) = ','s');f = inline(fs);

>> f(pi/2)

ans =

1

Inline : f(x) = sin(x)

Page 19: Lecture 6II While Loop

軟體實作與計算實驗 19

Roots

Mathematicsx is a root of f(x) if f(x) = 0

Numerical analysisx is a root of f(x) if abs(f(x)) < eps

>> sin(pi)

ans =

1.2246e-016

>> abs(sin(pi)) < eps

ans =

1

Page 20: Lecture 6II While Loop

軟體實作與計算實驗 20

Existence of roots

f(x) is well defined over interval [a,b] If f(x) is continuous and f(a)f(b) < 0

there exists at least one root within [a,b]

a

bf(a)

f(b)

Page 21: Lecture 6II While Loop

軟體實作與計算實驗 21

Bipartition

Partition interval [a,b] to two intervals

such that [a,b]=[a,c] U [c,b],

where c = (a+b) / 2

a bc = (a+b)/2

Page 22: Lecture 6II While Loop

軟體實作與計算實驗 22

Proposition

a bc = (a+b)/2

If f(a)f(b) < 0

it holds that f(a)f(c) < 0 or f(c)f(b) < 0

Page 23: Lecture 6II While Loop

軟體實作與計算實驗 23

Proof

f(a)f(b) < 0, f(c) ≠ 0

(I) f(a) > 0 and f(b) < 0

f(c)f(a) < 0 or f(c)f(b) < 0

(II) f(a) < 0 and f(b) > 0

f(c)f(a) < 0 or f(c)f(b) < 0

Page 24: Lecture 6II While Loop

軟體實作與計算實驗 24

Target interval

a bc = (a+b)/2

If f(a)f(b) < 0

it holds that f(a)f(c) < 0 or f(c)f(b) < 0

If f(a)f(c) < 0, choose [a c] as target intervalIf f(c)f(b) < 0, choose [c b] as target interval

Page 25: Lecture 6II While Loop

軟體實作與計算實驗 25

Binary search

Target interval [t1 t2]If halting condition holds, halta ← t1, b ← t2, c ← (t1+t2)/2If f(a)f(c) < 0, t1 ← a, t2 ← c choose [a

c]If f(c)f(b) < 0, t1 ← c, t2 ← b choose [c

b]

Page 26: Lecture 6II While Loop

軟體實作與計算實驗 26

Halting condition

c=(t1+t2)/2f(c) is close enough to zeroImplementation

abs(f(c)) < eps

Page 27: Lecture 6II While Loop

軟體實作與計算實驗 27

Zero finding

Flow Chart:Input t1,t2,fc=(t1+t2)/2

a=t1;b=t2;c=(a+b)/2if f(a)*f(c) < 0 t1=a;t2=c;else t1=c;t2=b;endc=(t1+t2)/2;

T

abs(f(c)) > eps

Return c

Page 28: Lecture 6II While Loop

軟體實作與計算實驗 28

my_fzero.m

Page 29: Lecture 6II While Loop

軟體實作與計算實驗 29

New

Page 30: Lecture 6II While Loop

軟體實作與計算實驗 30

Edit Text

Page 31: Lecture 6II While Loop

軟體實作與計算實驗 31

Addition

Page 32: Lecture 6II While Loop

軟體實作與計算實驗 32

Page 33: Lecture 6II While Loop

軟體實作與計算實驗 33

Page 34: Lecture 6II While Loop

軟體實作與計算實驗 34

function pushbutton1_Callback(hObject, eventdata, handles)% hObject handle to pushbutton1 (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB% handles structure with handles and user data (see GUIDATA)s1=get(handles.edit1,'String');s2=get(handles.edit2,'String');x=str2double(s1)+str2double(s2);s3=num2str(x);set(handles.edit3,'String',s3);return

Page 35: Lecture 6II While Loop

軟體實作與計算實驗 35

Page 36: Lecture 6II While Loop

軟體實作與計算實驗 36

Add.fig

GUI tool

Add.m

Page 37: Lecture 6II While Loop

軟體實作與計算實驗 37

ADD16

A toolbox for addition of hexadecimal numbers

Page 38: Lecture 6II While Loop

軟體實作與計算實驗 38

Page 39: Lecture 6II While Loop

軟體實作與計算實驗 39

Symbolic integration

A toolbox for demonstrating symbolic integration

Page 40: Lecture 6II While Loop

軟體實作與計算實驗 40

Page 41: Lecture 6II While Loop

軟體實作與計算實驗 41

Matlab Application Platform

Web serverStand-alone executionMobile connectionMatlab C++ or C mobile app

Page 42: Lecture 6II While Loop

軟體實作與計算實驗 42

Exercise #a#b

Generate four distinct characters within {’0’, ’1’, ’2’, ’3’, ’4’, ’5’,’6’,’7’,’8’,’9’} randomly

Draw a while-loop to realize the game of #a#bAllow a player to key-in a four-digit stringPrint na’a’nb’b’ in response to the given string Halt if the guess is scored as 4’a’

Page 43: Lecture 6II While Loop

軟體實作與計算實驗 43

na denotes the number of guessed characters that appear at right position in the target

nb denotes the number of guessed characters that appear at wrong position in the target

Page 44: Lecture 6II While Loop

軟體實作與計算實驗 44

Example

Target : 6481Guess : 1628 Output: 0a3bGuess : 1946 Output: 0a3bGuess : 6283 Output: 1a1bGuess : 6481 Output: 4a0b

Page 45: Lecture 6II While Loop

軟體實作與計算實驗 45

Draw a flow chart to illustrate how to determine na for given target and guess

Draw a flow chart to illustrate how to determine nb for given target and guess

Page 46: Lecture 6II While Loop

軟體實作與計算實驗 46

Write MATLAB functions to implement flow charts for #a#b

Page 47: Lecture 6II While Loop

軟體實作與計算實驗 47

Characters & integers

tt = randperm(10)-1

cc=input('keyin:', 's');

tt(1) == cc(1)

tt(1) == cc(1) - '0'