lecture8NumericalRepresentationE7

lecture8NumericalRepresentationE7 - E7 Spring 2010: Bad...

Info iconThis preview shows pages 1–10. Sign up to view the full content.

View Full Document Right Arrow Icon
E7 Spring 2010: Bad Stuff (lec 8) Raja Sengupta College of Engineering University of California Berkeley Todays mood by me
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
E7: Programming for Engineers Convert the engineering problem into the problem of calculating a function on some input – Use physics, Use mathematics – The function may have a recursive definition Programming: Write the function in a language understood by the computer (MATLAB) – Arithmetic, logical, boolean operations – If-elseif-else – Recursion, Iteration Run the program on the data
Background image of page 2
function out = fibIter (n) if gt(n,2) out1 = 1; out2 = 1; for counter = 3:1:n out = out2 + out1; out1 = out2; out2 = out; end else out = 1; end end Math: fib(n) = fib(n-1)+fib(n-2) if n>2 = 1 if n=1 or n=2 function out = fib(n) if gt(n,2) out = fib(n-1) + fib(n-2); elseif or(eq(n,1),eq(n,2)) out = 1; end end Some recursive definitions are directly programmable These are good definitions!
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Some are not directly programmable Bad Example 1: Consider the following definitions. The domain is the Naturals. func(k) = 2func(k-1) + gunc(k) if k > 1 = 0, if k = 1 gunc(k) = hunc(k)/2 hunc(k) = -4*func(k-1) + 2*func(k) Is there a function satisfying this recursive definition?
Background image of page 4
The program %The input of func should be a natural number function out = func(k) if gt(k,1) out = 2*func(k-1)+gunc(k); else out = 1; end end function out = gunc(k) gunc(k) = hunc(k)/2; end function out = hunc(k) hunc(k) = -4*func(k-1)+2*func(k); end
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
The pitfalls of recursive definition There may be many functions satisfying a recursive definition (Bad example 1) – Which one am I supposed to program? There may be only one function satisfying a recursive definition but the definition still may not be directly programmable (Bad example 2) – Basically, if the definition of func(k) relies on func(k) we cannot program it directly
Background image of page 6
Bad Example 2: This recursive definition is satisfied by only one function of type Naturals Naturals. It still cannot be programmed directly Consider the following definitions. The domain is the Naturals. func(k) = 2func(k-1) + gunc(k) if k > 1 = 1, if k = 1 gunc(k) = hunc(k)/2 hunc(k) = -4*func(k-1) - 2*func(k)
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This recursive definition is satisfied by only one function of type Naturals Naturals. It still cannot be programmed directly Consider the following definitions. The domain is the Naturals. func(k) = 2func(k-1) + gunc(k) if k > 1 = 1, if k = 1 gunc(k) = hunc(k)/2 hunc(k) = -4*func(k-1) - 2*func(k) The unique solution is func(k) = 0 if k > 1 func(k) = 1 if k = 1 One has to find the unique solution and then program it
Background image of page 8
An inductive definition (A special kind of recursive definition - good) General form of an inductive definition func(n) = g(func(n-1)) if n > 1 = a if n = 1 where g is some function of type X X , func is of type Naturals X and a is an element of the Naturals Example: func(n) = n*func(n-1) if n > 1 = 1 if n=1 – Defines the factorial function Example func(x,n) = x*func(x,n-1) if n > 1 = x if n = 1 – Defines the function func(x,n) = x^n
Background image of page 9

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 10
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 39

lecture8NumericalRepresentationE7 - E7 Spring 2010: Bad...

This preview shows document pages 1 - 10. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online