Beruflich Dokumente
Kultur Dokumente
r5
1. (10 pts) Assume first that the followinginstructionshave alreadybeen enteredinto MATLAts:
A = 12 _ 3 4 0l;
B = [ 4; - j- 2; 4; -A 2 );
c= t2 1? -0111 , 3
D - [ 3 5; B 10; 5 ' 7 ; 1 1 1 3 ];
11
t-4 qo
q l2 t/
a,J
-_) -3 v oJ t r - :75 v
, l L a ra b
'l
= l. DooD-0, 2 5 D0 t1lt b H . D (2 --3,L:2)
C. A*D ? tD
57
t ,
*r41 ,,"nh {- "(
G -a9 +Lb t0 -3 De8
I"
r|
[x,ki
tr ty ,
= max (B)
/
Y =1
a
=-<Y'1
(=l
rT. sun(D )
D= ll t3
L/
E. find (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(:,'l)=11
) ,r/'
p\otLoCt,i\,
A(r,,,),,r), (,{xam
+,+le ,nhlabtl[,fo,"
L fbl),laLl('furu zn)::t
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\
(tFLn\
3= gft45
,
t L=6,^r) / (A+r\ ./
, (, ]
^n[Surn(Ax,)
p v _.
\ /
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
shouldreturnthrceiterns-
Thefirst 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)) ',
Ca= SurnCn) / Q-t)
--Y,'ntn)
(4, - mlntn\tl@-\)
= Surn[n)' o
r)A--dl-nrud('arra'1
L) (c,L,l= Aav!(
x\h
r)
- - f '^ ;q )
rft
A1) K = l ? , 7 )t
<\^
=F,4,t
Page 4 of 8
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?
ma i n . m mmfun.m
Indicate command window output on the left below; put any "scratch" work on the right!
commandwindow scratch
q 9 lz h=-tz C=3 d = lZ
'la
ltL IL /vy / trl (d,b)
=-nrn-fun
g rs b
((2,72)
: tnry,'l:un
tr{L( tq q
= lL,Lf
$;= tz'
[g'= l7z = ltl t1
lz tL t+ l tcl tf
lL+tLlt[=tSG
o
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! "
u,tmblv=Nuh^bw
t^d
fov n-
It -- ;
1',ltn9+\0)
t + au)
[a, 'lat +t)
dtttl @.tn) < . ub)I (t[rr)y= ,a'l)
h3n1= runt1rl;
{-lil- .r"cil> - , btr
= 5l' +r)
\y,
r ,l '
,9+l,
= Wt, u)rti
Countors
j == fot
natr(coun'lors)
drl(('futg* arr,utl')
Iz \'o/Ly
tfuttI wwYbunlers)
d^A!(' watfunl,{\fl+'Vi;;"J
oLa-rC t r s p t l ( h o{ 0 ' r f )
{"a) 4u\
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 q(1'!-3 = b
3. (6pts)Find & display the maximum value of the firnction f(x,r,c) and the value of x where the marimum occurs
= - l( r r , , c ) ; I vat-' -l^'sc'\"
T ;) 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