Top Banner
Solutions to Lab Problems (Assignment 5)
31

Solutions to Lab Problems (Assignment 5)

Dec 01, 2021

Download

Documents

dariahiddleston
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Solutions to Lab Problems (Assignment 5)

SolutionstoLabProblems(Assignment5)

Page 2: Solutions to Lab Problems (Assignment 5)

GeneralInstructions

•  AlltheproblemsmustbedoneinsystemH:

•  Youcreateadirectorybytyping >>mkdirLabAssignment

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

Page 3: Solutions to Lab Problems (Assignment 5)

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)

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)

Functionrandint

Page 6: Solutions to Lab Problems (Assignment 5)

Script

Page 7: Solutions to Lab Problems (Assignment 5)

FirstFigure

Page 8: Solutions to Lab Problems (Assignment 5)

SecondFigure

Page 9: Solutions to Lab Problems (Assignment 5)

ThirdFigure

Page 10: Solutions to Lab Problems (Assignment 5)

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)

Sensordata(Thefirstlineissomewhatmessedup)

Page 12: Solutions to Lab Problems (Assignment 5)

FunctionReadSensorData

Page 13: Solutions to Lab Problems (Assignment 5)

ScriptProgram

Page 14: Solutions to Lab Problems (Assignment 5)

Output

Page 15: Solutions to Lab Problems (Assignment 5)

Problem3

Considertheabovematrix6x9matrix.(a)GeneratematrixArowbyrow.Thevaluesmustbegeneratedbythecomputer.

(b)ThesubmatrixBconsistsoflines1and4columns3‐6ofA.CreateB.

(c)ThematrixCconsistsofcolumnsofAinreverseorder.CreateCusing‘for’controlstructure.

Page 16: Solutions to Lab Problems (Assignment 5)

FunctionSpecialMatrix

Page 17: Solutions to Lab Problems (Assignment 5)

FunctionReverse

Page 18: Solutions to Lab Problems (Assignment 5)

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)

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)

Script

Page 21: Solutions to Lab Problems (Assignment 5)

OutputL(1)=50

Page 22: Solutions to Lab Problems (Assignment 5)
Page 23: Solutions to Lab Problems (Assignment 5)

OutputL(1)=30

Page 24: Solutions to Lab Problems (Assignment 5)

Plot

Page 25: Solutions to Lab Problems (Assignment 5)

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)

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)

FunctionPermutedtopermutetheAlphabet_string

Page 28: Solutions to Lab Problems (Assignment 5)

FunctionEncode

Page 29: Solutions to Lab Problems (Assignment 5)

FunctionDecode

Page 30: Solutions to Lab Problems (Assignment 5)

Scriptfile

Page 31: Solutions to Lab Problems (Assignment 5)

Output