Please use the sample format as attached mlx file and upload both mlx and pdf. Make sure to sign problems and which part is it.

By hand part you should write by hand and take a picture then insert it in.

Notes.

- When uploading your work, be sure to choose correct pages, without missing any, for each problem. Besides, make sure all pages are in order, no page is rotated, and none of your code or hand-written work is cut out.
- According to our homework policies, you
__are allowed__to collaborate for homework assignments in a group of up to 5 students. If you did, please list all your collaborators at the top of your homework, below your name. - Homework must be prepared using a live script (.mlx file). Submission generated using any other means (e.g., Words with MATLAB screenshots) will not be accepted.
- Starting from this assignment, you will be asked to write MATLAB functions. Unlike scripts, functions can be included inside your mlx file at the very end of the document. So you no longer need to write an external m-file and print its content using the type function. For more detailed instruction, please read

__[Guide] How to Embed Functions in Live Scripts____:__

Suppose that you have written a function as a separate function m-file, myfoo.m and now you want to include its contents within your homework live script, HW2_Kim.mlx. At the top of MATLAB window, click on Insert > Code Example. It creates a blank box. Any text typed within this box will be formatted in monospaced fonts with syntax highlighting.

Drawbacks:

- The box is not an executable code block.
- If you make changes to myfoo.m, you need update the code example block accordingly (by copying and pasting).

Still suppose that you have a function written as a separate m-file titled myfoo.m. Then you can simply type

type myfoo

within a code block. The contents of myfoo.m will be printed out in your live script.

Note:

- We have been using this to print out the contents of external script m-files. This works in the exact same way for function m-files.

Drawbacks:

- If you forget to run that block after your final edit, your live script will show an outdated version of your function.
- As you complete more and more assignments, your folders will be filled with more m-files and may be difficult to manage unless you are well-organized.

Instead of writing a separate m-file, you may just write the function at the very bottom of the document. The functions are local to the live script, and so you are able to call the function anywhere inside the live script. This way, your mlx file is fully self-contained and you no longer need to go back and forth between Editor and Live Editor. See the live script accompanying Lecture 9; all functions defined for the live script are gathered in the last section titled "Functions Used".

This is my recommended method!

The only caveat I can think of:

- Suppose Problem 1(a) asks you to write a function, and so you write it at the end of your live script. You then exports it to a pdf file and find out that Problem 1 solutions are on pp. 1–2, with the function written for 1(a) appearing on p. 7. In this case, when you upload the pdf file to Gradescope, you have to select pages 7, 1, and 2 for Problem 1, in that order.

Updated on September 30, 2021

Hints for Homework 5

1. (Improved triangular substitutions; adapted from FNC 2.3.5)

(a) The function backsub handles the case in which A is upper triangular; call it U:

U

x1 x2 · · · xp

︸ ︷︷ ︸ =X

=

b1 b2 · · · bp

︸ ︷︷ ︸ =B

.

For any j ∈ N[1,p], the solution of Uxj = bj is obtained by applying the backward substitution formula given in lecture:

xn,j =

bn,j un,n

and

xi,j = 1 ui,i

bi,j − n∑

k=i+1 ui,kxk,j

for i = n− 1,n− 2, . . . , 1.

Encode these formulas using nested for-loops in your program: • outer loop: iterate over columns (j = 1, 2, . . . ,p) • inner loop: implement the backward substitution formula (i = n,n− 1, . . . , 1).

You may use the following template to begin your program backsub.m1 by function X = backsub(U,B) % BACKSUB X = backsub(U,B) % Solve multiple upper triangular linear systems. % Input: % U upper triangular square matrix (n-by-n) % B right-hand side vectors concatenated into an (n-by-p) matrix % Output: % X solution of UX = B (n-by-p)

[n,p] = size(B); % grab dimensions X = zeros(n,p); % preallocate output %% Implement back. subs. formula (nested for-loop)

end

1You do not need to write external function m-files. Simply write them at the end of the document as instructed in the problem.

1

The function forelim can be written in a very analogous manner. Note. If you want to include a check for triangularity of the coefficient matrix, use

istriu (upper triangular) or istril (lower triangular), e.g., if ~istriu(U)

error(’The matrix U must be upper triangular.’); end

The inclusion of such a check will not be part of the grading rubrics. (b) One way to test your code using the given examples is to compute the norm of the

difference of the analytical inverse and the numerically calculated inverse. For example, L1 = …..; L1inv = …..; % analytical inverse given L1invNum = ltinverse(L1); % numerically calculated inverse norm(L1inv – L1invNum) % norm of the difference

We want to see a small norm, if not zero.

2. (Triangular substitution and stability; FNC 2.3.6)

(a) In this part, you show that x = (1, 1, 1, 1, 1)T solves the system for any values of α and β; this is an analytical result.

(b) In this second part, however, you check whether the computer indeed produces the same answer for changing values of β while α is fixed. When the problem says “Using MATLAB, solve the system . . .”, it simply means that you use the backslash to solve the system Ax = b.

3. (Vectorizing mylu.m; FNC 2.4.7)

(a) For this part, follow the direction given in the problem: • delete the keyword for in the inner loop; (just for, not the rest) • delete the matching end of the inner loop; • put a semicolon at the end of i = j+1:n.

This defines i to be a vector of indices (j + 1,j + 2, . . . ,n). Passing i into L or A in the next two lines, the code effectively accesses the (j + 1)st through nth rows of the respective matrices; see the slides titled "Using Vectors as Indices" in Lecture 7 on arrays.

(b) Let x ∈ Rn and A ∈ Rn×n, not necessarily the ones appearing in the problem. The ordinary elementwise vector/matrix notation refers to the convention of denoting the elements of a vector or a matrix using subscripts such as

• xi: the ith element of vector bx; • Ai,j: the element of A on the ith row and jth column.

In MATLAB, they are expressed as x(i) and A(i,j), respectively. Now, let’s concen- trate on the matrix case:

A(< rowindex >,< columnindex >)

When one of the two indices is a vector, as in the case of the problem, it represents the slice of the matrix on a specified row or column. For example,

2

• A row vector consisting of 5th through 8th elements on the 3rd row of A:

A(3, 5 : 8) −→ [ A3,5 A3,6 A3,7 A3,8

] • A column vector consisting of 5th through 8th elements on the 3rd column of A:

A(5 : 8, 3) −→

A5,3 A6,3 A7,3 A8,3

When both indices are vectors, it represents the submatrix on the specified rows and columns. For example,

• The submatrix of A between 2nd and 4th rows and between 1st and 5th columns:

A(2 : 4, 1 : 5) −→

A2,1 A2,2 A2,3 A2,4 A2,5A3,1 A3,2 A3,3 A3,4 A3,5 A4,1 A4,2 A4,3 A4,4 A4,5

You are to translate the MATLAB statements in your vectorized mylu into mathematical notations as shown above.

4. (Application of LU factorization: FNC 2.4.6)

(a) Recall the following properties of the determinant from linear algebra: • det(AB) = det(A) det(B). • det(T) = t11t22 · · ·tnn =

∏n i=1 tii, where T ∈ Rn×n is a triangular matrix.

(b) Try to vectorize your code. The function can be written in only couple lines (excluding the function header and the end statement) without using any loop. It would be also useful to recall that if you only want to generate the second output of a certain function, you put ~ in place of the first output argument as a placeholder. For instance, if you only need U from mylu(A), instead of calling it with [L,U] = mylu(A), use

[~, U] = mylu(A);

Note. Use the vectorized version of mylu which you already wrote for Question 3.

5. (Proper usage of lu; FNC 2.6.1) The expression U L b is equivalent to (U L) b.

6. (FLOP Counting)

(a) Consider the following example question. Question. How would you code x = ABb most efficiently in MATLAB, and how many flops are needed? Answer. There are only two ways2 to code it: x=A*(B*b) or x=(A*B)*b. If the former is used, MATLAB i. carries out B*b: matrix-by-vector multiplication (∼ 2n2 flops)

2This is the same as x = A*B*b.

3

ii. left-multiplies the previous result by A: another matrix-by-vector multiplication (∼ 2n2 flops)

In total, it takes ∼ 4n2 flops. On the other hand, the latter version requires the matrix- by-matrix multiplication A*B, which already takes ∼ 2n3 flops. So the former is the efficient one.

Lesson. Avoid matrix-by-matrix multiplication as much as possible! Also remember to use the backslash operator if matrix inversion is required, instead of inv. When is invoked, MATLAB in general does a pivoted Gaussian elimination, which takes ∼ (2/3)n3 flops.

7. (Matrix norms; Sp20 midterm) Hints are found in the lecture for 10/01/21 (F).

4

,

Updated on September 28, 2021

Math 3607: Homework 5 Due: 10:00PM, Tuesday, October 5, 2021

TOTAL: 30 points

• Problems marked with v are to be done by hand; those marked with � are to be solved using a computer.

• Important note. Do not use Symbolic Math Toolbox. Any work done using sym or syms will receive NO credit.

• Another important note. When asked write a MATLAB function, write one at the end of your live script.

1. (Improved triangular substitutions; adapted from FNC 2.3.5) � If B ∈ Rn×p has columns b1, . . . , bp, then we can pose p linear systems at once by writing AX = B, where X ∈ Rn×p whose jth column xj solves Axj = bj for j = 1, . . . ,p:

A

x1 x2 · · · xp

︸ ︷︷ ︸ =X

=

b1 b2 · · · bp

︸ ︷︷ ︸ =B

.

(a) Modify backsub.m and forelim.m from lecture1 so that they solve the case where the second input is an n×p matrix, for p ≥ 1. Include the programs at the end of your live script.

(b) If AX = I, then X = A−1. Use this fact to write a MATLAB function ltinverse that uses your modified forelim to compute the inverse of a lower triangular matrix. Include the program at the end of your live script. Then test your function using the following matrices, that is, compare your numerical solutions against the given exact solutions.

L1 =

2 0 08 −7 0

4 9 −27

, L−11 =

1 2 0 0 4 7 −

1 7 0

50 189 −

1 21 −

1 27

L2 =

1 0 0 0 1 3 1 0 0 0 13 1 0 0 0 13 1

, L−12 =

1 0 0 0 −13 1 0 0

1 9 −

1 3 1 0

− 127 1 9 −

1 3 1

1Lecture 12 (09/24/21, F): Square Linear Systems (Introduction)

1

2. (Triangular substitution and stability; FNC 2.3.6) Consider the following linear system Ax = b:

1 −1 0 α−β β 0 1 −1 0 0 0 0 1 −1 0 0 0 0 1 −1 0 0 0 0 1

︸ ︷︷ ︸ A

x1 x2 x3 x4 x5

︸ ︷︷ ︸ x

=

α 0 0 0 1

.

︸ ︷︷ ︸ b

(a) v Show that x = (1, 1, 1, 1, 1)T is the solution for any α and β. (b) � Using MATLAB, solve the system with α = 0.1 and β = 10, 100, . . . , 1012, making a

table of the values of β and |x1 − 1|. Write down your observation.

3. (Vectorizing mylu.m; FNC 2.4.7) Below is an instructional version of LU factorization code presented in lecture. function [L,U] = mylu(A) % MYLU LU factorization (demo only–not stable!). % Input: % A square matrix % Output: % L,U unit lower triangular and upper triangular such that LU=A

n = length(A); L = eye(n); % ones on diagonal % Gaussian elimination for j = 1:n-1 for i = j+1:n

L(i,j) = A(i,j) / A(j,j); % row multiplier A(i,j:n) = A(i,j:n) – L(i,j)*A(j,j:n);

end end U = triu(A);

end

Consider the innermost loop. Since the different iterations in i are all independent, it is possible to vectorize this group of operations, that is, rewrite it without a loop. In fact, the necessary changes are to delete the keyword for in the inner loop, and delete the following end line. (You should also put a semicolon at the end of i = j+1:n to suppress extra output.)

(a) � Make the changes as directed and verify that the function works properly. (b) v Write out symbolically (i.e., using ordinary elementwise vector and matrix notation)

what the new version of the function does in the case n = 5 for the iteration with j = 3.

4. (Application of LU factorization: FNC 2.4.6) When computing the determinant of a matrix by hand, it is common to use cofactor expansion and apply the definition recursively. But this is terribly inefficient as a function of the matrix size.

(a) v Explain why, if A = LU is an LU factorization,

det(A) = u11u22 · · ·unn = n∏

i=1 uii.

2

This part is an analytical question. Do it by hand. (b) �Using the result of part (a), write a MATLAB function determinant that computes

the determinant of a given matrix A using mylu from lecture. Include the function at the end of your live script. Use your function and the built-in det on the matrices magic(n) for n = 3, 4, . . . , 7, and make a table (using disp or fprintf) showing n, the value from your function, and the relative error when compared to det.

5. (Proper usage of lu; FNC 2.6.1) v Suppose that A ∈ Rn×n and b ∈ Rn. On the left is correct MATLAB code to solve Ax = b; on the right is similar but incorrect code. Explain using mathematical notation exactly what vector is found by the right-hand version. [L,U] = lu(A); x = U ( L b );

[L,U] = lu(A); x = U L b;

6. (FLOP Counting) v Do LM 10.1–12(a,b,d). Justify your calculation of p and c for each part.

7. (Matrix norms; Sp20 midterm) Let

A = [ 1 2 0 3

] .

(a) v Calculate ‖A‖1, ‖A‖2, ‖A‖∞, and ‖A‖F all by hand. (b) � Imagine that MATLAB does not offer norm function and you are writing one for

others to use, which begins with

function MatrixNorm(A, j) % MatrixNorm computes matrix norms % Usage: % mat_norm(A, 1) returns the 1-norm of A % mat_norm(A, 2) is the same as mat_norm(A) % mat_norm(A, ’inf’) returns the infinity-norm of A % mat_norm(A, ’fro’) returns the Frobenius norm of A

Complete the program. (Hint: To handle the second input argument properly which can be a number or a character, use ischaracter and/or strcmp.)

3

,

Homework 1 Math 3607, Spring 2021 [Your Name] Table of Contents Problem 1. (LM 2.1–6) Problem 2. (LM 2.1–8) Part (a) (i) Part (b) Part (c) Problem 3. (LM 2.1–14) Part (a) Problem 4. (Temperature Conversion) Problem 5. (Oblate Spheroid) Problem 1. (LM 2.1–6) Your answer here. % Code goes here Problem 2. (LM 2.1–8) Part (a) (i) (ii) (vi) Part (b) Part (c) Problem 3. (LM 2.1–14) Part (a) Problem 4. (Temperature Conversion) Problem 5. (Oblate Spheroid)

manual code ready 0.4

2021-01-13T21:38:03Z 2021-01-13T21:38:03Z

application/vnd.mathworks.matlab.code MATLAB Code R2020b

9.9.0.1444674 93b378b9-5b5d-4623-b00d-0e154b3a183c

9.9.0.1524771 R2020b Update 2 Nov 03 2020 2207788044

We are a professional custom writing website. If you have searched a question and bumped into our website just know you are in the right place to get help in your coursework.

Yes. We have posted over our previous orders to display our experience. Since we have done this question before, we can also do it for you. To make sure we do it perfectly, please fill our Order Form. Filling the order form correctly will assist our team in referencing, specifications and future communication.

1. Click on the “**Place order** tab at the top menu or “**Order Now**” icon at the
bottom and a new page will appear with an order form to be filled.

2. Fill in your paper’s requirements in the "**PAPER INFORMATION**" section
and click “**PRICE CALCULATION**” at the bottom to calculate your order
price.

3. Fill in your paper’s academic level, deadline and the required number of pages from the drop-down menus.

4. Click “**FINAL STEP**” to enter your registration details and get an account
with us for record keeping and then, click on “**PROCEED TO CHECKOUT**”
at the bottom of the page.

5. From there, the payment sections will show, follow the guided payment process and your order will be available for our writing team to work on it.

**Why Hire Safehomework.com writers to do your paper?**

- Quality- We are experienced and have access to ample research materials.
- We write plagiarism Free Content
- Confidential- We never share or sell your personal information to third parties.
- Support- Chat with us today! We are always waiting to answer all your questions.