Computing Square Roots

Slides:



Advertisements
Similar presentations
Classes & Objects INTRODUCTION : This chapter introduces classes ; explains data hiding, abstraction & encapsulation and shows how a class implements these.
Advertisements

Chapter 4 Methods F Introducing Methods –Benefits of methods, Declaring Methods, and Calling Methods F Passing Parameters –Pass by Value F Overloading.
Methods Java 5.1 A quick overview of methods
1 Chapter Six Algorithms. 2 Algorithms An algorithm is an abstract strategy for solving a problem and is often expressed in English A function is the.
מבוא מורחב 1 Lecture #6. מבוא מורחב 2 Primality testing (application) Know how to test whether n is prime in  (log n) time => Can easily find very large.
PPL Lecture 3 Slides by Dr. Daniel Deutch, based on lecture notes by Prof. Mira Balaban.
Fall 2008Programming Development Techniques 1 Topic 2 Scheme and Procedures and Processes September 2008.
Chapter 6: User-Defined Functions I
Parametric Inference.
מבוא מורחב 1 Lecture 3 Material in the textbook Sections to
מבוא מורחב - שיעור 2 1 Lecture 2 - Substitution Model (continued) - Recursion - Block structure and scope (if time permits)
1 Extended Introduction to Computer Science 2 Administration סגל הקורס: –מרצים: ד"ר דניאל דויטש, איל כהן –מתרגלת:לבנת ג'רבי –בודק: ינון פלד Book: Structure.
מבוא מורחב 1 Your turn (Review) What does a lambda expression return when it is evaluated? the value of a lambda expression is a procedure What three things.
מבוא מורחב 1 Review: scheme language things that make up scheme programs: self-evaluating 23, "hello", #t names +, pi combinations (+ 2 3) (* pi 4) special.
Loop Application: Numerical Methods, Part 1 The power of Matlab Mathematics + Coding.
Chapter 4: Subprograms Functions for Problem Solving Mr. Dave Clausen La Cañada High School.
1 The Evaluator. 2 Compiler vs. Interpreter Command Processing Unit The Computer Program in Low Level Machine Language Program in High Level Language.
C. Varela; Adapted w. permission from S. Haridi and P. Van Roy1 Functional Programming: Lists, Pattern Matching, Recursive Programming (CTM Sections ,
Abstraction: Procedures as Parameters CMSC Introduction to Computer Programming October 14, 2002.
CSI 3125, Subprograms, page 1 Subprograms General concepts Parameter passing Functions Subprograms as parameters.
Basic Scheme February 8, 2007 Compound expressions Rules of evaluation Creating procedures by capturing common patterns.
CS220 Programming Principles 프로그래밍의 이해 2003 가을학기 Class 2 한 태숙.
1 MODULAR DESIGN AND ABSTRACTION. 2 SPECIFYING THE DETAILS OF A PROBLEM INTO A RELATED SET OF SMALLER PROBLEMS.
1 Lecture 14: Assignment and the Environment Model (EM)
Chapter 3 : Top Down Design with Functions By Suraya Alias.
1/33 Basic Scheme February 8, 2007 Compound expressions Rules of evaluation Creating procedures by capturing common patterns.
Scope: What’s in a Name? CMSC Introduction to Computer Programming October 16, 2002.
4.8 Newton’s Method Mon Nov 9 Do Now Find the equation of a tangent line to f(x) = x^5 – x – 1 at x = 1.
Computer Science 112 Fundamentals of Programming II.
160 as a product of its prime factors is 2 5 x 5 Use this information to show that 160 has 12 factors.
Dale Roberts CSCI N305 Functions Declarations Department of Computer and Information Science, School of Science, IUPUI.
FUNCTIONS (C) KHAERONI, M.SI. OBJECTIVE After this topic, students will be able to understand basic concept of user defined function in C++ to declare.
Higher-Order Programming: Iterative computation (CTM Section 3
Lecture #5 מבוא מורחב.
Basic Scheme February 8, 2007 Compound expressions Rules of evaluation
Functional Programming: Lists, Pattern Matching, Recursive Programming (CTM Sections , 3.2, , 4.7.2) Carlos Varela RPI September 12,
Library Functions Goals of software engineering reliable code
Higher-Order Programming: Iterative computation (CTM Section 3
CMPT 201 Functions.
CSCI 161: Introduction to Programming Function
CS21b: Structure and Interpretation
User-Defined Functions
6.001 SICP Data abstractions
Higher-Order Procedures
Your turn (Review) What does a lambda expression return when it is evaluated? the value of a lambda expression is a procedure What three things are in.
Lecture #5 מבוא מורחב.
CS220 Programming Principles
Lecture #6 section pages pages72-77
Lecture #6 מבוא מורחב.
What would be our focus ? Geometry deals with Declarative or “What is” knowledge. Computer Science deals with Imperative or “How to” knowledge 12/25/2018.
Separating Definition & Implementation
Lecture 12: Message passing The Environment Model
Lecture 13 - Assignment and the environments model Chapter 3
Chapter 6: User-Defined Functions I
What would be our focus ? Geometry deals with Declarative or “What is” knowledge. Computer Science deals with Imperative or “How to” knowledge 2/23/2019.
Material in the textbook Sections to 1.2.1
Lecture #6 section pages pages72-77
Lecture 5 Higher-order procedures מבוא מורחב - שיעור 5.
3.8 Newton’s Method How do you find a root of the following function without a graphing calculator? This is what Newton did.
CS149D Elements of Computer Science
Today’s topics Abstractions Procedural Data
Lecture 2 מבוא מורחב.
Predefined Functions Revisited
Lecture 13: Assignment and the Environment Model (EM)
Introduction to the Lab
Lecture 2 מבוא מורחב.
Algebra 1 Section 2.3.
Good programming practices
Subprograms General concepts Parameter passing Functions
Presentation transcript:

Computing Square Roots = the y such that and y2=x The mathematical definition does not describe a procedure. Declarative : describing properties of things Imperative: describing how to do things (define (sqrt x) (the y (and (>= y 0) (= (square y) x))))

Newton’s Method Successive approximation Guess y for the square root of a number x Get a better guess by averaging y with x/y Guess Quotient Average 1 2/1=2 (1+2)/2=1.5 1.5 2/1.5=1.3333 (1.5+1.3333)/2=1.4167 1.4167 2/1.4167=1.4118 (1.4167+1.4118)=1.4142 1.4142 2/1.4142=...

(define (sqrt-iter guess x) (if (good-enough? guess x) guess (sqrt-iter (improve guess x) x))) (define (improve guess x) (average guess (/ x guess))) (define (average x y) (/ (+ x y) 2)) (define (good-enough? guess x) (< (abs (- (square guess) x)) 0.001)) (define (sqrt x) (sqrt-iter 1.0 x))

Decomposition of sqrt sqrt Sqrt-iter improve Good-enough? square abs average

Procedural Abstraction Procedures as “black boxes” Each procedure accomplishes an identifiable task. The details can be suppressed at the moment. ;; implementation #1 (define (square x) (* x x)) ;; implementation #2 (define (square x) (exp (double (log x)))) (define (double x) (+ x x))

Formal Parameters The meaning of a procedure should be independent of the parameter names (bound variables) used. The meaning of a procedure definition is unchanged if a bound variable is consistently renamed throughout the definition. If a variable is not bound, we say that it is free. In a procedure definition, the formal parameters have the body of the procedure as their scope. (define (square x) (* x x)) (define (square y) (* y y))