solutions to lab problems (assignment 5)binay/102/hw5_sol.pdf · general instructions (continued)...

31
Solutions to Lab Problems (Assignment 5)

Upload: duonganh

Post on 22-Apr-2018

218 views

Category:

Documents


4 download

TRANSCRIPT

Page 1: Solutions to Lab Problems (Assignment 5)binay/102/HW5_Sol.pdf · General Instructions (continued) • You are supposed to work on these problems in the CSIL lab during the

SolutionstoLabProblems(Assignment5)

Page 2: Solutions to Lab Problems (Assignment 5)binay/102/HW5_Sol.pdf · General Instructions (continued) • You are supposed to work on these problems in the CSIL lab during the

GeneralInstructions

•  AlltheproblemsmustbedoneinsystemH:

•  Youcreateadirectorybytyping >>mkdirLabAssignment

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

Page 3: Solutions to Lab Problems (Assignment 5)binay/102/HW5_Sol.pdf · General Instructions (continued) • You are supposed to work on these problems in the CSIL lab during the

GeneralInstructions(continued)

•  YouaresupposedtoworkontheseproblemsintheCSILlabduringthelecturehours.

•  Therewillbetwograduatestudents(EhsanIranmanesh(TA)andLaurensBakker)intheLABtohelpyouout.

•  YoushouldworkontheseproblemsbeforecomingtotheLAB.ItisfineifyouhavesolvedsomeoftheseproblemsoutsidetheLAB.

•  YoumustgetyourprogramrunningintheLAB.EhsanandLaurenswouldbehappytohelpyououtifyouarestuck.

•  Yourattendanceforeachdaywillbenoted.Ifyouhavefinishedsolvingalltheproblems,youdon’tneedtocome.Youareexcusedthen.

•  DiscussingtheproblemswiththefellowstudentsoutsidetheCSILlabareencouraged.

Page 4: Solutions to Lab Problems (Assignment 5)binay/102/HW5_Sol.pdf · General Instructions (continued) • You are supposed to work on these problems in the CSIL lab during the

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)

Page 5: Solutions to Lab Problems (Assignment 5)binay/102/HW5_Sol.pdf · General Instructions (continued) • You are supposed to work on these problems in the CSIL lab during the

Functionrandint

Page 6: Solutions to Lab Problems (Assignment 5)binay/102/HW5_Sol.pdf · General Instructions (continued) • You are supposed to work on these problems in the CSIL lab during the

Script

Page 7: Solutions to Lab Problems (Assignment 5)binay/102/HW5_Sol.pdf · General Instructions (continued) • You are supposed to work on these problems in the CSIL lab during the

FirstFigure

Page 8: Solutions to Lab Problems (Assignment 5)binay/102/HW5_Sol.pdf · General Instructions (continued) • You are supposed to work on these problems in the CSIL lab during the

SecondFigure

Page 9: Solutions to Lab Problems (Assignment 5)binay/102/HW5_Sol.pdf · General Instructions (continued) • You are supposed to work on these problems in the CSIL lab during the

ThirdFigure

Page 10: Solutions to Lab Problems (Assignment 5)binay/102/HW5_Sol.pdf · General Instructions (continued) • You are supposed to work on these problems in the CSIL lab during the

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.

Page 11: Solutions to Lab Problems (Assignment 5)binay/102/HW5_Sol.pdf · General Instructions (continued) • You are supposed to work on these problems in the CSIL lab during the

Sensordata(Thefirstlineissomewhatmessedup)

Page 12: Solutions to Lab Problems (Assignment 5)binay/102/HW5_Sol.pdf · General Instructions (continued) • You are supposed to work on these problems in the CSIL lab during the

FunctionReadSensorData

Page 13: Solutions to Lab Problems (Assignment 5)binay/102/HW5_Sol.pdf · General Instructions (continued) • You are supposed to work on these problems in the CSIL lab during the

ScriptProgram

Page 14: Solutions to Lab Problems (Assignment 5)binay/102/HW5_Sol.pdf · General Instructions (continued) • You are supposed to work on these problems in the CSIL lab during the

Output

Page 15: Solutions to Lab Problems (Assignment 5)binay/102/HW5_Sol.pdf · General Instructions (continued) • You are supposed to work on these problems in the CSIL lab during the

Problem3

Considertheabovematrix6x9matrix.(a)GeneratematrixArowbyrow.Thevaluesmustbegeneratedbythecomputer.

(b)ThesubmatrixBconsistsoflines1and4columns3‐6ofA.CreateB.

(c)ThematrixCconsistsofcolumnsofAinreverseorder.CreateCusing‘for’controlstructure.

Page 16: Solutions to Lab Problems (Assignment 5)binay/102/HW5_Sol.pdf · General Instructions (continued) • You are supposed to work on these problems in the CSIL lab during the

FunctionSpecialMatrix

Page 17: Solutions to Lab Problems (Assignment 5)binay/102/HW5_Sol.pdf · General Instructions (continued) • You are supposed to work on these problems in the CSIL lab during the

FunctionReverse

Page 18: Solutions to Lab Problems (Assignment 5)binay/102/HW5_Sol.pdf · General Instructions (continued) • You are supposed to work on these problems in the CSIL lab during the

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

Page 19: Solutions to Lab Problems (Assignment 5)binay/102/HW5_Sol.pdf · General Instructions (continued) • You are supposed to work on these problems in the CSIL lab during the

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.

Page 20: Solutions to Lab Problems (Assignment 5)binay/102/HW5_Sol.pdf · General Instructions (continued) • You are supposed to work on these problems in the CSIL lab during the

Script

Page 21: Solutions to Lab Problems (Assignment 5)binay/102/HW5_Sol.pdf · General Instructions (continued) • You are supposed to work on these problems in the CSIL lab during the

OutputL(1)=50

Page 22: Solutions to Lab Problems (Assignment 5)binay/102/HW5_Sol.pdf · General Instructions (continued) • You are supposed to work on these problems in the CSIL lab during the
Page 23: Solutions to Lab Problems (Assignment 5)binay/102/HW5_Sol.pdf · General Instructions (continued) • You are supposed to work on these problems in the CSIL lab during the

OutputL(1)=30

Page 24: Solutions to Lab Problems (Assignment 5)binay/102/HW5_Sol.pdf · General Instructions (continued) • You are supposed to work on these problems in the CSIL lab during the

Plot

Page 25: Solutions to Lab Problems (Assignment 5)binay/102/HW5_Sol.pdf · General Instructions (continued) • You are supposed to work on these problems in the CSIL lab during the

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.

Page 26: Solutions to Lab Problems (Assignment 5)binay/102/HW5_Sol.pdf · General Instructions (continued) • You are supposed to work on these problems in the CSIL lab during the

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

Page 27: Solutions to Lab Problems (Assignment 5)binay/102/HW5_Sol.pdf · General Instructions (continued) • You are supposed to work on these problems in the CSIL lab during the

FunctionPermutedtopermutetheAlphabet_string

Page 28: Solutions to Lab Problems (Assignment 5)binay/102/HW5_Sol.pdf · General Instructions (continued) • You are supposed to work on these problems in the CSIL lab during the

FunctionEncode

Page 29: Solutions to Lab Problems (Assignment 5)binay/102/HW5_Sol.pdf · General Instructions (continued) • You are supposed to work on these problems in the CSIL lab during the

FunctionDecode

Page 30: Solutions to Lab Problems (Assignment 5)binay/102/HW5_Sol.pdf · General Instructions (continued) • You are supposed to work on these problems in the CSIL lab during the

Scriptfile

Page 31: Solutions to Lab Problems (Assignment 5)binay/102/HW5_Sol.pdf · General Instructions (continued) • You are supposed to work on these problems in the CSIL lab during the

Output