31
Solutions to Lab Problems (Assignment 5)

Solutions to Lab Problems (Assignment 5)

  • Upload
    others

  • View
    3

  • Download
    0

Embed Size (px)

Citation preview

SolutionstoLabProblems(Assignment5)

GeneralInstructions

•  AlltheproblemsmustbedoneinsystemH:

•  Youcreateadirectorybytyping >>mkdirLabAssignment

•  Theprogramandtheoutputofeachproblemarestoredinaseparatesub‐directoryinLabAssignment.OnceyouareinthedirectoryLabAssignment(>>cdLabAssignment),youcreateasub‐directoryfortheproblembeingconsidered.Alltheprogramsandtheoutputrelatedtotheproblemarestoredinthesub‐directory.

GeneralInstructions(continued)

•  YouaresupposedtoworkontheseproblemsintheCSILlabduringthelecturehours.

•  Therewillbetwograduatestudents(EhsanIranmanesh(TA)andLaurensBakker)intheLABtohelpyouout.

•  YoushouldworkontheseproblemsbeforecomingtotheLAB.ItisfineifyouhavesolvedsomeoftheseproblemsoutsidetheLAB.

•  YoumustgetyourprogramrunningintheLAB.EhsanandLaurenswouldbehappytohelpyououtifyouarestuck.

•  Yourattendanceforeachdaywillbenoted.Ifyouhavefinishedsolvingalltheproblems,youdon’tneedtocome.Youareexcusedthen.

•  DiscussingtheproblemswiththefellowstudentsoutsidetheCSILlabareencouraged.

Problem1

Createafunctionthatgeneratesrandomvectorsofintegersbetweenaandb,inclusive.Usethedefinitionline

functionA=randint(a,b,N) whereaandbdefinetherangeandNdefinesthesizeofthe

vector.Eachnumberxinthevectorsatisfiesa≤x≤b.1.  Testyourfunctionwiththefollowingpieceofcode: Y=randint(10,17,100000); hist(Y,10:17) Theresultinghistogramshouldbenearlyflat.2.  Testyourfunctionwiththefollowingpiecesofcode: Y=randint(‐30,5,100000); hist(Y,‐30:5)

Y=randint(‐45,‐35,100000); hist(Y,‐45:‐35)

Functionrandint

Script

FirstFigure

SecondFigure

ThirdFigure

Problem2

•  ThisproblemrequiresyoutoreadthefileSensor.datandperformsomeoperationsonthedata.

•  Firstdownloadthefile(availablefromthecoursewebpage)intoyourdirectoryfortheproblem.

•  Thefollowingoperationsareneededtobeperformed:

(a)Readthedatafileandandprintthenumberofsensors(s)andthenumberofseconds‐datacontainedinthefile(t).

(b)Storethedataintofilesasfollows: Times():Avectorwithtelementsstoringthetimesofrecording

Sensors():Antx5arraystoringthesensordata.

(b)Findboththemaximumandtheminimumvaluerecordedoneachsensor.

(c)Findthemeanvalueofeachsensordatacollected. (d)Printthedataincludingthemaximum,minimumandthemean

valueswithappropriateheadings.

Sensordata(Thefirstlineissomewhatmessedup)

FunctionReadSensorData

ScriptProgram

Output

Problem3

Considertheabovematrix6x9matrix.(a)GeneratematrixArowbyrow.Thevaluesmustbegeneratedbythecomputer.

(b)ThesubmatrixBconsistsoflines1and4columns3‐6ofA.CreateB.

(c)ThematrixCconsistsofcolumnsofAinreverseorder.CreateCusing‘for’controlstructure.

FunctionSpecialMatrix

FunctionReverse

Problem4

•  Acertaincompanymanufacturesandsellsgolfcarts.Attheendofeachweek,thecompanytransfersthecartsproducedthatweektostorage(inventory).Allcartsthataresoldaretakenfromfromtheinventory.Asimplemodelofthisprocessis

l(k+1)=P(k)+l(k)–S(k)

where P(k)–thenumberofcartsproducedinweekk

l(k)‐‐thenumberofcartsininventoryk S(k)–thenumberofcartssoldinweekk

Theprojectedweeklysalesfor10weeksare

Week 1 2 3 4 5 6 7 8 9 10

Sales 50 55 60 70 70 75 80 80 90 55

Problem4(continued)

•  Supposetheweeklyproductionisbasedonthepreviousweek’ssalessothatP(k)=S(k‐1).Assumethatthefirstweek’sproductionis50carts:i.e.P(1)=50.– Writeascripttocomputeandplotthenumberofcartsininventoryforeachofthe10weeksoruntiltheinventorydropsbelowzero.

–  Runtheprogramfortwocases:(a)aninitialinventoryof50cartssothatl(1)=50,and(b)aninitialinventoryof30cartssothatl(1)=30.

Script

OutputL(1)=50

OutputL(1)=30

Plot

Problem5(Encodinganddecodingoftexts)

•  Theproblemistoencodeatextmessagebyreplacingeachletterofthetextbyanotherletter.Thedecodingprocesstakestheencodedmessageandconvertsittotheoriginaltext.

•  Inoursystemeachalphabetletterwillbereplacedbyanotherletter.Thismappingisdoneusingatable.

•  Thealphabetvectorstoresthelettersthataregoingtobemapped.Forexample

alphabet_string=‘abcdef....xyzABCD....XYZ0123...89‘ Correspondingtothealphabet_string,wecomputeapermuted

stringbyrandomlypermutedstring.Thepermutedstringmaylooklike:

permuted_string=‘hEfOUzxqrk0o.......’•  Intheabovecaseallainthetextwillbereplacedbyhinthe

encodedtext,allbinthetextwillbereplacedbyEintheencodedtext,andsoon.

Problem5(continued)(Encodinganddecodingoftexts)

•  ThetasksofProblem5are:–  Generatepermuted_stringautomaticallybyusing‘rand’builtinfunction.

Generatetwointegersmandnin[1,length(alphabet_string)];swapthemthandthenthcharactersinalphabet_string.Afterrepeatingthisfunctionseveraltimes,thepermuted_stringiscreated.

–  Acceptaninputtext(T)fromtheterminal.–  Writeafunctionwiththefollowingdefinition

functionText_encoded=Encode(Text,alphabet_string,permuted_string)

thatencodesTextandencodedtextText_encodedisreturned.

–  Writeafunctionwiththefollowingdefinition

functionText=Decode(Text_encoded,alphabet_string,permuted_string) thatdecodesText_encodedtooriginalText.

–  WritebothTextandText_encodedinafile‘Encode_Decode_output.dat’.

FunctionPermutedtopermutetheAlphabet_string

FunctionEncode

FunctionDecode

Scriptfile

Output