Solutions to Lab Problems (Assignment 5)binay/102/HW5_Sol.pdf · General Instructions (continued)...
Transcript of Solutions to Lab Problems (Assignment 5)binay/102/HW5_Sol.pdf · General Instructions (continued)...
![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](https://reader033.fdocuments.us/reader033/viewer/2022051801/5adc87957f8b9a1a088c1aed/html5/thumbnails/1.jpg)
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](https://reader033.fdocuments.us/reader033/viewer/2022051801/5adc87957f8b9a1a088c1aed/html5/thumbnails/2.jpg)
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](https://reader033.fdocuments.us/reader033/viewer/2022051801/5adc87957f8b9a1a088c1aed/html5/thumbnails/3.jpg)
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](https://reader033.fdocuments.us/reader033/viewer/2022051801/5adc87957f8b9a1a088c1aed/html5/thumbnails/4.jpg)
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](https://reader033.fdocuments.us/reader033/viewer/2022051801/5adc87957f8b9a1a088c1aed/html5/thumbnails/5.jpg)
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](https://reader033.fdocuments.us/reader033/viewer/2022051801/5adc87957f8b9a1a088c1aed/html5/thumbnails/6.jpg)
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](https://reader033.fdocuments.us/reader033/viewer/2022051801/5adc87957f8b9a1a088c1aed/html5/thumbnails/7.jpg)
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](https://reader033.fdocuments.us/reader033/viewer/2022051801/5adc87957f8b9a1a088c1aed/html5/thumbnails/8.jpg)
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](https://reader033.fdocuments.us/reader033/viewer/2022051801/5adc87957f8b9a1a088c1aed/html5/thumbnails/9.jpg)
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](https://reader033.fdocuments.us/reader033/viewer/2022051801/5adc87957f8b9a1a088c1aed/html5/thumbnails/10.jpg)
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](https://reader033.fdocuments.us/reader033/viewer/2022051801/5adc87957f8b9a1a088c1aed/html5/thumbnails/11.jpg)
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](https://reader033.fdocuments.us/reader033/viewer/2022051801/5adc87957f8b9a1a088c1aed/html5/thumbnails/12.jpg)
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](https://reader033.fdocuments.us/reader033/viewer/2022051801/5adc87957f8b9a1a088c1aed/html5/thumbnails/13.jpg)
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](https://reader033.fdocuments.us/reader033/viewer/2022051801/5adc87957f8b9a1a088c1aed/html5/thumbnails/14.jpg)
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](https://reader033.fdocuments.us/reader033/viewer/2022051801/5adc87957f8b9a1a088c1aed/html5/thumbnails/15.jpg)
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](https://reader033.fdocuments.us/reader033/viewer/2022051801/5adc87957f8b9a1a088c1aed/html5/thumbnails/16.jpg)
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](https://reader033.fdocuments.us/reader033/viewer/2022051801/5adc87957f8b9a1a088c1aed/html5/thumbnails/17.jpg)
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](https://reader033.fdocuments.us/reader033/viewer/2022051801/5adc87957f8b9a1a088c1aed/html5/thumbnails/18.jpg)
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](https://reader033.fdocuments.us/reader033/viewer/2022051801/5adc87957f8b9a1a088c1aed/html5/thumbnails/19.jpg)
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](https://reader033.fdocuments.us/reader033/viewer/2022051801/5adc87957f8b9a1a088c1aed/html5/thumbnails/20.jpg)
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](https://reader033.fdocuments.us/reader033/viewer/2022051801/5adc87957f8b9a1a088c1aed/html5/thumbnails/21.jpg)
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](https://reader033.fdocuments.us/reader033/viewer/2022051801/5adc87957f8b9a1a088c1aed/html5/thumbnails/22.jpg)
![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](https://reader033.fdocuments.us/reader033/viewer/2022051801/5adc87957f8b9a1a088c1aed/html5/thumbnails/23.jpg)
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](https://reader033.fdocuments.us/reader033/viewer/2022051801/5adc87957f8b9a1a088c1aed/html5/thumbnails/24.jpg)
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](https://reader033.fdocuments.us/reader033/viewer/2022051801/5adc87957f8b9a1a088c1aed/html5/thumbnails/25.jpg)
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](https://reader033.fdocuments.us/reader033/viewer/2022051801/5adc87957f8b9a1a088c1aed/html5/thumbnails/26.jpg)
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](https://reader033.fdocuments.us/reader033/viewer/2022051801/5adc87957f8b9a1a088c1aed/html5/thumbnails/27.jpg)
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](https://reader033.fdocuments.us/reader033/viewer/2022051801/5adc87957f8b9a1a088c1aed/html5/thumbnails/28.jpg)
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](https://reader033.fdocuments.us/reader033/viewer/2022051801/5adc87957f8b9a1a088c1aed/html5/thumbnails/29.jpg)
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](https://reader033.fdocuments.us/reader033/viewer/2022051801/5adc87957f8b9a1a088c1aed/html5/thumbnails/30.jpg)
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](https://reader033.fdocuments.us/reader033/viewer/2022051801/5adc87957f8b9a1a088c1aed/html5/thumbnails/31.jpg)
Output