{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

Matlab test 1A solutions

Matlab test 1A solutions - PGE 310 TEST#1(October 9 2008...

Info iconThis preview shows pages 1–6. Sign up to view the full content.

View Full Document Right Arrow Icon
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Background image of page 2
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Background image of page 4
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Background image of page 6
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: PGE 310 TEST #1 (October 9, 2008) Name: Answer Key Signature: UT EID: NOTES: 1. You have 1 hour and 15 minutes. Points will be deducted for tests turned in after 12:15 2. Write your name on all pages of the test 3. SHOW ALL WORK. Partial (or any credit) cannot be given for problems without work shown 1. Short Answer (35 points) Consider the following arrays for problem 1 x=[ -2 4 «2]; y=[ 1 -3 -2];A=[114;-2 01];B=[13 2; -2 21]; (a) Use the L2 norm to determine which vector has a larger magnitude, x or y. li$ll;‘C’A1+‘11+tA)2)E :1 :2 7’ HyHZT—UZ Ha) W1A>):W 5) HXHz>HYHZ X iACKS 0L iaVSwer MSMI‘HLJe c t (b)>:n;131<1}13? 1‘313x1—AAQ‘ B]: 3 g 1 1 11' l A 1+ 3+3 ~A+3K+L1 ~A o \ 3 1 ’ 1 {,1 1 140+; L1+o+1 _ l9. 1—1 r I, A B , 1O 51 (0) >> A./B % Leave your answer as fractions A/g: E, E ‘3] ((1) Using a single command replace the 2nd row of “A” with the vector “X” .>>../~\(;,:): X W A(>,‘-‘.~):><L:) 50 20 ' (e) Write the MATLAB code that is equivalent to the following 2 Z i2 + j2 i=1 j=2,4,... sum = 0; for i = 1:50 for j = 2:2:20 sum = sum + i"2 +jA2; end end (i) Name the two types of unavoidable errors in computing and explain the difference Roundoff — digits to the right of the decimal are discarded Truncation — approximate a continuous function with discrete values (g) Using the first 2 terms of the Taylor Series, estimate f(1) for the following function around the expansion point of “0”. Then calculate the true relative percent error f(x)=x3+2x-1 f(x>=f(xo)+f'(xo)(x—xo)+f':f°)(x—xo)2+... SIG»: 5761' l R n : ohm 4 4 @{olztlli‘koi 1" .Acwl (3m: \3+;m-1 2; E ': ;—\ *(oo ”3 500(0 W ; 2. Consider the following Matlab code written in a script file (15 points). Dr. Balhoff’s hint: Write out the result as it passes through the loops, show all your work and it won’t be so intimidating. x=[2 -2 6 3]; for i=1 :4 jmax=1; max=x(jmax); for j=2:4 if x(j)>max max=x(j); jmax=j; end end x(jmax)=—250;y(i)=max end After this code is run, what is (a) x? (b) y? ‘5 2 (,3; ( L93 ‘1 LZLI Lmlfl: XLU:& 1 m0~$:X(.i):3- ( MafoU): r ”‘04 : KL»: .ASO ‘:;_ / :3: . ‘ —- \\ ‘ 3:; “1(0):ng ' $9537”) 1‘ ESL M2 ‘) XO):7§.>‘&50 3; PA ’ gran—my; f MS): wail '33::39- «1 X )2 ‘ ‘- 3’1 _ ’4‘ \ 3—4 J :‘ 5 cl 0—3 fix}? ' XLV):3>9~ M ‘i 0/; ’ Mammy-A '24 I W1>X0023 «WVKUF; ’ 3:7 icLh_g%é . JML‘Is-TLX ! QMccfiti ' ()- qu):'a\50é& " :‘ 3: 10:35" "3): .mitxt;)z-A xcslraso 70% gm) A80 y() 3; 7 AM» ’ S0 D a 380 ‘51 (X'EA-kfio use) ><>E¥>o *4 “350 “450] I W3: ‘9»50 7‘”) A x: ’ ‘ ’ ( . j t 3 A} 50 . vie) y:[{é> 33 Y Ho «inst: «450 $50 an) , , [7:3, 3 a 1;] x5593» QED $50 “0601 3> y;[(9 3 A 2;) 3. Consider the attached data file named gaspricesdat. Write a Matlab script file called gasplotm to read in and plot the data with annotations and then print the price in the last year (25points) Dr. Balhoff’s hint: I split it up into 3 parts and you can credit even if you messed up the previous parts. % Read in the data file gas.dat and create a matrix called GasPrices which has the year in the 1St column and the price in the 2nd column fid = fopen(‘gasprices.dat’,’rt’); headingl = fgetl(fid); heading2 = fgetl(fid); heading3 = fget1(fid); d = fscanf(fid, ‘%f’); GasPrices = reshape(d,[2 length(d)/2])’ % Make a plot of the data (price per gallon versus date). Use red circle points with a line going through the data. Annotate the plot by including a legend, labeling the x and y axis, and setting axis limits from 1980 to 2010 for the dates and O to 5 dollars for the price. Year = GasPrices(:,1); Price = GasPrices(:,2); p10t0(ear,Price,’r—o’). legend(‘Price per Gallon’) xlabel(‘Year’) ' ylabel(‘Price ($ per gallon)’) axis([1980 2010 0 5]) % Print a statement to the screen that tells us what the price is in the last year of the data (a good program will still work if add data in future years). The statement should read: The gasoline price in [YEAR] is [PRICE] dollars per gallon fprintf(‘The gasoline price in %f is %f dollars per gallon’, Year(end), Prices(end)) 4. Matrix/Vector Multiplication (25 points) a. Given the following matrix, T, and vector y, compute d=Tz 1 -2 3 2 . T: 2 —3 1 '2: 1 5*3'3fl“3><l l at 3 a A 7:141 2 ~3 i ‘ , :33} [33/ “A 1 I v; 3 :> Ci : (if 3—3 b. Write a Matlab function file, called ‘matvecm’ that when sent ANY square nXm matrix, A, an m><1 vector, x, and the dimensions, n and m, it outputs a vector b (which is A times x. You must use loop(s) to perform the multiplication and cannot use any built in MATLAB commands (like A*x). Then in the MATLAB command window call the function. Dr. Balhoff’s hint: You can get plenty of partial credit on this problem even if you have no idea how to do the actual algorithm function b = matvec(A,x,m,n) b = zeros(n,l); for i = 1:n for j = :m b(i,1) = b(L1) + A(i,j)*XG); end end =============MATLAB COMMAND W1NDOW=================== >>T=[1-23;2-3l;11-2]; >> Z = [2; 1; 3]; >>b = matvec(T,z,3,3) ...
View Full Document

{[ snackBarMessage ]}