% PS1_1.m is the m-file answer for Problem set #1, question 1. % Written by T. Kenna 9809041140. % NOTE: Any information appearing after a "%" sign will be ignored as a % matlab command it is a good idea to use this feature to make comments % about your program so in a few weeks, months, years??, when you want to % try and figure out what you did, your comments will be right there. Also % blank spaces like the one above will also be ignored and will make your % programs easier to follow. % For more information on any matlab command type help and the command. % For other ways to search or get help type help help % Now for the answer % The display format I am using makes the answers come out in matlab % as a readable paragraph. What I found works best when writing text is to % define each line of the paragraph as an individual text string. By using % the command disp(string1),disp(string2),etc. after you have defined each % line, you get a well-formatted paragraph without, headers, funny line-wraps % or spaces. A clc command before the display clears the screen before % displaying the answer and a pause command after the display suspends the % program so the answer can be read; the program is resumed by striking any % key. clc % clears the previous screen ans1=['Start by loading in the data and solving the equation x=A\b.']; disp(ans1) % displays the answer strings load A.dat % use the load command load b.dat % use it again x=A\b % solve for x ans1=['Above is the solution for x, hit any key to see the Determinant and']; ans2=['the Rank.']; disp(ans1),disp(ans2) % displays and centers the answer string pause % suspends program execution. % Now get the determinant and the rank as follows. Remember to leave off % the ";" so the m-file will print the values when it runs. ']; clc Determinant=det(A) % use the det() command Rank=rank(A) % use the rank() command ans1=['The rank value of 7 says that our matrix is full; and that the']; ans2=['equations are solvable uniquely (remember 7 equations and 7']; ans3=['unknowns. Now we calculate the singular values for A by issuing the']; ans4=['command [U S V]=svd(A,0); ']; disp(ans1),disp(ans2),disp(ans3),disp(ans4) pause % Now do a singular value decomposition of matrix A by issuing the command % [U S V]=svd(A,0); remember to use the semicolon unless you want a face full % of numbers'] clc [U S V]=svd(A,0); % the svd command. ans1=['By defining [U S V] as the output of svd(A,0), we end up with 3 new']; ans2=['variables (see your notes for the definition of each matrix). What we']; ans3=['are interested in for this exercise is the diagonal of matrix S which']; ans4=['are the singular values of A. To obtain this, we use the command']; ans5=['singular_values_of_A=diag(S). The long variable name is used for']; ans6=['clairity. ']; disp(ans1),disp(ans2),disp(ans3),disp(ans4),disp(ans5),disp(ans6) pause clc singular_values_of_A=diag(S) % takes the diagonal of matrix S. ans1=['The key to the matrix being well behaved is that the range of']; ans2=['singular values is relatively small (about 1.5 orders of magnitude).']; ans3=['Although not required, you can obtain the condition of this matrix by']; ans4=['typing cond(A) or by taking the ratio of the largest to the smallest']; ans5=['singular values; what do you get? ']; disp(ans1),disp(ans2),disp(ans3),disp(ans4),disp(ans5) pause clc Condition_of_A=cond(A) ans1=['mmm 22.1819, that is actually not too bad considering that a truly']; ans2=['ill-conditioned matrix steps towards positive infinity. ']; disp(ans1),disp(ans2) pause clc ans1=['End of Problem 1']; disp(ans1)