61A-006-Newton_1pp.pdf

61A Lecture 6
Friday, September 9
Friday, September 9, 2011
Lambda Expressions
2
Friday, September 9, 2011
Lambda Expressions
>>> ten = 10
2
Friday, September 9, 2011
Lambda Expressions
>>> ten = 10
>>> square = x * x
2
Friday, September 9, 2011
Lambda Expressions
>>> ten = 10
An expression: this one
evaluates to a number
>>> square = x * x
2
Friday, September 9, 2011
Lambda Expressions
>>> ten = 10
An expression: this one
evaluates to a number
>>> square = x * x
>>> square = lambda x: x * x
2
Friday, September 9, 2011
Lambda Expressions
>>> ten = 10
An expression: this one
evaluates to a number
>>> square = x * x
Also an expression:
evaluates to a function
>>> square = lambda x: x * x
2
Friday, September 9, 2011
Lambda Expressions
An expression: this one
evaluates to a number
>>> ten = 10
>>> square = x * x
Also an expression:
evaluates to a function
>>> square = lambda x: x * x
A function
2
Friday, September 9, 2011
Lambda Expressions
An expression: this one
evaluates to a number
>>> ten = 10
>>> square = x * x
Also an expression:
evaluates to a function
>>> square = lambda x: x * x
A function
with formal parameter x
2
Friday, September 9, 2011
Lambda Expressions
An expression: this one
evaluates to a number
>>> ten = 10
>>> square = x * x
Also an expression:
evaluates to a function
>>> square = lambda x: x * x
A function
with formal parameter x
and body "return x * x"
2
Friday, September 9, 2011
Lambda Expressions
An expression: this one
evaluates to a number
>>> ten = 10
>>> square = x * x
Also an expression:
evaluates to a function
>>> square = lambda x: x * x
Notice: no "return"
A function
with formal parameter x
and body "return x * x"
2
Friday, September 9, 2011
Lambda Expressions
An expression: this one
evaluates to a number
>>> ten = 10
>>> square = x * x
Also an expression:
evaluates to a function
>>> square = lambda x: x * x
Notice: no "return"
A function
with formal parameter x
and body "return x * x"
Must be a single expression
2
Friday, September 9, 2011
Lambda Expressions
An expression: this one
evaluates to a number
>>> ten = 10
>>> square = x * x
Also an expression:
evaluates to a function
>>> square = lambda x: x * x
Notice: no "return"
A function
with formal parameter x
and body "return x * x"
>>> square(4)
16
Must be a single expression
2
Friday, September 9, 2011
Lambda Expressions
An expression: this one
evaluates to a number
>>> ten = 10
>>> square = x * x
Also an expression:
evaluates to a function
>>> square = lambda x: x * x
Notice: no "return"
A function
with formal parameter x
and body "return x * x"
>>> square(4)
16
Must be a single expression
Lambda expressions are rare in Python, but important in general
2
Friday, September 9, 2011
Lambda Expressions Versus Def Statements
3
Friday, September 9, 2011
Lambda Expressions Versus Def Statements
VS
3
Friday, September 9, 2011
Lambda Expressions Versus Def Statements
square = lambda x: x * x
VS
3
Friday, September 9, 2011
Lambda Expressions Versus Def Statements
square = lambda x: x * x
VS
def square(x):
return x * x
3
Friday, September 9, 2011
Lambda Expressions Versus Def Statements
square = lambda x: x * x
VS
def square(x):
return x * x
• Both create a function with the same arguments & body
3
Friday, September 9, 2011
Lambda Expressions Versus Def Statements
square = lambda x: x * x
VS
def square(x):
return x * x
• Both create a function with the same arguments & body
• Both of those functions are associated with the environment
in which they are defined
3
Friday, September 9, 2011
Lambda Expressions Versus Def Statements
square = lambda x: x * x
VS
def square(x):
return x * x
• Both create a function with the same arguments & body
• Both of those functions are associated with the environment
in which they are defined
• Both bind that function to the name "square"
3
Friday, September 9, 2011
Lambda Expressions Versus Def Statements
square = lambda x: x * x
VS
def square(x):
return x * x
• Both create a function with the same arguments & body
• Both of those functions are associated with the environment
in which they are defined
• Both bind that function to the name "square"
• Only the def statement gives the function an intrinsic name
3
Friday, September 9, 2011
Lambda Expressions Versus Def Statements
square = lambda x: x * x
VS
def square(x):
return x * x
• Both create a function with the same arguments & body
• Both of those functions are associated with the environment
in which they are defined
• Both bind that function to the name "square"
• Only the def statement gives the function an intrinsic name
<lambda>(x):
return x * x
3
Friday, September 9, 2011
Lambda Expressions Versus Def Statements
square = lambda x: x * x
VS
def square(x):
return x * x
• Both create a function with the same arguments & body
• Both of those functions are associated with the environment
in which they are defined
• Both bind that function to the name "square"
• Only the def statement gives the function an intrinsic name
<lambda>(x):
return x * x
square(x):
return x * x
3
Friday, September 9, 2011
Function Currying
4
Friday, September 9, 2011
Function Currying
def make_adder(n):
return lambda k: n + k
4
Friday, September 9, 2011
Function Currying
def make_adder(n):
return lambda k: n + k
>>> make_adder(2)(3)
5
>>> add(2, 3)
5
4
Friday, September 9, 2011
Function Currying
def make_adder(n):
return lambda k: n + k
>>> make_adder(2)(3)
5
>>> add(2, 3)
5
There's a general
relationship between
these functions
4
Friday, September 9, 2011
Function Currying
def make_adder(n):
return lambda k: n + k
>>> make_adder(2)(3)
5
>>> add(2, 3)
5
There's a general
relationship between
these functions
Currying: Transforming a multi-argument function into a
single-argument, higher-order function.
4
Friday, September 9, 2011
Function Currying
def make_adder(n):
return lambda k: n + k
>>> make_adder(2)(3)
5
>>> add(2, 3)
5
There's a general
relationship between
these functions
Currying: Transforming a multi-argument function into a
single-argument, higher-order function.
Fun Fact: Currying was discovered by Moses Schönfinkel and
later re-discovered by Haskell Curry.
4
Friday, September 9, 2011
Function Currying
def make_adder(n):
return lambda k: n + k
>>> make_adder(2)(3)
5
>>> add(2, 3)
5
There's a general
relationship between
these functions
Currying: Transforming a multi-argument function into a
single-argument, higher-order function.
Fun Fact: Currying was discovered by Moses Schönfinkel and
later re-discovered by Haskell Curry.
Schönfinkeling?
4
Friday, September 9, 2011
Newton's Method Background
Finds approximations to zeroes of differentiable functions
5
Friday, September 9, 2011
Newton's Method Background
Finds approximations to zeroes of differentiable functions
y = x2 - 2
5
Friday, September 9, 2011
Newton's Method Background
Finds approximations to zeroes of differentiable functions
2.5
y = x2 - 2
-5
-2.5
0
2.5
5
-2.5
5
Friday, September 9, 2011
Newton's Method Background
Finds approximations to zeroes of differentiable functions
2.5
y = x2 - 2
-5
-2.5
A "zero"
0
2.5
5
-2.5
5
Friday, September 9, 2011
Newton's Method Background
Finds approximations to zeroes of differentiable functions
2.5
y = x2 - 2
-5
-2.5
A "zero"
0
-2.5
2.5
5
x=1.414213562373095
5
Friday, September 9, 2011
Newton's Method Background
Finds approximations to zeroes of differentiable functions
2.5
y = x2 - 2
-5
-2.5
A "zero"
0
-2.5
2.5
5
x=1.414213562373095
Application: a method for (approximately) computing square
roots, using only basic arithmetic.
5
Friday, September 9, 2011
Newton's Method Background
Finds approximations to zeroes of differentiable functions
2.5
y = x2 - 2
-5
-2.5
A "zero"
0
-2.5
2.5
5
x=1.414213562373095
Application: a method for (approximately) computing square
roots, using only basic arithmetic.
The positive zero of y = x2 - a is
5
Friday, September 9, 2011
Newton's Method Background
Finds approximations to zeroes of differentiable functions
2.5
y = x2 - 2
-5
A "zero"
-2.5
0
2.5
5
x=1.414213562373095
-2.5
Application: a method for (approximately) computing square
roots, using only basic arithmetic.
The positive zero of y =
x2
- a is
√
D
5
Friday, September 9, 2011
Newton's Method
Begin with a function f and
an initial guess x
I[)
[−
I
[
6
Friday, September 9, 2011
Newton's Method
Begin with a function f and
an initial guess x
I[)
[−
I
[
6
Friday, September 9, 2011
Newton's Method
Begin with a function f and
an initial guess x
1.
Compute the value of f at guess: f(x)
I[)
[−
I
[
6
Friday, September 9, 2011
Newton's Method
Begin with a function f and
an initial guess x
(x, f(x))
1.
Compute the value of f at guess: f(x)
I[)
[−
I
[
6
Friday, September 9, 2011
Newton's Method
Begin with a function f and
an initial guess x
(x, f(x))
1.
Compute the value of f at guess: f(x)
2.
Compute the derivative of f at guess: f'(x)
I[)
[−
I
[
6
Friday, September 9, 2011
Newton's Method
Begin with a function f and
an initial guess x
(x, f(x))
1.
Compute the value of f at guess: f(x)
2.
Compute the derivative of f at guess: f'(x)
3.
Update guess x to be:
I[)
[−
I
[
6
Friday, September 9, 2011
Newton's Method
Begin with a function f and
an initial guess x
-f(x)
(x, f(x))
1.
Compute the value of f at guess: f(x)
2.
Compute the derivative of f at guess: f'(x)
3.
Update guess x to be:
I[)
[−
I
[
6
Friday, September 9, 2011
Newton's Method
Begin with a function f and
-f(x)/f'(x)
an initial guess x
-f(x)
(x, f(x))
1.
Compute the value of f at guess: f(x)
2.
Compute the derivative of f at guess: f'(x)
3.
Update guess x to be:
I[)
[−
I
[
6
Friday, September 9, 2011
Newton's Method
Begin with a function f and
-f(x)/f'(x)
an initial guess x
-f(x)
(x, f(x))
1.
Compute the value of f at guess: f(x)
2.
Compute the derivative of f at guess: f'(x)
3.
Update guess x to be:
I[)
[−
I
[
6
Friday, September 9, 2011
Visualization of Newton's Method
(Demo)
http://en.wikipedia.org/wiki/File:NewtonIteration_Ani.gif
7
Friday, September 9, 2011
Using Newton's Method
f(x)=x2 - 2
8
Friday, September 9, 2011
Using Newton's Method
How to find the square root of 2?
f(x)=x2 - 2
8
Friday, September 9, 2011
Using Newton's Method
How to find the square root of 2?
>>> f = lambda x: x*x - 2
>>> find_root(f, 1)
f(x)=x2 - 2
8
Friday, September 9, 2011
Using Newton's Method
How to find the square root of 2?
>>> f = lambda x: x*x - 2
>>> find_root(f, 1)
f(x)=x2 - 2
How to find the log base 2 of 1024?
8
Friday, September 9, 2011
Using Newton's Method
How to find the square root of 2?
>>> f = lambda x: x*x - 2
>>> find_root(f, 1)
f(x)=x2 - 2
How to find the log base 2 of 1024?
>>> g = lambda x: pow(2, x) - 1024
>>> find_root(g, 1)
8
Friday, September 9, 2011
Using Newton's Method
How to find the square root of 2?
>>> f = lambda x: x*x - 2
>>> find_root(f, 1)
f(x)=x2 - 2
How to find the log base 2 of 1024?
>>> g = lambda x: pow(2, x) - 1024
>>> find_root(g, 1)
What number is one less than its square?
8
Friday, September 9, 2011
Using Newton's Method
How to find the square root of 2?
>>> f = lambda x: x*x - 2
>>> find_root(f, 1)
f(x)=x2 - 2
How to find the log base 2 of 1024?
>>> g = lambda x: pow(2, x) - 1024
>>> find_root(g, 1)
What number is one less than its square?
>>> h = lambda x: x*x - (x+1)
>>> find_root(h, 1)
8
Friday, September 9, 2011
Special Case: Square Roots
[+
[=
D
[
Babylonian Method
9
Friday, September 9, 2011
Special Case: Square Roots
How to compute square_root(a)
Idea: Iteratively refine a guess x about the square root of a
[+
[=
D
[
Babylonian Method
9
Friday, September 9, 2011
Special Case: Square Roots
How to compute square_root(a)
Idea: Iteratively refine a guess x about the square root of a
Update:
[+
[=
D
[
Babylonian Method
9
Friday, September 9, 2011
Special Case: Square Roots
How to compute square_root(a)
Idea: Iteratively refine a guess x about the square root of a
Update:
[+
[=
D
[
Babylonian Method
What guess should start the computation?
9
Friday, September 9, 2011
Special Case: Square Roots
How to compute square_root(a)
Idea: Iteratively refine a guess x about the square root of a
Update:
[+
[=
D
[
Babylonian Method
What guess should start the computation?
How do we know when we are finished?
9
Friday, September 9, 2011
Special Case: Square Roots
How to compute square_root(a)
Idea: Iteratively refine a guess x about the square root of a
Update:
[+
[=
D
[
Babylonian Method
Implementation questions:
What guess should start the computation?
How do we know when we are finished?
9
Friday, September 9, 2011
Special Case: Square Roots
·[+
[=
D
[
10
Friday, September 9, 2011
Special Case: Square Roots
How to compute cube_root(a)
Idea: Iteratively refine a guess x about the cube root of a
·[+
[=
D
[
10
Friday, September 9, 2011
Special Case: Square Roots
How to compute cube_root(a)
Idea: Iteratively refine a guess x about the cube root of a
·[+
[=
D
[
What guess should start the computation?
10
Friday, September 9, 2011
Special Case: Square Roots
How to compute cube_root(a)
Idea: Iteratively refine a guess x about the cube root of a
·[+
[=
D
[
What guess should start the computation?
How do we know when we are finished?
10
Friday, September 9, 2011
Special Case: Square Roots
How to compute cube_root(a)
Idea: Iteratively refine a guess x about the cube root of a
·[+
[=
D
[
Implementation questions:
What guess should start the computation?
How do we know when we are finished?
10
Friday, September 9, 2011
Special Case: Square Roots
How to compute cube_root(a)
Idea: Iteratively refine a guess x about the cube root of a
Update:
·[+
[=
D
[
Implementation questions:
What guess should start the computation?
How do we know when we are finished?
10
Friday, September 9, 2011
Iterative Improvement
(Demo)
11
Friday, September 9, 2011
>>> cube_root(27)
3.0
"""
x = 1
while pow(x, 3) != a:
x = cube_root_update(x, a)
return x
Iterative Improvement
def cube_root_update(x, a):
return (2*x + a/(x * x))/3
(Demo)
# Iterative improvement
def golden_update(x):
return 1/x + 1
def golden_test(x):
return x * x == x + 1
def iter_improve(update, done, guess=1, max_updates=1000):
"""Iteratively improve guess with update until done returns a true value.
guess éé An initial guess
update éé A function from guesses to guesses; updates the guess
done éé A function from guesses to boolean values; tests if guess is good
>>> iter_improve(golden_update, golden_test)
1.618033988749895
"""
k = 0
while not done(guess) and k < max_updates:
guess = update(guess)
k = k + 1
return guess
def square_root_improve(a):
"""Return the square root of a.
>>> square_root_improve(9)
Friday, September 9, 2011
11
>>> cube_root(27)
3.0
>>> cube_root(27)
"""
3.0
x
= 1
"""
while
3) != a:
guess pow(x,
= 1
x =
cube_root_update(x,
a) == x:
while
not
guess * guess * guess
return
x = cube_root_update(guess, x)
guess
return guess
def cube_root_update(x, a):
(2*x + a/(x * x))/3x): (Demo)
def return
square_root_update(guess,
return average(guess, x / guess)
Iterative Improvement
#
Iterative
improvement
def
cube_root_update(guess,
x):
return (x / (guess * guess) + 2 * guess)/3
def golden_update(x):
1/x + 1
def return
golden_update(guess):
return 1/guess + 1
def golden_test(x):
x * x == x + 1
def return
golden_test(guess):
return guess * guess == guess + 1
def iter_improve(update, done, guess=1, max_updates=1000):
improvedone,
guessguess=1,
with update
until done returns a true value.
def """Iteratively
iter_improve(update,
max_updates=1000):
"""Iteratively improve guess with update until done returns a true value.
guess éé An initial guess
update
éé AA number
function from guesses to guesses; updates the guess
guess éé
done
éé éé
A function
from
guesses
to to
boolean
values; tests if guess is good
update
A function
from
guesses
guesses
done éé A function from guesses to boolean values
>>> iter_improve(golden_update, golden_test)
1.618033988749895
>>> iter_improve(golden_update, golden_test)
"""
1.618033988749895
k
= 0
"""
while
n = 0 not done(guess) and k < max_updates:
guess
update(guess)
while
not =
done(guess)
and n < max_updates:
k
= k +
guess
= 1update(guess)
return
n =guess
n + 1
return guess
def square_root_improve(a):
the square root of a.
11
def """Return
square_root_improve(x):
"""Return the square root of x.
>>>
Friday, September 9, 2011 square_root_improve(9)
"""
>>> cube_root(27)
guess = 1
3.0
>>> cube_root(27)
while not guess * guess * guess == x:
"""
3.0
guess = cube_root_update(guess, x)
x
= 1
"""
return guess
while
3) != a:
guess pow(x,
= 1
x =
cube_root_update(x,
a) == x:
while
not
guess * guess * guess
def square_root_update(guess,
x):
return
x = cube_root_update(guess,
guess
x)
return average(guess, x / guess)
return guess
def cube_root_update(x, a):
(Demo)
def
x):
(2*x + a/(x * x))/3
def return
square_root_update(guess,
x): cube_root_update(guess,
return (x / (guess * guess) + 2 * guess)/3
return average(guess, x / guess)
Iterative Improvement
def golden_update(guess):
#
Iterative
improvement
def
cube_root_update(guess,
x):
1/guess + 1
return (x / (guess * guess) + return
2 * guess)/3
def golden_update(x):
1/x + 1
def golden_test(guess):
def return
golden_update(guess):
return guess * guess == guess + 1
return 1/guess + 1
def golden_test(x):
x * x == x + 1
def iter_improve(update, done, guess=1, max_updates=
def return
golden_test(guess):
improve guess with update until do
return guess * guess == guess """Iteratively
+ 1
def iter_improve(update, done, guess=1, max_updates=1000):
improvedone,
guessguess=1,
with
until done returns a true value.
guessupdate
éé
A number
def """Iteratively
iter_improve(update,
max_updates=1000):
update
éé A until
function
guesses
to guesses
"""Iteratively improve guess with
update
donefrom
returns
a true
value.
guess éé An initial guess
done éé A function from guesses to boolean values
update
éé AA number
function from guesses to guesses; updates the guess
guess éé
done
éé éé
A function
from
guesses
boolean
values; tests if guess
is good
>>>toiter_improve(golden_update,
golden_test)
update
A function
from
guesses
to
guesses
1.618033988749895
done éé A function from guesses
to boolean values
>>> iter_improve(golden_update,
golden_test)
"""
1.618033988749895
n =golden_test)
0
>>> iter_improve(golden_update,
"""
while not done(guess) and n < max_updates:
1.618033988749895
k
= 0
guess = update(guess)
"""
while
n = n + 1
n = 0 not done(guess) and k < max_updates:
guess
update(guess)
guess
while
not =
done(guess)
and n < return
max_updates:
k
= k +
guess
= 1update(guess)
return
def square_root_improve(x):
n =guess
n + 1
"""Return the square root of x.
return guess
def square_root_improve(a):
the square root of a.
>>> square_root_improve(9)
11
def """Return
square_root_improve(x):
3.0
"""Return the square root of x.
"""
Friday, September>>>
9, 2011 square_root_improve(9)
Iterative Improvement
golden_update:
golden_test:
iter_improve:
golden_update
golden_test
iter_improve
12
Friday, September 9, 2011
Iterative Improvement
golden_update:
golden_test:
iter_improve:
golden_update
golden_test
iter_improve
update:
done:
guess: 1
iter_improve
12
Friday, September 9, 2011
Iterative Improvement
golden_update:
golden_test:
iter_improve:
golden_update
golden_test
iter_improve
update:
done:
guess: 1
iter_improve
guess: 1
golden_test
12
Friday, September 9, 2011
Iterative Improvement
golden_update:
golden_update
golden_test:
golden_test
iter_improve:
iter_improve
update:
done:
guess: 1
iter_improve
guess: 1
golden_test
guess:
12
Friday, September 9, 2011
Iterative Improvement
golden_update:
golden_update
golden_test:
golden_test
iter_improve:
iter_improve
1
update:
done:
guess: 1
iter_improve
guess: 1
golden_test
guess:
12
Friday, September 9, 2011
Iterative Improvement
golden_update:
golden_update
golden_test:
golden_test
iter_improve:
iter_improve
1
update:
done:
guess: 1
iter_improve
2.0
guess: 1
golden_test
guess:
12
Friday, September 9, 2011
Iterative Improvement
golden_update:
golden_update
golden_test:
golden_test
iter_improve:
iter_improve
1
update:
done:
guess: 1
iter_improve
2.0
1.5
guess: 1
golden_test
guess:
12
Friday, September 9, 2011
Iterative Improvement
golden_update:
golden_update
golden_test:
golden_test
iter_improve:
iter_improve
1
update:
done:
guess: 1
iter_improve
2.0
1.5
1.6666666666666665
guess: 1
golden_test
guess:
12
Friday, September 9, 2011
Iterative Improvement
golden_update:
golden_update
golden_test:
golden_test
iter_improve:
iter_improve
1
update:
done:
guess: 1
iter_improve
2.0
1.5
1.6666666666666665
guess: 1
1.6
golden_test
guess:
12
Friday, September 9, 2011
Iterative Improvement
golden_update:
golden_update
golden_test:
golden_test
iter_improve:
iter_improve
1
update:
done:
guess: 1
iter_improve
2.0
1.5
1.6666666666666665
guess: 1
1.6
golden_test
guess:
1.625
12
Friday, September 9, 2011
Iterative Improvement
golden_update:
golden_update
golden_test:
golden_test
iter_improve:
iter_improve
1
update:
done:
guess: 1
iter_improve
2.0
1.5
1.6666666666666665
guess: 1
1.6
golden_test
guess:
1.625
1.6153846153846154
12
Friday, September 9, 2011
Iterative Improvement
golden_update:
golden_update
golden_test:
golden_test
iter_improve:
iter_improve
1
update:
done:
guess: 1
iter_improve
2.0
1.5
1.6666666666666665
guess: 1
1.6
golden_test
guess:
1.625
1.6153846153846154
1.619047619047619
12
Friday, September 9, 2011
Iterative Improvement
golden_update:
golden_update
golden_test:
golden_test
iter_improve:
iter_improve
1
update:
done:
guess: 1
iter_improve
2.0
1.5
1.6666666666666665
guess: 1
1.6
golden_test
guess:
1.625
1.6153846153846154
1.619047619047619
1.6176470588235294
12
Friday, September 9, 2011
Iterative Improvement
golden_update:
golden_update
golden_test:
golden_test
iter_improve:
iter_improve
1
update:
done:
guess: 1
iter_improve
2.0
1.5
1.6666666666666665
guess: 1
1.6
golden_test
guess:
1.625
1.6153846153846154
1.619047619047619
1.6176470588235294
1.6181818181818182
12
Friday, September 9, 2011
Square Roots by Iterative Improvement
(Demo)
13
Friday, September 9, 2011
Square Roots by Iterative Improvement
square_root:
square_root
(Demo)
iter_improve:
...
iter_improve
13
Friday, September 9, 2011
Square Roots by Iterative Improvement
square_root:
square_root
(Demo)
iter_improve:
...
iter_improve
square_root(256)
13
Friday, September 9, 2011
Square Roots by Iterative Improvement
square_root:
square_root
(Demo)
iter_improve:
...
iter_improve
square_root(256)
13
Friday, September 9, 2011
Square Roots by Iterative Improvement
square_root:
square_root
(Demo)
iter_improve:
iter_improve
...
square_root(256)
square_root(256)
13
Friday, September 9, 2011
Square Roots by Iterative Improvement
square_root:
square_root
(Demo)
iter_improve:
iter_improve
...
a: 256
square_root
square_root(256)
square_root(256)
13
Friday, September 9, 2011
Square Roots by Iterative Improvement
square_root:
square_root
(Demo)
iter_improve:
iter_improve
...
a: 256
square_root
square_root(256)
def update(guess):
...
...
return iter_improve(...)
square_root(256)
13
Friday, September 9, 2011
Square Roots by Iterative Improvement
square_root:
square_root
(Demo)
iter_improve:
iter_improve
...
a: 256
square_root
square_root(256)
def update(guess):
...
...
return iter_improve(...)
square_root(256)
13
Friday, September 9, 2011
Square Roots by Iterative Improvement
square_root:
(Demo)
square_root
iter_improve:
iter_improve
...
a: 256
update:
update(guess):
square_root
return ...
square_root(256)
def update(guess):
...
...
return iter_improve(...)
square_root(256)
13
Friday, September 9, 2011
Square Roots by Iterative Improvement
square_root:
(Demo)
square_root
iter_improve:
iter_improve
...
a: 256
update:
done: ...
update(guess):
square_root
return ...
square_root(256)
def update(guess):
...
...
return iter_improve(...)
square_root(256)
13
Friday, September 9, 2011
Square Roots by Iterative Improvement
square_root:
(Demo)
square_root
iter_improve:
iter_improve
...
a: 256
update:
done: ...
update(guess):
square_root
square_root(256)
...
return iter_improve(...)
return ...
square_root(256)
14
Friday, September 9, 2011
Square Roots by Iterative Improvement
square_root:
(Demo)
square_root
iter_improve:
iter_improve
...
a: 256
update:
done: ...
square_root
guess: 1
update:
...
update(guess):
return ...
iter_improve
square_root(256)
...
return iter_improve(...)
square_root(256)
14
Friday, September 9, 2011
Square Roots by Iterative Improvement
square_root:
(Demo)
square_root
iter_improve:
iter_improve
...
a: 256
update:
done: ...
square_root
guess:1
guess: 1
update:
...
update(guess):
return ...
iter_improve
update
square_root(256)
...
return iter_improve(...)
square_root(256)
14
Friday, September 9, 2011
Derivatives of Single-Argument Functions
I[KI[
I
[ = lim
K
K →
15
Friday, September 9, 2011
Derivatives of Single-Argument Functions
3
I[KI[
I
[ = lim
K
K →
2
1
-4
-3
-2
-1
0
1
2
3
4
5
-1
-2
-3
15
Friday, September 9, 2011
Derivatives of Single-Argument Functions
3
I[KI[
I
[ = lim
K
K →
2
1
-4
-3
-2
-1
0
1
2
3
4
5
-1
x = 1
-2
-3
15
Friday, September 9, 2011
Derivatives of Single-Argument Functions
3
I[KI[
I
[ = lim
K
K →
2
1
-4
-3
-2
-1
0
1
2
3
4
5
x + h = 1.1
-1
x = 1
-2
-3
15
Friday, September 9, 2011
Derivatives of Single-Argument Functions
3
I[KI[
I
[ = lim
K
K →
2
1
-4
-3
-2
-1
0
1
2
3
4
5
x + h = 1.1
-1
x = 1
-2
-3
15
Friday, September 9, 2011
Derivatives of Single-Argument Functions
3
I[KI[
I
[ = lim
K
K →
2
1
-4
-3
-2
-1
0
1
2
3
4
5
x + h = 1.1
-1
x = 1
-2
-3
(Demo)
http://en.wikipedia.org/wiki/File:Graph_of_sliding_derivative_line.gif
15
Friday, September 9, 2011
Approximating Derivatives
(Demo)
16
Friday, September 9, 2011
Implementing Newton's Method
17
Friday, September 9, 2011
return find_root(lambda y: y * y * y é x)
def approx_derivative(f, x, delta=1eé5):
"""Return an approximation to the derivative of f at x."""
Implementing
Newton's
Method
df = f(x + delta)
é f(x)
return df/delta
def newton_update(f):
"""Return an update function for f using Newton's method."""
def update(x):
return x é f(x) / approx_derivative(f, x)
return update
def find_root(f, guess=1):
"""Return a guess of a zero of the function f, near guess.
>>> from math import sin
>>> find_root(lambda y: sin(y), 3)
3.141592653589793
"""
return iter_improve(newton_update(f), lambda x: f(x) == 0, guess)
@main
def run():
interact()
17
Friday, September 9, 2011
return find_root(lambda y: y * y * y é x)
def approx_derivative(f, x, delta=1eé5):
"""Return an approximation to the derivative of f at x."""
Implementing
Newton's
Method
df = f(x + delta)
é f(x)
return df/delta
def newton_update(f):
"""Return an update function for f using Newton's method."""
def update(x):
return x é f(x) / approx_derivative(f, x)
return update
Could be replaced with
def find_root(f, guess=1):
derivative
"""Return a guess of a the
zeroexact
of the
function f, near guess.
>>> from math import sin
>>> find_root(lambda y: sin(y), 3)
3.141592653589793
"""
return iter_improve(newton_update(f), lambda x: f(x) == 0, guess)
@main
def run():
interact()
17
Friday, September 9, 2011
return find_root(lambda y: y * y * y é x)
"""Return the square root of x.
def approx_derivative(f, x, delta=1eé5):
>>> square_root_newton(9)
"""Return
an approximation to the derivative of f at x."""
3.0
Implementing
Newton's
Method
df = f(x + delta)
é f(x)
"""
return
df/delta
return find_root(lambda y: y * y é x)
def newton_update(f):
def """Return
cube_root_newton(x):
an update function for f using Newton's method."""
"""Return
the cube root of x.
def update(x):
return x é f(x) / approx_derivative(f, x)
>>>
cube_root_newton(27)
return
update
3.0
Could be replaced with
"""
def find_root(f,
guess=1): y: y * y * y é x)
return find_root(lambda
derivative
"""Return a guess of a the
zeroexact
of the
function f, near guess.
def approx_derivative(f, x, delta=1eé5):
>>>
from math
import sin
"""Return
an approximation
to the derivative of f at x."""
>>>
find_root(lambda
y:
sin(y),
3)
df = f(x + delta) é f(x)
3.141592653589793
return df/delta
"""
iter_improve(newton_update(f), lambda x: f(x) == 0, guess)
def return
newton_update(f):
"""Return an update function for f using Newton's method."""
@maindef update(x):
def run():
return x é f(x) / approx_derivative(f, x)
interact()
return update
def find_root(f, guess=1):
"""Return a guess of a zero of the function f, near guess.
>>> from math import sin
>>> find_root(lambda y: sin(y), 3)
3.141592653589793
"""
17
return iter_improve(newton_update(f), lambda x: f(x) == 0, guess)
Friday, September 9, 2011
return find_root(lambda y: y * y * y é x)
"""Return the square root of x.
def approx_derivative(f, x, delta=1eé5):
>>> square_root_newton(9)
"""Return
an approximation to the derivative of f at x."""
3.0
Implementing
Newton's
Method
df = f(x + delta)
é f(x)
"""
return
df/delta
return find_root(lambda y: y * y é x)
def newton_update(f):
def """Return
cube_root_newton(x):
an update function for f using Newton's method."""
"""Return
the cube root of x.
def update(x):
return x é f(x) / approx_derivative(f, x)
>>>
cube_root_newton(27)
return
update
3.0
Could be replaced with
"""
def find_root(f,
guess=1): y: y * y * y é x)
return find_root(lambda
derivative
"""Return a guess of a the
zeroexact
of the
function f, near guess.
def approx_derivative(f, x, delta=1eé5):
>>>
from math
import sin
"""Return
an approximation
to the derivative of f at x."""
>>>
find_root(lambda
y:
sin(y),
3)
df = f(x + delta) é f(x)
3.141592653589793
return df/delta
"""
Limit approximated
iter_improve(newton_update(f), lambda x: f(x) == 0, guess)
def return
newton_update(f):
by a small value
"""Return an update function for f using Newton's method."""
@maindef update(x):
def run():
return x é f(x) / approx_derivative(f, x)
interact()
return update
def find_root(f, guess=1):
"""Return a guess of a zero of the function f, near guess.
>>> from math import sin
>>> find_root(lambda y: sin(y), 3)
3.141592653589793
"""
17
return iter_improve(newton_update(f), lambda x: f(x) == 0, guess)
Friday, September 9, 2011
return find_root(lambda y: y * y * y é x)
"""Return the square root of x.
"""
find_root(lambda
y * y é x)
def return
approx_derivative(f,
x, y:
delta=1eé5):
>>> square_root_newton(9)
"""Return
an approximation to the derivative of f at x."""
3.0
def
cube_root_newton(x):
Implementing
Newton's
Method
df = f(x + delta)
é f(x)
"""
"""Return
the cube root of x.
return
df/delta
return find_root(lambda y: y * y é x)
cube_root_newton(27)
def >>>
newton_update(f):
def 3.0
cube_root_newton(x):
"""Return
an update function for f using Newton's method."""
"""Return
the cube root of x.
"""
def update(x):
return
find_root(lambda
y: y * y * y é x) x)
return
x é f(x) / approx_derivative(f,
>>> cube_root_newton(27)
return
update
3.0
def approx_derivative(f,
x, delta=1eé5):
Could to
be the
replaced
with of f at x."""
"""
"""Return
an
approximation
derivative
def find_root(f,
guess=1): y: y * y * y é x)
return
find_root(lambda
derivative
df
=
f(x
+
delta)
"""Return a guess é
off(x)
a the
zeroexact
of the
function f, near guess.
return df/delta
def approx_derivative(f, x, delta=1eé5):
>>>
from math
import sin
"""Return
an approximation
to the derivative of f at x."""
def newton_update(f):
>>>
find_root(lambda
y:
sin(y),
3)
df = f(x +andelta)
é function
f(x)
"""Return
update
for
f using Newton's method."""
3.141592653589793
return df/delta
def
""" update(x):
Limit
approximated
return
x
é
f(x)
/
approx_derivative(f,
x)x: f(x) == 0, guess)
return
iter_improve(newton_update(f),
lambda
def return
newton_update(f):
by a small value
update
"""Return an update function for f using Newton's method."""
@maindef update(x):
def
guess=1):
def find_root(f,
run():
returna xguess
é f(x)
"""Return
of /a approx_derivative(f,
zero of the function x)
f, near guess.
interact()
return update
>>> from math import sin
def >>>
find_root(f,
guess=1):
find_root(lambda
y: sin(y), 3)
"""Return a guess of a zero of the function f, near guess.
3.141592653589793
"""
>>> from
math import sin
return
iter_improve(newton_update(f),
lambda x: f(x) == 0, guess)
>>> find_root(lambda y: sin(y), 3)
@main3.141592653589793
"""
def run():
17
return iter_improve(newton_update(f), lambda x: f(x) == 0, guess)
interact()
Friday, September 9, 2011
return find_root(lambda y: y * y * y é x)
"""Return the square root of x.
"""
find_root(lambda
y * y é x)
def return
approx_derivative(f,
x, y:
delta=1eé5):
>>> square_root_newton(9)
"""Return
an approximation to the derivative of f at x."""
3.0
def
cube_root_newton(x):
Implementing
Newton's
Method
df = f(x + delta)
é f(x)
"""
"""Return
the cube root of x.
return
df/delta
return find_root(lambda y: y * y é x)
cube_root_newton(27)
def >>>
newton_update(f):
def 3.0
cube_root_newton(x):
"""Return
an update function for f using Newton's method."""
"""Return
the cube root of x.
"""
def update(x):
return
find_root(lambda
y: y * y * y é x) x)
return
x é f(x) / approx_derivative(f,
>>> cube_root_newton(27)
return
update
3.0
def approx_derivative(f,
x, delta=1eé5):
Could to
be the
replaced
with of f at x."""
"""
"""Return
an
approximation
derivative
def find_root(f,
guess=1): y: y * y * y é x)
return
find_root(lambda
derivative
df
=
f(x
+
delta)
"""Return a guess é
off(x)
a the
zeroexact
of the
function f, near guess.
return df/delta
def approx_derivative(f, x, delta=1eé5):
>>>
from math
import sin
"""Return
an approximation
to the derivative of f at x."""
def newton_update(f):
>>>
find_root(lambda
y:
sin(y),
3)
df = f(x +andelta)
é function
f(x)
"""Return
update
for
f using Newton's method."""
3.141592653589793
return df/delta
def
""" update(x):
Limit
approximated
return
x
é
f(x)
/
approx_derivative(f,
x)x: f(x) == 0, guess)
return
iter_improve(newton_update(f),
lambda
def return
newton_update(f):
by a small value
update
"""Return an update function for f using Newton's method."""
@maindef update(x):
def
guess=1):
def find_root(f,
run():
returna xguess
é f(x)
"""Return
of /a approx_derivative(f,
zero of the function x)
f, near guess.
interact()
return update
>>> from math import sin
Definition of a
def >>>
find_root(f,
guess=1):
find_root(lambda
y: sin(y), 3)
function
"""Return a guess of a zero of the function
f, nearzero
guess.
3.141592653589793
"""
>>> from
math import sin
return
iter_improve(newton_update(f),
lambda x: f(x) == 0, guess)
>>> find_root(lambda y: sin(y), 3)
@main3.141592653589793
"""
def run():
17
return iter_improve(newton_update(f), lambda x: f(x) == 0, guess)
interact()
Friday, September 9, 2011