General Computer Science for Engineers CISC 106 Lecture 08 James Atlas Computer and Information Sciences 9/21/2009
Objectives Use Recursion to Solve Problems Understand collections of data in Matlab Arrays/Matrices
Recursive Function Function that refers to itself Example: sum numbers 1 to n function out=sum(n) if n < 1 out = 0; else out = n + sum(n - 1); Termination Condition Simplify Problem, Assemble Results
Fibonacci Sequence F n = F n-1 + F n-2 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89,... each number is the sum of the two previous numbers
Fibonacci Sequence F n = F n-1 + F n-2 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89,...
Newton’s Method
x n+1 = x n - f(x) / f’(x) x n+1 = x n - (x n 2 - r) / 2x n
Arrays
Arrays
Arrays Fundamental unit of data in MATLAB Collection of data into rows and columns (MATrix LABoratory) 1 dimensional, vector 2 dimensional, matrix 0 dimensional, scalar (a 1x1 array)
Arrays
Array commands a = [ ] b = [1; 2; 3; 4] c = [1 2; 3] (error) d = [1 2; 3 4] f = d(1,2) g(4,5) = 7 a(2:end) d’
Arrays continued zeros(), ones(), eye() scalar vs. array arithmetic +, -, * (.*), / (./) ^ etc.