Matlab test 1B solutions

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

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

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

View Full Document

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

View Full Document

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

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: TEST #1 (October 9, 2008) PGE 310 Name: Ans over \42/ 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=[13 ~2]; y=[ 2 -4 -3];A=[213;-1-3 O];B=[14 —3; 2 2 -1]; (3) Use the L2 norm to determine which vector has a larger magnitude, x or y. H‘XHA:(11+ 31+ (3):): : W Hynz: (211+("I)Z+('3>7’ *3 3 (a? TI; Hyll2> ”KN; y has Os lager Magnitude (b) Compute >>A*B’ 2x3 5x>_=> an. 3"}. 43'.“ 3‘7“? 73.13 l—3O ’- “l Dob ~A'G‘Po ‘ :WB: 133 (0) >> A. /B % Leave your answer as fractions 1 Eats] [143}: [%/3/3 :[a'qu I 30 / Eyz 33/.2 0/, lg 340 g , ,_ 9‘4/ ‘1 >3 Aa/E/ «132 o ((1) Using a single command replace the 2nd row of “A” with the vector “x” Wt; ) XL?) (3::in At;,(:\x 50 (e) Write the MATLAB code that is equivalent to the following 2 2012i + j2 SCAM: O,’ ”"24“" ﬁr L: [50 grdlgaw 30W“ 15MM+ LﬁAl-J «9- encl and (f) Name the two types of unavoidable errors in computing and explain the difference Rot/molt??? Ermvdrﬁé’rs to 44m (73M of clem‘ml av: dt‘scarclect Tfunm‘l‘foA Error~ QPPFOV‘ W ‘3‘ (”44“”“0‘45 ‘QAM‘l‘coA but HA Clucrete Values (g) Using the ﬁrst 2 terms of the Taylor Series, estimate f(1) for the following function around the expansion point of ‘fO”. Then calculate the true relative percent error f(x)=x3+3x-2 fIl2(x0) ﬁx): f(xo)+f'(xo)(x xo)+ f (103:3x:*3:3 700) =03+3(bl‘é—+ 30‘ ’03 :gggx :5~;:l RUN Actual? (l)? +3LU‘A 7—9. (x— x0)2 +... ER: Il‘al {(00 : 50620 \N 2. Consider the following Matlab code written in a script ﬁle (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=[3 7 -l 4]; for i=1 :4 jmax=1; max=x(jmax); for j=224 if x(j)>max max=x(j); jmax=j; end end x(jmax)=—100;y(i)=max end After this code is run, what is (a) x? (b) y? C: ii if; 71:2 EFL] _ k} 3 , maizxu): ms mix; YUPS , magixtl): -/oo maxry \ r- , <2 « gr . ““1 3% / ﬁéﬂz—[oof3 ‘ )éggz'loofg l XilF‘loo/ ~/oo x‘w: ‘, ‘~: . w)?“ . ‘ ’ 1239/2700 MGK‘AZX0337 AXLE): ”I %3 I 7&3), (%S I 31a "K(%)“"l JONAS; \ ,9” J: ":‘IZB ‘— 3=3 ‘9 .‘LI. “LUV/0028 J “3314747 / Xmﬂ * - - J: Maxwell” /’3’“‘"”‘")'3 ’ xut>:~/0<>2"-/ d: W7 1 WM 0mm! (3) I a) I X00: / .4 , _ “n ' i'co ( 2' d . ﬂ «(or—zoo WNW M M3 , V -:>x<a)=vloo YUM/So Is; 40 gig-£3 “/00 4 Lil x:[3 We *1 “/wJ'X‘E/w‘m ”’ "7°03 ’ “H” ”/00 ”/00 "/“J g j y: L7 LB ‘yzﬁ “/ S1 ,y:[7 L/E’lj . Consider the attached data ﬁle named gasprices.dat. Write a Matlab script ﬁle 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 ﬁle gasprices.dat and create a matrix called Gas which has the year in the 1St column and the price in the 2nd column \(Icl 1‘ ﬁsfwn ([1306 iDn‘ceSrdat‘, “ft“ 3 “awry : {gal USA); (“60305 33- 1C art ((#4); We“; ‘5 3 {gal 66a); Cl : kmn—p (Qt-Al ’°zo§‘35 \CcloSe (Luis; _ I (gas : reﬁllux‘M/(Cl/ [9. Imamtd%]) 1 % 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 0 to 5 dollars for the price. [31Meas(:,t)/(,as(:,;),’reo‘l or legend C/Pw‘aa Psir Sadie/C) , “ammo 9.010 0 5)) XiaB-el \iYar\) W x/lmbcH’Pn‘ce. [ll/Gall) year: 6.9.3311); Price : Guest}, 1) Pl°+(year/ price, ’f—o‘) % 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 ﬁpriai‘gil’l’ke \$0~SQUAC PﬁCE. in 04%: is 0/0“: ClollGXS Per Salim"; YC‘q-(QVALM . Price-(8‘43” 4. Matrix/Vector Multiplication (25 points) a. Given the following matrix, P, and vector y, compute c=Py 2 —3 —2 1 P: —1 2 —1;y=23 3x33”: 3x] 3 3 __§ ,3 9.4% ‘1 —I a ~I we» a l a 3M“ b. Write a Matlab function ﬁle, called ‘matvecm’ that when sent ANY square n><m matrix, A, an m><1 vector, x, and the dimensions, 11 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 ﬁanction. 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 £(Ant‘i‘ch b :- VYloCl‘VCC(t/Y\/ V\/ A/X) b=icro\$ (ml) >> P= [2 -3 -2;-12 -1;312]; >> y= [1; 3; 2]; >> l2: matvcc(3,- 3/ P, y) ...
View Full Document

{[ snackBarMessage ]}

### What students are saying

• As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

Kiran Temple University Fox School of Business ‘17, Course Hero Intern

• I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

Dana University of Pennsylvania ‘17, Course Hero Intern

• The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

Jill Tulane University ‘16, Course Hero Intern