24
軟軟軟軟軟軟軟 1 While loop Root Finding GUI tool Design Lecture 6II While Loop

Lecture 6II While Loop

  • Upload
    alisa

  • View
    47

  • Download
    0

Embed Size (px)

DESCRIPTION

Lecture 6II While Loop. While loop Root Finding GUI tool Design. Inline : f(x) = sin(x). fs = input('f(x) = ','s'); f = inline(fs);. >> f(pi/2) ans = 1. Roots. Mathematics x is a root of f(x) if f(x) = 0 Numerical analysis x is a root of f(x) if abs(f(x)) < eps. >> sin(pi) - PowerPoint PPT Presentation

Citation preview

Page 1: Lecture 6II While Loop

軟體實作與計算實驗 1

While loopRoot Finding

GUI tool Design

Lecture 6II While Loop

Page 2: Lecture 6II While Loop

軟體實作與計算實驗 2

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

>> f(pi/2)

ans =

1

Inline : f(x) = sin(x)

Page 3: Lecture 6II While Loop

軟體實作與計算實驗 3

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 4: Lecture 6II While Loop

軟體實作與計算實驗 4

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 5: Lecture 6II While Loop

軟體實作與計算實驗 5

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 6: Lecture 6II While Loop

軟體實作與計算實驗 6

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 7: Lecture 6II While Loop

軟體實作與計算實驗 7

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 8: Lecture 6II While Loop

軟體實作與計算實驗 8

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 9: Lecture 6II While Loop

軟體實作與計算實驗 9

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 10: Lecture 6II While Loop

軟體實作與計算實驗 10

Halting condition

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

abs(f(c)) < eps

Page 11: Lecture 6II While Loop

軟體實作與計算實驗 11

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 12: Lecture 6II While Loop

軟體實作與計算實驗 12

my_fzero.m

Page 13: Lecture 6II While Loop

軟體實作與計算實驗 13

New

Page 14: Lecture 6II While Loop

軟體實作與計算實驗 14

Edit Text

Page 15: Lecture 6II While Loop

軟體實作與計算實驗 15

Addition

Page 16: Lecture 6II While Loop

軟體實作與計算實驗 16

Page 17: Lecture 6II While Loop

軟體實作與計算實驗 17

Page 18: Lecture 6II While Loop

軟體實作與計算實驗 18

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 19: Lecture 6II While Loop

軟體實作與計算實驗 19

Page 20: Lecture 6II While Loop

軟體實作與計算實驗 20

Add.fig

GUI tool

Add.m

Page 21: Lecture 6II While Loop

軟體實作與計算實驗 21

ADD16

A toolbox for addition of hexadecimal numbers

Page 22: Lecture 6II While Loop

軟體實作與計算實驗 22

Page 23: Lecture 6II While Loop

軟體實作與計算實驗 23

Symbolic integration

A toolbox for demonstrating symbolic integration

Page 24: Lecture 6II While Loop

軟體實作與計算實驗 24