מבוא מורחב למדעי המחשב בשפת Scheme תרגול 4. Outline High order procedures –Finding Roots –Compose Functions Accelerating Computations –Fibonacci 2.

Slides:



Advertisements
Similar presentations
Towers of Hanoi Move n (4) disks from pole A to pole C such that a disk is never put on a smaller disk A BC ABC.
Advertisements

COMPOSITE FUNCTIONS. The composite function: fg means… Apply the rule for g, then, apply the rule for f. So, if f(x) = x 2 and g(x) = 3x + 1 Then fg(2)
מבוא מורחב 1 Lecture 4 Material in the textbook on Pages 44-46, of 2nd Edition Sections and Hanoy towers.
Regula-Falsi Method. Type of Algorithm (Equation Solver) The Regula-Falsi Method (sometimes called the False Position Method) is a method used to find.
מבוא מורחב למדעי המחשב בשפת Scheme תרגול 4. Outline Repeated f Accelerating computations – Fibonacci Let and let* Recursion examples – Palindrome? – Log.
Bisection Method (Midpoint Method for Equations).
מבוא מורחב למדעי המחשב בשפת Scheme תרגול 7 1. Outline More list examples Symbols 2.
מבוא מורחב למדעי המחשב בשפת Scheme תרגול 2. 2 Outline Scoping and block structure Recursive and iterative processes Orders of growth.
Second Term 05/061 Roots of Equations Bracketing Methods.
מבוא מורחב למדעי המחשב בשפת Scheme תרגול 2. Reminder: Recursive algorithm Reduce problem to one or more sub-problems of smaller sizes (linear or tree.
מבוא מורחב - שיעור 4 1 Lecture 4 Order of Growth Fun with recursion  Fast exponentiation  Hanoi towers.
מבוא מורחב למדעי המחשב בשפת Scheme תרגול 4. Outline High order procedures –Finding Roots –Compose Functions Accelerating Computations –Fibonacci 2.
Roots of Equations Bracketing Methods.
מבוא מורחב - שיעור 6 1 Lecture 6 High order procedures Primality testing The RSA cryptosystem.
מבוא מורחב 1 Lecture 3 Material in the textbook Sections to
מבוא מורחב למדעי המחשב בשפת Scheme תרגול 3. 2 Outline Let High order procedures.
Lectures on Numerical Methods 1 Numerical Methods Charudatt Kadolkar Copyright 2000 © Charudatt Kadolkar.
מבוא מורחב 1 Lecture 3 Material in the textbook on Pages of 2nd Edition Sections to
Fin500J: Mathematical Foundations in Finance Topic 3: Numerical Methods for Solving Non-linear Equations Philip H. Dybvig Reference: Numerical Methods.
- + Suppose f(x) is a continuous function of x within interval [a, b]. f(a) = - ive and f(b) = + ive There exist at least a number p in [a, b] with f(p)
Section 5.5 – The Real Zeros of a Rational Function
February 17, 2015Applied Discrete Mathematics Week 3: Algorithms 1 Double Summations Table 2 in 4 th Edition: Section th Edition: Section th.
Roots of Equations Chapter 3. Roots of Equations Also called “zeroes” of the equation –A value x such that f(x) = 0 Extremely important in applications.
Scientific Computing Algorithm Convergence and Root Finding Methods.
Copyright © 2011 Pearson, Inc. 2.3 Polynomial Functions of Higher Degree with Modeling.
Solving Non-Linear Equations (Root Finding)
Continuity ( Section 1.8) Alex Karassev. Definition A function f is continuous at a number a if Thus, we can use direct substitution to compute the limit.
AP CALCULUS PERIODIC REVIEW. 1: Limits and Continuity A function y = f(x) is continuous at x = a if: i) f(a) is defined (it exists) ii) iii) Otherwise,
Sudeshna Sarkar, IIT Kharagpur 1 Functions : Recursion Lecture
Approximate computations Jordi Cortadella Department of Computer Science.
Week 6 - Monday.  What did we talk about last time?  Exam 1!  Before that:  Recursion.
Today’s topics Orders of growth of processes Relating types of procedures to different orders of growth.
Solving equations numerically The sign - change rule If the function f(x) is continuous for an interval a  x  b of its domain, if f(a) and f(b) have.
Principles Of Programming Languages Lecture 2 Today Design-By-Contract Iteration vs. Recursion.
Sudeshna Sarkar, IIT Kharagpur 1 Functions : Recursion Lecture
Principles Of Programming Languages Lecture 2 Outline Design-By-Contract Iteration vs. Recursion Scope and binding High-order procedures.
1.4 Continuity  f is continuous at a if 1. is defined. 2. exists. 3.
The Bisection Method. Introduction Bisection Method: Bisection Method = a numerical method in Mathematics to find a root of a given function.
Solving Non-Linear Equations (Root Finding)
Numerical Methods Solution of Equation.
Recursive Methods for Finding Roots of Functions Bisection & Newton’s Method.
From Lambda Calculus to LISP Functional Programming Academic Year Alessandro Cimatti
A Different Solution  alternatively we can use the following algorithm: 1. if n == 0 done, otherwise I. print the string once II. print the string (n.
מבוא מורחב 1 Lecture 4 Material in the textbook on Pages 44-46, of 2nd Edition Sections and Hanoy towers.
מבוא מורחב למדעי המחשב בשפת Scheme תרגול 3. Outline High order procedures –Finding Roots –Compose Functions Accelerating Computations –Fibonacci 2.
Analyzing Programs: Order of Growth CMSC Introduction to Computer Programming October 11, 2002.
מבוא מורחב - שיעור 5 1 Lecture 5 Higher-order procedures.
Solution of Nonlinear Equations ( Root Finding Problems ) Definitions Classification of Methods  Analytical Solutions  Graphical Methods  Numerical.
MATH342: Numerical Analysis Sunjae Kim.
Lecture #5 מבוא מורחב.
Lecture 16 Streams continue Infinite Streams מבוא מורחב - שיעור 16.
COP 3503 FALL 2012 Shayan Javed Lecture 15
Solution of Nonlinear Equations (Root finding Problems
Big O notation Big O notation is used in Computer Science to describe the performance or complexity of an algorithm. Big O specifically describes the worst-case.
3.7 The Real Zeros of a Polynomial Function
CS21b: Structure and Interpretation
Recursion "To understand recursion, one must first understand recursion." -Stephen Hawking.
3.7 The Real Zeros of a Polynomial Function
Algorithm design and Analysis
Lecture #5 מבוא מורחב.
Important Values for Continuous functions
Lecture #6 section pages pages72-77
Material in the textbook on
Intermediate Value Theorem
6.001 SICP Streams – the lazy way
Intermediate Value Theorem
Continuity Alex Karassev.
Last Class We Covered Recursion Stacks Parts of a recursive function:
Solutions for Nonlinear Equations
Presentation transcript:

מבוא מורחב למדעי המחשב בשפת Scheme תרגול 4

Outline High order procedures –Finding Roots –Compose Functions Accelerating Computations –Fibonacci 2

Finding roots of equations Input: Continuous function f(x) a, b such that f(a)<0<f(b) Goal: Find a root of the equation f(x)=0 Relaxation: Settle with a “close-enough” solution 3

General Binary Search Search space: set of all potential solutions –e.g. every real number in the interval [a b] can be a root Divide search space into halves – e.g. [a (a+b)/2) and [(a+b)/2 b] Identify which half has a solution –e.g. r is in [a (a+b)/2) or r is in [(a+b)/2 b] Find the solution recursively in reduced search space –[a (a+b)/2) Find solution for small search spaces –E.g. if abs(a-b)<e, r=(a+b)/2 4

Back to finding roots Theorem: if f(a)<0<f(b) and f(x) is continuous, then there is a point c  (a,b) such that f(c)=0 –Note: if a>b then the interval is (b,a) Half interval method –Divide (a,b) into 2 halves –If interval is small enough – return middle point –Otherwise, use theorem to select correct half interval –Repeat iteratively 5

Example b a 6

Example (continued) b a And again and again… 7

(define (search f a b) (let ((x (average a b))) (if (close-enough? a b) (let ((y (f x))) (cond ((positive? y) ) ((negative? y) ) (else )))))) x (search f a x) (search f x b) x Scheme implementation 8 Complexity?

Determine positive and negative ends (define (half-interval-method f a b) (let ((fa (f a)) (fb (f b))) (cond ((and ) (search f a b)) ((and ) (search f b a)) (else (display “values are not of opposite signs”))) )) (negative? fa) (positive? fb) (negative? fb) (positive? fa) We need to define (define (close-enough? x y) (< (abs (- x y)) 0.001)) 9

sin(x)=0, x  (2,4) (half-interval-method ) x 3 -2x-3=0, x  (1,2) (half-interval-method ) Examples: sin … (lambda (x) (- (* x x x) (* 2 x) 3))

11 (define (double f) (lambda (x) (f (f x)))) (define (inc x) (+ x 1)) ((double inc) 5) => 7 inc(x) = x + 1 f(f(x)) = x + 2 ((double square) 5) => 625 square(x) = x^2 f(f(x)) = x^4 Double

12 (((double double) inc) 5) => 9 (double double) = double twice ((double double) inc) = (double (double inc)) inc = x + 1 ((double double) inc) = x + 4 (((double double) square) 5) => square = x^2 (double square) = x^4 ((double double) square) = x^16 Double

13 (((double (double double)) inc) 5) => 21 (double double) = double twice (double (double double) = twice, then twice again (4 times) inc = x + 1 ((double (double double)) inc) = x + 16 Double Double Double

14 Compose Compose f(x), g(x) to f(g(x)) (define (compose f g) (lambda (x) (f (g x)))) (define (inc x) (+ x 1)) ((compose inc square) 3)  10 ((compose square inc) 3)  16

15 (= n 1) f (repeated f (- n 1)) f(x), f(f(x)), f(f(f(x))), … apply f, n times (define (repeated f n) (if (compose f ))) ((repeated inc 5) 100) => 105 ((repeated square 2) 5) => 625 Repeated f (define (compose f g) (lambda (x) (f (g x)))) Compose now Execute later

16 (define (repeated f n) (lambda (x) (repeated-iter f n x))) Repeated f - iterative (define (repeated-iter f n x) (if (= n 1) (f x) (repeated-iter f (- n 1) (f x)))) Do nothing until called later

17 Repeated f – Iterative II (define (repeated f n) (define (repeated-iter count accum) (if (= count n) accum (repeated-iter (+ count 1) (compose f accum)))) (repeated-iter 1 f)) Compose now Execute later

18 (define (smooth f) (let ((dx 0.1)) )) (define (average x y z) (/ (+ x y z) 3)) (lambda (x) (average (f (- x dx)) (f x) (f (+ x dx)))) Smooth a function f: g(x) = (f(x – dx) + f(x) + f(x + dx)) / 3 ((repeated smooth n) f) Repeatedly smooth a function (define (repeated-smooth f n) )

Compose Compose f(x), g(x) to f(g(x)) (define (compose f g) (lambda (x) (f (g x)))) (define (inc x) (+ x 1)) ((compose inc square) 3)  10 ((compose square inc) 3)  16 19

Accelerating Computations 20

Iterative Fibonacci (define (fib n) (define (fib-iter a b count) (if (= count 0) b (fib-iter (+ a b) a (- count 1))) (fib-iter 1 0 n)) Computation time:  (n) Much better than Recursive implementation, but… Can we do better? 21

Slow vs Fast Expt Slow (linear) –b 0 =1 –b n =b  b n-1 Fast (logarithmic) –b n =(b 2 ) n/2 if n is even –b n =b  b n-1 if n is odd Can we do the same with Fibonacci? 22

Double Steps Fibonacci Transformation: b a a+b 2a+b 3a+2b … Double Transformation: 23

A Squaring Algorithm If we can square (or multiply) linear transformations, we have an algorithm: –Apply T n on (a,b), where: –T n =(T 2 ) n/2 If n is even –T n =T  T n-1 If n is odd 24

Squaring Transformations General Linear Transformation: Squared: 25

Iterative Algorithm Initialize: Stop condition: If count=0 return b Step count is oddcount is even 26

Representing Transformations We need to remember x, y, z, w Fibonacci Transformations belong to a simpler family: T 01 is the basic Fibonacci transformation Squaring (verify on your own!): 27

Implementation (finally) (define fib n) (fib-iter n)) (define (fib-iter a b p q count) (cond ((= count 0) b) ((even? count) (fib-iter a b (/ count 2) (else (fib-iter p q (- count 1)))) (+ (square p) (square q)) (+ (* 2 p q) (square q)) (+ (* b q) (* a q) (* a p)) (+ (* b p) (* a q)) 28