lecture8NumericalRepresentationE7

# lecture8NumericalRepresentationE7 - E7 Spring 2010 Bad...

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

E7 Spring 2010: Bad Stuff (lec 8) Raja Sengupta College of Engineering University of California Berkeley Todays mood by me

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

View Full Document
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
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!

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

View Full Document
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?
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

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

View Full Document
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
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)

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

View Full Document
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
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

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

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

## This note was uploaded on 02/19/2010 for the course ENGINEERIN 72826 taught by Professor Sengupta/leachman/johnson during the Spring '10 term at Berkeley.

### Page1 / 39

lecture8NumericalRepresentationE7 - E7 Spring 2010 Bad...

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

View Full Document
Ask a homework question - tutors are online