軟體實作與計算實驗 1  Binary search  While loop  Root Finding Lecture 6II While Loop.

Slides:



Advertisements
Similar presentations
軟體實作與計算實驗 1  Roots of nonlinear functions  Nested FOR loops Lecture 5II.
Advertisements

數值方法 2008, Applied Mathematics NDHU 1 Nonlinear systems Newton’s method The steepest descent method.
Analysis of Algorithms
MATLAB Examples. CS 1112 MATLAB Examples Find the number of positive numbers in a vector x = input( 'Enter a vector: ' ); count = 0; for ii = 1:length(x),
CIS 101: Computer Programming and Problem Solving Lecture 7 Usman Roshan Department of Computer Science NJIT.
Get number The ginput command creates crosshairs. When the user clicks the x,y values of the crosshairs are returned.
Lectures on Numerical Methods 1 Numerical Methods Charudatt Kadolkar Copyright 2000 © Charudatt Kadolkar.
Week 10 - Programming III So far: –Branches/condition testing –Loops Today: –Problem solving methodology –Examples Textbook chapter 7.
Fin500J: Mathematical Foundations in Finance Topic 3: Numerical Methods for Solving Non-linear Equations Philip H. Dybvig Reference: Numerical Methods.
Data Compression Arithmetic coding. Arithmetic Coding: Introduction Allows using “fractional” parts of bits!! Used in PPM, JPEG/MPEG (as option), Bzip.
- ABHRA DASGUPTA Solving Adhoc and Math related problems.
Lecture 15 Practice & exploration Subfunctions: Functions within functions “Guessing Game”: A hands-on exercise in evolutionary design © 2007 Daniel Valentine.
Ch. 8 & 9 – Linear Sorting and Order Statistics What do you trade for speed?
Scientific Computing Algorithm Convergence and Root Finding Methods.
Chapter 5 Algorithm Analysis 1CSCI 3333 Data Structures.
INTRO TO PROGRAMMING Chapter 2. M-files While commands can be entered directly to the command window, MATLAB also allows you to put commands in text files.
Exercise problems for students taking the Programming Parallel Computers course. Janusz Kowalik Piotr Arlukowicz Tadeusz Puzniakowski Informatics Institute.
Martin Ellison University of Warwick and CEPR Bank of England, December 2005 Introduction to MATLAB.
數值方法 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.
CMPSC 200 Spring 2013 Lecture 38 November 18 or 20, 2013 (depending on section)
軟體實作與計算實驗 1 Lecture 9 Classification Two mappings from position to color Linear combination of distances to centers RBF: Linear combination of exponential.
1.4 Continuity and One-Sided Limits This will test the “Limits” of your brain!
Microsoft Visual Basic 2008 CHAPTER NINE Using Arrays and File Handling.
25 November 2014Birkbeck College1 Introduction to Computer Systems Lecturer: Steve Maybank Department of Computer Science and Information Systems
Introduction to Matlab & Data Analysis
STRINGS CMSC 201 – Lab 3. Overview Objectives for today's lab:  Obtain experience using strings in Python, including looping over characters in strings.
© by Kenneth H. Rosen, Discrete Mathematics & its Applications, Sixth Edition, Mc Graw-Hill, 2007 Chapter 9 (Part 2): Graphs  Graph Terminology (9.2)
Approximate computations Jordi Cortadella Department of Computer Science.
Order Statistics. Order statistics Given an input of n values and an integer i, we wish to find the i’th largest value. There are i-1 elements smaller.
ROM & PLA Digital Logic And Computer Design
function varargout = MovieJoiner(varargin) % MOVIEJOINER M-file for MovieJoiner.fig % MOVIEJOINER, by itself, creates a new MOVIEJOINER or raises the.
Control Structures II Repetition (Loops). Why Is Repetition Needed? How can you solve the following problem: What is the sum of all the numbers from 1.
CSCI/CMPE 4341 Topic: Programming in Python Review: Exam I Xiang Lian The University of Texas – Pan American Edinburg, TX 78539
Analyzing Functions (4.16) y=f(x) MATLAB. Functional Analysis includes: Plotting and evaluating a function Finding extreme points Finding the roots (zeros.
Numerical Methods Solution of Equation.
Introduction to Numerical Analysis I MATH/CMPSC 455 Bisection Method.
軟體實作與計算實驗 1  While-loop flow chart  Decimal to binary representations Lecture 6 While-Loop programming.
EGR 115 Introduction to Computing for Engineers
8.1 8 Algorithms Foundations of Computer Science  Cengage Learning.
EGR 115 Introduction to Computing for Engineers Branching & Program Design – Part 3 Friday 03 Oct 2014 EGR 115 Introduction to Computing for Engineers.
The MatLab Language Mathematics Laboratory The MathWorks
INVITATION TO Computer Science 1 11 Chapter 2 The Algorithmic Foundations of Computer Science.
MATLAB and SimulinkLecture 61 To days Outline Graphical User Interface (GUI) Exercise on this days topics.
CS Class 04 Topics  Selection statement – IF  Expressions  More practice writing simple C++ programs Announcements  Read pages for next.
Arrays. Arrays are objects that help us organize large amounts of information.
軟體實作與計算實驗 1  While loop  Positive integer square root  Decimal to binary transition Lecture 6 While Loop.
Finding zeros (also called roots) of a function Overview: Define the problem Methods of solution Graphical Newton’s Bisection Secant.
Click to edit Master title style Click to edit Master text styles –Second level Third level –Fourth level »Fifth level 1 Fundamentals of Programming Most.
Discrete Mathematics Lecture # 17 Function. Relations and Functions  A function F from a set X to a set Y is a relation from X to Y that satisfies the.
Program Design & Development EE 201 C7-1 Spring
軟體實作與計算實驗 1  SUDOKU  New  Play  Check Lecture 8II SUDOKU PUZZLE.
數值方法 2008, Applied Mathematics NDHU 1 Numerical Integration.
Chapter 5 Numerical Root Findings
程式設計實作.
Approximate computations
Unit-III Algebraic Structures
Solution of Nonlinear Equations (Root finding Problems
Approximate computations
The relational operators
© Akhilesh Bajaj, All rights reserved.
Introduction to Programming
Algorithm Discovery and Design
MATLAB Tutorial ECE 002 Professor S. Ahmadi.
Finding zeros (also called roots) of a function
Introduction to Programming
1.4 Continuity and One-Sided Limits This will test the “Limits”
Week 7: Computer Tools for Problem Solving and Critical Thinking
Electrical and Computer Engineering Department SUNY – New Paltz
Presentation transcript:

軟體實作與計算實驗 1  Binary search  While loop  Root Finding Lecture 6II While Loop

軟體實作與計算實驗 2 An array of sorted integers X is an array of n sorted integers X[i] < X[j], if i < j X=[ ] e.x. X[3] = 5 < X[6] =11

軟體實作與計算實驗 3 Searching Check whether integer s belongs X or not Sequential search: Compare s with X[1],X[2],…,X[n] one by one

軟體實作與計算實驗 4 Input s,x n = length(x) i=1; while X[i] < s i=i+1; X[i] == s Y fprintf(‘s in X’)fprintf(‘s not in X’)

軟體實作與計算實驗 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

軟體實作與計算實驗 6 An array of sorted elements X is a vector of positive integers X[i] < X[j] if i < j Input s Output i i > 0, where x[i] == s i=0, when s ~= x[i] for all i

軟體實作與計算實驗 7 Example X=[ ] and s=5 Output : 3

軟體實作與計算實驗 8 Example X=[ ] and s=4 Output : 0

軟體實作與計算實驗 9 An array of sorted natural numbers N=10; X=ceil(rand(1,N)*10000); X=sort(X) X = Columns 1 through Columns 8 through

軟體實作與計算實驗 10 N=500; X=ceil(rand(1,N)*10000); X=sort(X) a=1;b=length(X);

軟體實作與計算實驗 11 Binary search cuts [a,b] into two sub- interval Where c=floor((a+b)/2)

軟體實作與計算實驗 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:

軟體實作與計算實驗 13 Example for case II X=[ ] and s=4 a=1; b=9 c= 5 The location of s is within [a,c] By operation, the searching range [a b] is [1 4]

軟體實作與計算實驗 14 Example for case III X=[ ] and s=13 a=1; b=9 c= 5 The location of s is within [c,b] By operation the searching range [a b] is [6 9]

軟體實作與計算實驗 15 Example X=[ ] and s=66 a=1; b=5 c= 3 The location of s is within [c,b] By operation the searching range [a b] is [3 5]

軟體實作與計算實驗 16 Input s,x b = 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

軟體實作與計算實驗 17 Entry condition Halting condition (a>b | X(c) == s) ~(a>b | X(c) == s)

軟體實作與計算實驗 18 fs = input('f(x) = ','s'); f = inline(fs); >> f(pi/2) ans = 1 Inline : f(x) = sin(x)

軟體實作與計算實驗 19 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) ans = e-016 >> abs(sin(pi)) < eps ans = 1

軟體實作與計算實驗 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 b f(a) f(b)

軟體實作與計算實驗 21 Bipartition Partition interval [a,b] to two intervals such that [a,b]=[a,c] U [c,b], where c = (a+b) / 2 ab c = (a+b)/2

軟體實作與計算實驗 22 Proposition ab c = (a+b)/2 If f(a)f(b) < 0 it holds that f(a)f(c) < 0 or f(c)f(b) < 0

軟體實作與計算實驗 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 f(c)f(a) < 0 or f(c)f(b) < 0

軟體實作與計算實驗 24 Target interval ab c = (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 interval If f(c)f(b) < 0, choose [c b] as target interval

軟體實作與計算實驗 25 Binary search Target interval [t1 t2] If halting condition holds, halt a ← t1, b ← t2, c ← (t1+t2)/2 If 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]

軟體實作與計算實驗 26 Halting condition c=(t1+t2)/2 f(c) is close enough to zero Implementation abs(f(c)) < eps

軟體實作與計算實驗 27 Zero finding Flow Chart: Input t1,t2,f c=(t1+t2)/2 a=t1;b=t2;c=(a+b)/2 if f(a)*f(c) < 0 t1=a;t2=c; else t1=c;t2=b; end c=(t1+t2)/2; T abs(f(c)) > eps Return c

軟體實作與計算實驗 28 my_fzero.m

軟體實作與計算實驗 29 New

軟體實作與計算實驗 30 Edit Text

軟體實作與計算實驗 31 Addition

軟體實作與計算實驗 32

軟體實作與計算實驗 33

軟體實作與計算實驗 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

軟體實作與計算實驗 35

軟體實作與計算實驗 36 Add.fig GUI tool Add.m

軟體實作與計算實驗 37 ADD16 A toolbox for addition of hexadecimal numbers

軟體實作與計算實驗 38

軟體實作與計算實驗 39 Symbolic integration A toolbox for demonstrating symbolic integration

軟體實作與計算實驗 40

軟體實作與計算實驗 41 Matlab Application Platform Web server Stand-alone execution Mobile connection Matlab  C++ or C  mobile app

軟體實作與計算實驗 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#b Allow a player to key-in a four-digit string Print n a ’a’n b ’b’ in response to the given string Halt if the guess is scored as 4’a’

軟體實作與計算實驗 43 n a denotes the number of guessed characters that appear at right position in the target n b denotes the number of guessed characters that appear at wrong position in the target

軟體實作與計算實驗 44 Example Target : 6481 Guess : 1628 Output: 0a3b Guess : 1946 Output: 0a3b Guess : 6283 Output: 1a1b Guess : 6481 Output: 4a0b

軟體實作與計算實驗 45 Draw a flow chart to illustrate how to determine n a for given target and guess Draw a flow chart to illustrate how to determine n b for given target and guess

軟體實作與計算實驗 46 Write MATLAB functions to implement flow charts for #a#b

軟體實作與計算實驗 47 Characters & integers tt = randperm(10)-1 cc=input('keyin:', 's'); tt(1) == cc(1) tt(1) == cc(1) - '0'