0% found this document useful (0 votes)
22 views

Lecture Notes 5

This document summarizes a math lecture that discusses recursion, matrices, and solving systems of linear equations. It begins with an overview of recursion and examples of computing factorials and Fibonacci numbers recursively. It then covers basic matrix operations in MATLAB like creating, accessing, and manipulating matrices. Finally, it discusses solving systems of linear equations Ax=b using inverse, left division, and matrix factorizations. It provides exercises for students to practice these concepts.

Uploaded by

manikandan
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views

Lecture Notes 5

This document summarizes a math lecture that discusses recursion, matrices, and solving systems of linear equations. It begins with an overview of recursion and examples of computing factorials and Fibonacci numbers recursively. It then covers basic matrix operations in MATLAB like creating, accessing, and manipulating matrices. Finally, it discusses solving systems of linear equations Ax=b using inverse, left division, and matrix factorizations. It provides exercises for students to practice these concepts.

Uploaded by

manikandan
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 20

Lecture 5: Recursion and Matrices

Math 98, Spring 2019

Math 98, Spring 2019 Lecture 5: Recursion and Matrices 1 / 20


Reminders

Instructor: Eric Hallman


Login: !cmfmath98
Password: c@1analog
Class Website: https://math.berkeley.edu/~ehallman/98-sp19/
Assignment Submission: https://bcourses.berkeley.edu
Homework 4:
1 Due Feb 21 by 11:59pm on bCourses
2 Collaboration welcome

Math 98, Spring 2019 Lecture 5: Recursion and Matrices 2 / 20


HW4 Pointers

See the sample programs for examples of most of the commands used
in making the plots.
[mx, idx] = max(v)...
’MarkerIndices’
(please submit .pdf or image of graph rather than .fig)

Math 98, Spring 2019 Lecture 5: Recursion and Matrices 3 / 20


Recursion

How do we compute the factorial of a number?


(
1 n == 0
n! =
n × (n − 1)! n > 0

A for loop will do nicely.

function nfac = myFactorial(n)


nfac = 1;
for i = 1:n
nfac = nfac * i;
end
end

Math 98, Spring 2019 Lecture 5: Recursion and Matrices 4 / 20


Recursion
How do we compute the factorial of a number?
(
1 n == 0
n! =
n × (n − 1)! n > 0

We can also take advantage of the recursive definition, and define our
function recursively:

function nfac = myFactorial(n)


if n == 0
nfac = 1;
else
nfac = n*myFactorial(n-1);
end
end

Math 98, Spring 2019 Lecture 5: Recursion and Matrices 5 / 20


Recursion

How do we sort a list of numbers?


There are many ways, but quickSort offers a simple recursive
implementation.
1 Pick an element x ∈ v to be the pivot element.
2 Divide the rest of the list in two: those smaller than x and those
larger than x.
3 output = [quickSort(Smaller), x, quickSort(Larger)]
A few questions we need to answer when working out the details:
What are the base cases that we need to handle?
What if some numbers are the same size as x?

Math 98, Spring 2019 Lecture 5: Recursion and Matrices 6 / 20


Exercise

Define the Fibonacci numbers as



0
 n == 0
f (n) = 1 n == 1

f (n − 1) + f (n − 2) n >= 2

Write a recursive function to compute f (n), then write a non-recursive


function (for loop) to do the same. The non-recursive function should
compute all numbers f (0), f (1), . . . , f (n).

Math 98, Spring 2019 Lecture 5: Recursion and Matrices 7 / 20


Recursion

Math 98, Spring 2019 Lecture 5: Recursion and Matrices 8 / 20


Recursion
The problem: our recursive definition did lots of unnecessary computation
by not using previously computed values.

>> fiboRec(4)
Computing f(4)
Computing f(2)
Computing f(0)
Computing f(1)
Computing f(3)
Computing f(1)
Computing f(2)
Computing f(0)
Computing f(1)
ans =
3

Math 98, Spring 2019 Lecture 5: Recursion and Matrices 9 / 20


MATLAB = Matrix Laboratory
Matrices: like vectors, but with more dimensions?

>> ones(2, 3)
1 1 1
1 1 1
>> ones(2)
1 1
1 1
Try also:
zeros(m,n)
rand(m,n)
randn(m,n)
randi(k,m,n)
What happens if you enter only m and leave out n?

Math 98, Spring 2019 Lecture 5: Recursion and Matrices 10 / 20


Matrices

Other commands to create special types of matrices:

>> eye(2)
1 0
0 1
>> v = [3,5]; diag(v)
3 0
0 5
>> diag(v,-1) + 2*eye(3)
2 0 0
3 2 0
0 5 2

Math 98, Spring 2019 Lecture 5: Recursion and Matrices 11 / 20


Matrices

As with vectors, we have lots of ways to access and manipulate the entries
of matrices. Try out the following commands:

>> A = reshape(1:12,3,4)
>> size(A) % also size(A,1) and size(A,2)
>> A’
>> fliplr(A) % also flipud(A)
>> A(2,3)
>> A(:,[2,4])
>> p = randperm(3); q = randperm(4); A(p,q)
>> reshape(A,2,6)
>> A(5,5) = 1

Math 98, Spring 2019 Lecture 5: Recursion and Matrices 12 / 20


Solving Ax = b

Two ways to solve Ax = b:

>> x = inv(A)*b
>> x = A\b

Which one should we use?

Math 98, Spring 2019 Lecture 5: Recursion and Matrices 13 / 20


Solving Ax = b

Math 98, Spring 2019 Lecture 5: Recursion and Matrices 14 / 20


Factorizations

[L, U, P] = lu(A) returns matrices such that P · A = L · U, where L


and U are lower and upper triangular, and P is a pivoting matrix (in
other words, this is the algorithm for Gaussian elimination).
[Q, R] = qr(A) computes the QR factorization of A. If A is m × n
where m  n, use [Q,R] = qr(A,0) to return an m × n matrix Q
rather than an m × m matrix.
[V , D] = eig(A) computes the eigenvalue decompositon of A, so that
A · V = V · D.
These functions have many variations depending on what extra inputs you
give and what outputs you request. . . check the documentation for more
information.

Math 98, Spring 2019 Lecture 5: Recursion and Matrices 15 / 20


Exercise

Solve the system Ax = b, where


   
1 0 0 0 1 1
0 2 0 0 2 1
   
A=0 0 3 0 3 1
and b =  

0 0 0 4 4 1
1 2 3 4 5 1

Do not code A by hand. . . instead, build it using the vector


v = [1, 2, 3, 4, 5].

Math 98, Spring 2019 Lecture 5: Recursion and Matrices 16 / 20


128A Assignment

Given: N ≥ M ≥ 0, a ∈ R, {x0 , . . . , xn } distinct.


Define  m
m d
δnk (a) = Lnk (x)|x=a
dx
Obtain the recurrence relations
m a − xn m
m
δnk (a) = δ m−1 (a) + δ (a),
xk − xn n−1,k xk − xn n−1,k
m ωn−2 (xn−1 ) m−1 m
δn,n (a) = (mδn−1,n−1 (a) + (a − xn−1 )δn−1,n−1 ),
ωn−1 (xn )

where
j
Y
ωj (x) = (x − xk ).
k=0

Math 98, Spring 2019 Lecture 5: Recursion and Matrices 17 / 20


128A Assignment

Try to unpack the recurrence:


0 (a) = Ln (a).
For any n and k, D(0, n, k) = δnk k
Get the relations

D(m, n, k) = F1 [D(m − 1, n − 1, k), D(m, n − 1, k)]


D(m, n, n) = F2 [D(m − 1, n − 1, n − 1), D(m, n − 1, n − 1)]

Layer n can be computed from layer n − 1!

Math 98, Spring 2019 Lecture 5: Recursion and Matrices 18 / 20


128A Assignment

Examining the layers: build up 3-tensor D of size (M + 1, N + 1, N + 1).

(n = 1) (k = 0) (k = 1)
(n = 0) (k = 0)
m 7→ (m = 0) · ·
(m = 0) δn,k m
(m = 1) · δn,k

n k =0:n
m = 0 : min(n, M) m
δn,k

FOR n = 1 : N, to begin with. . .

Math 98, Spring 2019 Lecture 5: Recursion and Matrices 19 / 20


128A Assignment

At the finish, have D(0 : M, 0 : N, 0 : N).


Just want D(M, N, :)
d = D(end,end,:);
d = reshape(d,1,[]);

Math 98, Spring 2019 Lecture 5: Recursion and Matrices 20 / 20

You might also like