Write a function in Matlab that takes as input a size n and a tridiagonal matrix
ID: 3799012 • Letter: W
Question
Write a function in Matlab that takes as input a size n and a tridiagonal matrix given as three vectors: n × 1 vector v representing the main diagonal, (n 1) × 1 vector w representing the upper diagonal, and (n 1) × 1 vector z representing the lower diagonal. Have this function output the LU factorization with the U as two vectors and the L as one vector representing the diagonals. Also output the number of flops used. Use only basic programming.
(a) Write out or print out your function.
(b) Run the case with n = 10, v the vector of 2’s, w and z the vector of 1’s, and b the vector of 1’s. Write down your results for the diagonals of L and U.
(c) Run the case with n = 50 and n = 100 with v the vector of 2’s, w and z the vector of 1’s. Write down your results for the number of flops used
Explanation / Answer
Syntax for diagonals of matrix
X = diag(v,k)
X = diag(v)
v = diag(X,k)
v = diag(X)
Description
X = diag(v,k) when v is a vector of n components, returns a square matrix X of order n+abs(k), with the elements of v on the kth diagonal. k = 0 represents the main diagonal, k > 0 above the main diagonal, and k < 0 below the main diagonal.
X = diag(v) puts v on the main diagonal, same as above with k = 0.
v = diag(X,k) for matrix X, returns a column vector v formed from the elements of the kth diagonal of X.
v = diag(X) returns the main diagonal of X, same as above with k = 0 .
Tips
diag(diag(X)) is a diagonal matrix.
sum(diag(X)) is the trace of X.
diag([]) generates an empty matrix, ([]).
diag(m-by-1,k) generates a matrix of size m+abs(k)-by-m+abs(k).
diag(1-by-n,k) generates a matrix of size n+abs(k)-by-n+abs(k).
Examples
The statement
diag(-m:m)+diag(ones(2*m,1),1)+diag(ones(2*m,1),-1)
produces a tridiagonal matrix of order 2*m+1.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.