C.r.\rJ!.r5
lr;itti,
1. (10 pts) Assume first that the followinginstructionshave alreadybeen enteredinto MATLAts: A = 12 _ 3 4 0l; B = [ 4;
c= D -
- j- 2;
4;
-A 2 );
t2 1? -0111 , [ 3 5;
B 10;
3 5 ' 7 ; 1 1 1 3 ];
rVowassumethat the followinginstructionsare entercdinto MATLABin the ordergiven. Undereach one, wite the output that MATLAB will produce. Yau can omit the "ans=". lf the entry wil! resultin an effor, wite "errof'. ,,
A.
'1 t)
F . lin s p a c e (3 , 1 2 , s } )
ca t(1,A,c)
a,J
l2
t/
(C ,2)
G. po1l rval
tl
0
d'ial
B.
q
qo
11 t-4
A. /C
-_) -3 v oJ t r , l L a ra b = l. DooD-0, 2 5 D0
t1lt
:75
v
'l
b
H . D (2 --3,L:2)
? tD 57
C. A*D
t
G -a9 +Lb
t0 -3 De8
I"
/
=-
,
{- "( *r41 ,,"nh r| tr ty , = max (B) [x,ki
Y =1
a
(=l rT.
D= E. find
ll
sun(D )
t3 L/
(A>c)
2-7
3S
o Page 2 of 8
2. (15 pts total)Supposethat matrix I is previously defined. fhe follo$,ing cornmandhas already bee.nenteredin MATLAB: [m nl: size(.,l); trllthe spaccsprouided, rvrite the MATLAB commalds to do the follovring. Use only MATLAE's implier.{Xoops (array opcrations & functions|-no expliclt ioops are permitted. Considereach caseseparately. a. Delete thc 4'h column of A
A(:,'l)=11 )
,r/'
b. Assumingthat the first row of A containstfle "x-" coordinates and thesccondrow of A <;ontains l-bc"ycoordinates" of a curve,generate an "x-y" plot-use a sr.nooth redline. Givetheplot the name Exam 2P\ot, labelthe horizontalaxis Itow I A andtheverticalaxis Rorv2 A.
p\otLoCt,i\, A(r,,,),,r), ,nhlabtl[,fo," zn)::t (,{xam L fbl),laLl('furu +,+le c. Create a new matrix C by squaring each element of A and then chviding t V th._p_fr_gBgg$.llg.*eJcmenlinthe sum of A and B where B is the samesize as A birt has all elementsequalto l. (You wiJl have to generateB, it rs not alreadYdec-^'rr\
,
t
(tFLn\ 3= gft45
L=6,^r) / (A+r\ ./
d. Count the number of entriesin A tirat are strictly bigger than 3,
,
(,
p v _.
] ^n[Surn(Ax,) \
/
e. Createa newmatrix D which is the sameasA exceptthat everyelementin A tliat is greaterthan 10 is replaced bv 5 andeveryelementthat is lessthanor equalto 10is replacedby -5"
D=F '-): ..)
b (fr ) t o)= 5
b( AK= tD=-s Page3 of 8
3. (20 ptstotal)Useonly MATLAB's implieclloops(array operations& functionsFno explicitloopsare permittedin eitherparts below! a.(l0pts).Write a li:nction namedAavg that hasoneinput paramcternamcdA which is antmxn matrix. The firnction shouldreturnthrceiternsThefirst item is a rov, vectornarnedca. Eachelemcntof ca shouldtle the averageof the clcmentsin the corrcsponding cglumn9r after "dropping"the smallestvalue,That is, ca[] will bc the averageof thc rernainingvaluesin colurnn1 {, of A afler droppingthe smallestvaluein column 1. ca[2] will bethe averageof thc remainingvaluesin colunm2 of A afler droppingthe smallestvaluein columl 2, etc.The matrix A mu.stnot be chzmged by this calculation! Thc seconditem is a columnveclornarnedra- Eachelementof ra shouldbe the averageof thc elementsin tlre corresponding row of A' afler "dropping"tle largestvalue.That is, ra[l] rvill trethe averageof t}e remainingyaluesin row I of A afler dropping the largestvalue in row 1. ra[2] will be the averageof the remaining valuesin row 2 of A :rJler droppingthe largestvaluein row 2, etc.The rnatrix A mustnot be changedby this calculation! The third item rehrrnedis a scalarnamedx which is the averageof all of the elementsof the array
I un+iot\lcrI yA/ yj .'t -
F,nl=ury,Cft)) / Q-t) --Y,'ntn) Ca= SurnCn) (4,
',
- mlntn\tl@-\) = Surn[n)'
b.(l0pts)Writea scriptwhich will accomplishthe following tasksin order: 1) load the commadelimited datafrom the frle iirrayl)ata.Lxtinto the array A 2) invokethe fincfion Aavgby passingthe pararneterA to it and placingthe returnvaluesil the variablesC, ld andx in that order 3) appendC to A so that it is the last row ofA 4) appendx to R sothat it is in the last row ofR 5) appendR to A so that it is the last column of A
r)A--dl-nrud('arra'1
L) (c,L,l= Aav!( x\h
- - f '^ ;q )
A1) K
= l ? , 7 )t
r)
rft
<\^
=F,4,t Page 4 of 8
o
4. (15pts)Suppose that the functionfile mmfun.mandthe scriptfile main.mas indicatedbelowarein the current directory. If main.mib "run", what would you expectto seein the commandwindow? mmfun.m
ma i n . m cl e ar ; c lc ; a :9 ; n b: 1- 2; d i sp ( [ a b c rL : m m r un ^1-*-t,.^ta L o r al d :d + a; d i sp ( [ a b c
r ' c :8 i
/ d :L 2 i
d ])-I' \ a t D ) i .-,/ d] )
'
functi on Ic, d] :mmfun (a.b) i f a> b c:b; d:a t e]sei f a= = b e:anZi d= b^Z; el se c:a; d:b; end dicn/l-r s l v rrL s
1-' r
n v
Al\ sJ/
Indicate command window output on the left below; put any "scratch" work on the right! commandwindow scratch
q 'la
ltL
tr{L(
9
IL tq q
lz
/vy / trl g
rs b
h=-tz
C=3
(d,b) =-nrn-fun ((2,72) : tnry,'l:un = $;= tz' [g'= l7z = lz
lL,Lf ltl t1 tL
lL+tLlt[=tSG
o
d = lZ
t+ l
tcl tf
5-( I 5pts) You are thinking aboutbuying that new BMW that you'vehad your eyeon for a while. you decideto let MATLAB help you out with your decision by letting it "roll the dice" for you. Write a script file that will generate a randomnumber100timesandusetlosevaluestodecidewhetherornotiobuvthecar+
You will needto usethe rand(lrl)functionwhereN will be equalto 1. Calling rand(I) will producea randomnumber between0 and l. Setup an explicit loop to producea randomnumber 100times and assignthis yalue to x. You will need to test the value of x and incrementone of tlnee countersasfollows: if (x < .33),then incrementthe counterfai otlrerwise,if (.33 <= x < .66),then incrementthe counterwny otherwise,if (x >: .66),incrementthe countgfi. After testing all 100randomvaluesyou will needto inspectthe threecountersto determinewhether or not you sholld buy tlte car. Iffai hasthe highestcountofall ofthe countersthen display"ForgetAboutIt!" ff wny hasthe highestcount of all of the countersthen display 'Wait until next year." if gfi has the highestcount of all ofthe countersthen display "Go for it! " You cal assumethat all of the countervalueswill be different. N= t)
nAmVU=fl) f A1=C);w^I =tr\1q*r=cJ) Nhrtt
nLlru\bTt f .- rl y,= U / wnde)J)
o)
f l: u,tmblv=Nuh^bw
,-dRr
t^d
fov
; 1',ltn9+\0)
n-
It --
t + au)
[a, 'lat +t)
< . ub)I (t[rr)y= ,a'l) @.tn) h3n1= runt1rl;
dtttl
.r"cil> -
{-lil-
\y, r
, btr
= 5l' +r) ,l '
= Wt, u)rti ,9+l, Countors j
== fot natr(coun'lors) drl(('futg* arr,utl')
Iz tfuttI wwYbunlers)
\'o/Ly
d^A!(' watfunl,{\fl+'Vi;;"J oLa-rC t r s p t l ( h o{ 0 ' r f )
{"a) 4u\ 6.(25pts)Suppose that f(x,r,c) is a functionof threevariablesand g(x) is a function of a singlevariable.For particular values of r and c, the functions are graphedbelolv as they vary with x:
D4 'l {trrd.h'r-torc4Pti
er[=olla*(u,,,c)) Junc{rorrlru N( .N
f( x, r, c) g(x)
LtlD "/,+tnl, 2,cl, r,c)) (@oLlxel, , = f zero c ), r,c) - quarl(Eg, D,r) fitta-=?Ltad(e-f / o/ x/ x
of the functionis: function[val] = f(x,r,c). Likewise a firnction named g to calculate g(x) has already been written and savedin the file g.m. The first line (header)ofthis function is: function [val]:g(x). Give the commandsthat you could put in a script file to do each of the following. If additional function files are required, indicate how they would be named and the commandsthat they would contain. Use Matlab's function-functions,as appropriateand not array functions. I . (3pts)Thevalues of r and c are on the first two lines of a plain text data file namedgood.dat (onevalue for r on the first line and one value for c on the secondline). Readthesevalues and assignthem to variablesnamedr
i
'/"Delw-a nul tuwttion g\ Junctron lqj] : qt
q1= 1 o ) - 3 )
q(1'!-3
= b
lhfi.rn xvol= fzeYl(@4t,s)l
&ttP(rat)
3. (6pts)Find & display the maximum value of the firnction f(x,r,c) and the value of x where the marimum occurs
%Ddwt-a Junr+ton t'ur,ction[4=i\( x,t L)
T ;)
= - l( r r , , c ) ;
Nv--
= { zero 'i @+ l,- z, o' r t' r' c) , I xtral, uol|
I vat-' -l^'sc'\" tu4 /,tsp(Prfal', P agel of9
4. (l0pts)Find and display the value of the area of the region bounded,bythe positive vertical axis, the function f(x,r,c), the firnction g(x) and the positive x axis.
Page 8 of8