Sie sind auf Seite 1von 7

lr;itti, C.r.\rJ!.

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 ];

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'.
,, '1 t)
A. ca t(1,A,c) F . lin s p a c e (3 , 1 2 , s } )

11
t-4 qo
q l2 t/

a,J

tl G. po1l rval (C ,2)


d'ial
0
B. A. /C

-_) -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. 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"y-
coordinates"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), (,{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\ ./

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

, (, ]
^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

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

cl e ar ; c lc ; functi on Ic, d] :mmfun (a.b)


a :9 ; n b: 1- 2; r ' c :8 i / d :L 2 i ' i f a> b
d i sp ( [ a c:b;
b c d ])--
rL : m m r un I' d:a t
L o r al
^1-*-t,.^ta \ a t D ) i .-,/ e]sei f a= = b
d :d + a;
e:anZi
d i sp ( [ a b c d] ) 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 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! "

You cal assumethat all of the countervalueswill be different.


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

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\

that f(x,r,c) is a functionof threevariablesand g(x) is a function of a singlevariable.For


6.(25pts)Suppose
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
f( x, r, c) N( .N
g(x)
LtlD
"/,+tnl,
2,cl, r,c))
(@oLlxel,
, = f zero
c ), r,c) - quarl(Eg, D,r)
/ o/ x/
fitta-=?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 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

'/"Delw-a nul tuwttion lhfi.rn


g\
lqj] : qt
Junctron 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

%Ddwt-a Junr+ton Nv--


t'ur,ction[4=i\( x,t L)
'i
I xtral, uol| @+ l,- z, o' r t' r' c) ,
= { zero

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

Das könnte Ihnen auch gefallen