lr;itti

,

rJ!.r5 C.r.\

1. (10 pts) Assume first that the followinginstructions have alreadybeen enteredinto MATLAts: A = 12 _ 3 4 0l;
B = [ 4; - j- 2; 4; -A 2 );

c=
D -

t2 1? -0111 ,
[ 3 5; B 10; 5 ' 7 ; 1 1 1 3 ];

3

rVow assumethat the following instructions entercdinto MATLAB the ordergiven. Undereach are in one, wite the output that MATLAB will produce. Yau can omit the "ans=". lf the entry wil! resultin
an effor, wite "errof'.
,, '1 t)

A.

ca t(1,A,c)

F . lin s p a c e (3 , 1 2 , s } )

11 t-4

a,J

qo
0 tl
G. po1l rval (C ,2)

q

l2

t/

d'ial

B.

A. /C

-_) -3 v oJ t r , l L a ra b
= l. DooD 2 5 D0 -0,
C. A*D

:75

v

'l
H . D (2 --3,L:2)

t1lt

b

? tD 57
G -a9 +Lb
'1 =-<Y a

t0 -3 De8
/

I"

-" *r41 ,,"nh{ ( r| tr ty , = max (B) [x,ki

t

,

Y =1
(=l
rT. sun(D )

D=
E. find

ll

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.n enteredin 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. Assuming the first row of A contains "x-" coordinates thesccond that and tfle row of A <;ontains "yl-bc coordinates" a curve,generate "x-y" plot-use a sr.nooth line. Givetheplot the name Exam 2P\ot, of an red labelthe horizontal axis Itow I A andtheverticalaxis Rorv2 A.

p\ot A(r,,,),,r), (,{xamfbl),laLl('furu ,nhlabtl[,fo," zn)::t LoCt,i\, +,+le L
c. Create a new matrix C by squaring each element of A and then chviding t V th._p_fr_gBgg\$.llg.*eJcmenlin the 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\

,

(tFLn\ 3= gft45

t

L=6,^r) / (A+r\ ./

d. Count the number of entriesin A tirat are strictly bigger than 3,

,

p v _.

(,

] ^n[Surn(Ax,)
\ /

e. Create newmatrix D which is the same A except a as that everyelement A tliat is greater in than 10 is replaced bv 5 andeveryelement is lessthanor equalto 10is replaced -5" that by

D=F
'-): ..)

b (fr ) t o)= 5

b( AK= tD=-s
Page3 of 8

3. (20 ptstotal)Useonly MATLAB's impliecl loops(array operations functionsFno explicitloopsare & permittedin eitherparts below!
a.(l0pts).Write a li:nction named Aavg that hasoneinput paramcter namcdA which is ant mxn matrix. The firnction shouldreturnthrceiternsThefirst item is a rov, vectornarned Eachelemcntof ca shouldtle the average the clcments the corrcsponding ca. of in cglumn9r after "dropping"the smallest value,That is, ca[] will bc the average thc rernaining of valuesin colurnn1 {, of A afler droppingthe smallest valuein column 1. ca[2] will bethe average thc remainingvaluesin colunm2 of A of afler droppingthe smallest valuein columl 2, etc.The matrix A mu.st be chzmged this calculation! not by Thc second item is a columnveclornarned Eachelementof ra shouldbe the average thc elements tlre raof in corresponding of A' afler "dropping"tle largestvalue.That is, ra[l] rvill trethe average t}e remainingyaluesin row of row I of A afler dropping the largestvalue in row 1. ra[2] will be the average the remaining valuesin row 2 of A :rJler of droppingthe largestvaluein row 2, etc.The rnatrix A mustnot be changed this calculation! by The third item rehrrned a scalarnamed which is the average all of the elements the array is x of of

I un+iot\lcrI yA yj /
.'t -

F,nl=ury,Cft))
/ --Y,'ntn) Q-t) Ca= SurnCn)
(4,

',

= Surn[n)' mlntn\t l@-\)

o

b.(l0pts)Writea scriptwhich will accomplish following tasksin order: the 1) load the commadelimited datafrom the frle iirrayl)ata.Lxtinto the array A 2) invokethe fincfion Aavgby passing pararneter to it and placingthe returnvaluesil the variablesC, ld andx in A the that order 3) append to A so that it is the last row ofA C 4) append to R sothat it is in the last row ofR x 5) appendR to A so that it is the last column of A

dl-nrud('arra'1 r)A--

L) (c,L,l= Aav!(
r)

x\h

- - f '^ ;q )
= l ? , 7 )t
rft

A1) K

<\^

=F,4,t
Page 4 of 8

4. (15pts)Suppose the functionfile mmfun.mandthe scriptfile main.mas indicated that belowarein the current directory. If main.mib "run", what would you expectto seein the command window?
ma i n . m cl e ar ; c lc ; a :9 ; n b: 1- 2; d i sp ( [ a b c rL ^1-*-t,.^ta L o r al : m m r un d :d + a; d i sp ( [ a b c mmfun.m 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/

r ' c :8 i

/ d :L 2 i

'

d ])-I' \ a t D ) i .-,/ d] )

Indicate command window output on the left below; put any "scratch" work on the right! commandwindow scratch

q
'la

9

lz

h=-tz

C=3

d = lZ

ltL

IL tq q

/vy / trl
g rs b

tr{L(

(d,b) =nrn-fun ((2,72) : tnry,'l:un
= \$;= tz'
[g'= l7z =
lz

lL,Lf
ltl t1
tL t+ l tcl tf

lL+tLlt[=tSG

o

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 produce randomnumber a 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 increment counterfai the otlrerwise, (.33 <= x < .66),then increment counter if the wny otherwise, (x >: .66),increment countgfi. if the After testing all 100randomvaluesyou will needto inspectthe threecountersto determinewhether or not you sholld buy tlte car. Iffai hasthe highestcountofall ofthe counters then display"ForgetAboutIt!" ff wny hasthe highestcount of all of the counters then display 'Wait until next year." if gfi has the highestcount of all ofthe counters then display "Go for it! " You cal assume that all of the countervalueswill be different. t) N=

nAmVU=fl) =C);w^I =tr\1q*r=cJ) f A1
Nhrtt nLlru\bTt
f .- rl y,= U / wnde)J)

f l: Nuh^bw u,tmblv=

t^d

o)
,-dRr

fov

It --

n-

; 1',ltn9+\0)
[a, 'lat +t)

t + au)
dtttl

(t[rr)y= ,a'l) < @.tn) . ub)I rl; = h3n1 runt1
> .r"cil , btr

{-lil-

\y,
r

= 5l' +r)
,l '

= Wt ,9+l, Countors , u)rti
j

== fot natr(coun'lors)
drl(('futg* arr,utl')
/Ly \'o

Iz tfuttI wwYbunlers)

unl,{ (' d^A! watf \fl+'Vi;;"J oLa-rC t r s p t l ( h o 0 ' r f ) {

{"a) 4u\
6.(25pts)Suppose f(x,r,c) is a functionof threevariablesand g(x) is a function of a singlevariable.For that
particular values of r and c, the functions are graphedbelolv as they vary with x:

D4 'l {trrd.h'r-torc4Pti

olla* c) er[= Junc{rorrlru (u,,, )
f( x, r, c) g(x)

N( .N

LtlD "/,+tnl, 2,c (@oLlxel, l, r,c)) , = f zero c ), r,c) - quarl(Eg, D, r) fitta-= / o/ x/ ?Ltad(e-f
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 commands that 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

q(1'!-3

= b

'/"Delw-a nul tuwttion Junctron : qt g\ lqj]

lhfi.rn xvol= fzeYl(@4t,s)l

q1= 1 o ) - 3 )

&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

a %Ddwt- Junr+ton
t'ur,ction[4=i\( x,t L)

Nv--

= { zero l,- z, o' r t' r' c) , 'i @+ I xtral, uol|

T ;)

= - l( r r , , c ) ;

-' 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