isequal(mysort(A).ppt_第1页
isequal(mysort(A).ppt_第2页
isequal(mysort(A).ppt_第3页
isequal(mysort(A).ppt_第4页
isequal(mysort(A).ppt_第5页
已阅读5页,还剩19页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

IT1005,Labsessiononweek9(5thmeeting),GoodFridayandHappyEastertoallstudents,especiallyforthosewhounderstandthemeaningandcelebratetheseevents.SinceFridayisapublicholiday,studentswithlabsessionsonFridayareadvisedtoattendparallellabsessionsontheotherdays!,Lab5QuickCheck,Haveyouallreceivedmyreplyforlab5?Myreplyshouldcontains:YourMfilesCheckyourfilesformycomments,lookforthistag“SH7:blablabla”.UseCTRL+F(Findfeature)ofyourtexteditortoquicklyidentifymySH7tags.YourMicrosoftWordfileOnlyifyoudonotgivemeyourMfiles.MostofmycommentswillbeintheMfiles.Anyway,checkmycommentshere,ifany,especiallyforq1.candq2.b.IMPORTANT:PLEASEDONOTSENDMEDUPLICATESTUFFS!IFYOUSENDMFILES,DONOTCOPYTHECODETOMSWORDFILE!Yourmarksisstoredinthefile“Marks.txt”insidethereturnedzipfile!Ifthereissomethingwrongwiththemarks,clarifyitwithme.Eitherundercountingorovercounting.Ihavebeenquitelenientinthepastfewlabs,nexttimeIwillbestricter.,L5.Q1.a:my_mean(standard),functiontheMean=my_mean(x)%letusnametheinputarrayasx.%ifyouwanttoexplainsomethingaboutthisfunction,hereisthebestplace!theSum=0;%donotusenamesum,asitisadefaultMatlabfunctionnamen=numel(x);forindex=1:n%sumeverythingfromindex1tolast(note:thereareotherwaystodothisloop)theSum=theSum+x(index);endtheMean=theSum/n;%returntheaveragea.k.a.mean%NoticethatwesuppressALLintermediateoutputswith;!%Ourfunctionshouldbeclean!,L5.Q1.a:my_mean(geek),functiontheMean=my_mean(x)theMean=sum(x(:)/numel(x);%x(:)willgivemeanenumerationoftheinputarrayx(whateverdimension)intocolumnvector%Doingx(:)willdefinitelygivemearowvector%Thus,thistrickwillworkforarrayofANYdimension=)%sumwillthenworkforthisrowvector.%Notethatthereisthislineinthelab5question1.a.%YouareNOTALLOWEDtousetheMATLABmeancommand,%andshouldinsteaduseloops,etc(Matlabfunctionsumisok)toimplementyourfunction.%Veryshort,eh=).,L5.Q1.b:my_var(standard),functiontheVar=my_var(x)%Weshouldjustcallourmy_meanfunctionhere.Donotcreateitagain(redundant!).%Storetheresultinavariablesothatwedonotre-countmeaneverytime!(Important!)theMean=my_mean(x);n=numel(x);sumOfSD=0;%sumofsquareddeviation!forindex=1:n%accumulatethesumofsquareddeviationsumOfSD=sumOfSD+(x(index)-theMean)2;endtheVar=sumOfSD/(n-1);%youknowwhywedividethiswithn-1right?,L5.Q1.b:my_var(geek),functiontheVar=my_var(x)theVar=sum(x(:)-my_mean(x).2)/(numel(x)-1);%Ifyoudonotunderstandwhythatworks,seethese:%1.Convertinputarrayxintoarowvector=x(:)%2.Findthedifferencewiththemean,scalarexpansion!=x(:)-my_mean(x)%3.Squarethedifferences=(x(:)-my_mean(x).2%4.Sumthesquareddifferences=sum(resultofpart3above)%5.Dividetheresultinstep4abovewithn1,L5.Q1.c:(Crazy)Testing,%Manywaystodothis,aslongasyoucanshowthatyourprogramiscorrect.%Myway:TherearemeanandvarcommandsinMatlab,letuscomparewiththemTHOROUGHLY!%meanandvar(iance)arefloatingpoints,wemustusefloatingpointcomparison!%TestswithMANYinputs,thebestistouserandomfunction:D,thoroughtestwithminimumworkfortest=1:100%canincreasethisifyouwant:p,thisiscalledstresstestinSoftwareEngineeringA=randn(1,100);%justgenerateabigrandomarray,everytimethearrayisdifferentresult1=abs(my_mean(A)-mean(A(:)d(j+1)tmp=d(j);d(j)=d(j+1);d(j+1)=tmp;changed=1;%somethingisstillswapped,setthischangedflagto1!endendifchanged=0%doGooglesearchonthetermbubblesorttolearnaboutthisenhancement!break;%stopthebubblesortassoonasyoudonotseeanymoreswapinaniteration!endend,L5.Q2.b:(Crazy)Testing,%Manywaystodothis,aslongasyoucanshowthatyourprogramiscorrect.%Myway:generatemanyrandomarrays,comparemy_sortwithMatlabdefaultsort!%TestswithMANYinputs,thebestistouserandomfunction:D,thoroughtestwithminimumworkfortest=1:100%canincreasethisifyouwant:p,thisiscalledstresstestinSoftwareEngineeringA=rand(1,100);%justgenerateabigrandomarray,everytimethearrayisdifferentresult=all(my_sort(A)=sort(A);%checkifeverysingleelementaftersortingisthesame!%alternatively,youcansayresult=isequal(my_sort(A),sort(A);%youcanalsocreateatesttocheckwhethertheelementsinAkeepsincreasingfromlefttoright!ifresult=1disp(sprintf(Test%d:WARNING,yourprogramisstillWRONG!,test);elsedisp(sprintf(Test%d:OK,test);endend,L5.Q2.c:my_sort2.m(better),functiond=my_sort2(x,asc)%nowyouhaveTWOarguments!d=x;fori=numel(x):-1:1changed=0;forj=1:(i-1)if(asc=1endend,L5.Q2.c:my_sort2.m(geek),functiond=my_sort2(x,asc)d=my_sort(x);%callourstandardascendingsort,ifasc=1,nothingelsewillhappen.ifasc=1%ifuserwantsdescendingd=fliplr(d);%justreverseit:)end,L5.Q2.d:Application,length=0;whilelength0)oftherandomarray:);%thistime,noneedtouserandn!endasc=-1;%assume1=ascending,0=descendingwhile(asc=1|asc=0)%keepaskingwhentheinputisnot1nor0!asc=input(Sortascending(1)ordescending(0):);endbefore=rand(1,length)%Randomarray,elementsfrom0.0-1.0,;omittedsoarrayisprinted!after=my_sort2(before,asc)%Idonotuse;sothatthesortedarrayisimmediatelyprinted.,TheArtofWhiteSpacing,Whitespace=blanklines.Usedtoseparate(orgroup)logicalblocksofcode.Sothatwecanseethatblocksinaglance!Letsseethefollowingexamples:,TheArtofWhiteSpacing(Bad),functiond=my_sort(x)%assumetheinputarrayxisarowvectord=x;%copyxtodfirst,disthevariablethatholdsouranswer!fori=numel(x):-1:1%noticethatthisloopisdecreasingnowchanged=0;forj=1:(i-1)%wecanactuallystopwhenjisalreadyi-1!,andrememberthatiisdecreasing!ifd(j)d(j+1)tmp=d(j);d(j)=d(j+1);d(j+1)=tmp;changed=1;%somethingisstillswappedendendifchanged=0%doGooglesearchonthetermbubblesorttolearnaboutthisenhancement!break;%stopthebubblesortassoonasyoudonotseeanymoreswapinaniteration!endend,TheArtofWhiteSpacing(Bad),functiond=my_sort(x)%assumetheinputarrayxisarowvectord=x;%copyxtodfirst,disthevariablethatholdsouranswer!fori=numel(x):-1:1%noticethatthisloopisdecreasingnowchanged=0;forj=1:(i-1)%wecanactuallystopwhenjisalreadyi-1!,andrememberthatiisdecreasing!ifd(j)d(j+1)tmp=d(j);d(j)=d(j+1);d(j+1)=tmp;changed=1;%somethingisstillswappedendendifchanged=0%doGooglesearchonthetermbubblesorttolearnaboutthisenhancement!break;%stopthebubblesortassoonasyoudonotseeanymoreswapinaniteration!endend,TheArtofWhiteSpacing(OK),functiond=my_sort(x)%assumetheinputarrayxisarowvectord=x;%copyxtodfirst,disthevariablethatholdsouranswer!fori=numel(x):-1:1%noticethatthisloopisdecreasingnowchanged=0;forj=1:(i-1)%wecanactuallystopwhenjisalreadyi-1!,andrememberthatiisdecreasing!ifd(j)d(j+1)tmp=d(j);d(j)=d(j+1);d(j+1)=tmp;changed=1;%somethingisstillswappedendendifchanged=0%doGooglesearchonthetermbubblesorttolearnaboutthisenhancement!break;%stopthebubblesortassoonasyoudonotseeanymoreswapinaniteration!endend,TheArtofWhiteSpacing(OK),functiond=my_sort(x)%assumetheinputarrayxisarowvectord=x;%copyxtodfirst,disthevariablethatholdsouranswer!fori=numel(x):-1:1%noticethatthisloopisdecreasingnowchanged=0;forj=1:(i-1)%wecanactuallystopwhenjisalreadyi-1!,andrememberthatiisdecreasing!ifd(j)d(j+1)tmp=d(j);d(j)=d(j+1);d(j+1)=tmp;changed=1;%somethingisstillswappedendendifchanged=0%doGooglesearchonthetermbubblesorttolearnaboutthisenhancement!break;%stopthebubblesortassoonasyoudonotseeanymoreswapinaniteration!endend,Application1:RootFinding,Polynomialequationsf(x)=1*x2+2*x-1%thecoefficientsarep=12-1Transcendentalequationsf(x)=sin(x)%cannotbeexpressedassimplealgebraRootfindingFindingvaluesofx(andy,z,dependingonthenumberofvariablesused)sothatthefunctionvalueiszero.HowtodoitinMatlabPolynomialequations:roots(12-1)%pisthecoefficientsofthepolynomial,ans=-2.4142and0.4142!fzero(x)x2+2*x-1,0)%theresultis0.4142fzero(x)x2+2*x-1,-2)%theresultis-2.4142fsolve(x)x2+2*x-1,0)%onlyworksifyourMatlabhasoptimizationtoolbox.Transcendentalequations(cannotuseroots):fzero(x)x+cos(x),0)%theresultis-0.7391(seeslide12).fsolve(x)x+cos(x),0)%onlyworksifyourMatlabhasoptimizationtoolbox.,Application2:LinearAlgebra,Simultaneousequations/SetofLinearAlgebraicequationsx=y+12x=yHowtodoitinMatlabRewritethefunctionsinastandardway.1x-1y=12x-1y=0ConvertthemtoMatrices.1-1;2-1*x;y=1;0DoMatrixoperationsinMatlabA*z=bA-1A*z=A-1*bI*z=A-1*bz=A-1*bz=inv(A)*b;z=Abmorepreferredz=1-1;2-11;0z=-1;-2x=-1;y=-2;,TermAssignment-Overview,Thisis30%ofyourfinalIT1005grade.Beseriouswithit.Noplagiarismplease,Iwilldothoroughcheckforeverysubmissions.Mustgivestrongindividualflavorinyouranswers!Strictdeadline,Saturday5April08,5pm,submittoIVLE“TermAssignment”folder!Question1:TrapeziumruleforfindingintegrationA.NaveversionB.MoreaccurateversionNote:Colinhasrevisedtheambiguousvariableainsidefunctionf(t)tobevariablec!Question2:ZebraversusLionSimultaneous,NonLinear,OrdinaryDifferentialEquationQuestion3:SimilartoQ2,4speciesNote:DrSaifhasrevisedxitoxjintheequationthere!Donotworryifyoudonotknowwhattodoyet.Listentothesetwoweekslecturesverycarefully!DrSaifwilltouchthesetopicsinthecominglectures,soon.Thatistheinfofornow,staytunednextweekformoreinformation.,Lab6+SubmissionSystem,Q1.PlayingwithFunctions,toreinforceconceptsaboutfunctions!A-E.DONOTgiveme

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论