《原版matlab英文课件1》由会员分享,可在线阅读,更多相关《原版matlab英文课件1(36页珍藏版)》请在金锄头文库上搜索。
1、Introduction to MATLAB 7for Engineers William J. Palm IIIAn Overview of MATLAB吳煒吳煒 (Wei Wu)DepartmentofChemicalEngineeringNationalYunlinUniversityofScienceandTechnologyYunlin640,Taiwan,R.O.C.The default MATLAB Desktop. Figure1.111-21-2More?Seepages6-7.Entering Commands and ExpressionsEntering Comman
2、ds and Expressionsn nPressthekeyoncetoseethepreviousentry,Pressthekeyoncetoseethepreviousentry,andsoon.andsoon.n nPressthePresstheEnterEnterkeytoexecutethecommand.keytoexecutethecommand.An Example Session8/10ans=0.80005*ansans=4r=8/10r=0.8000rr=0.8000s=20*rs=16Scalar Arithmetic Operations Table1.11S
3、ymbol Operation MATLAB formexponentiation:ab ab*multiplication:aba*b/rightdivision:a/ba/bleftdivision:b/aab+addition:a +ba+bsubtraction:a -babExamplesofPrecedence8+3*5ans=238+(3*5)ans=23(8+3)*5ans=5542128/4*2ans=042128/(4*2)ans=3(continued)ExamplesofPrecedence(continued)3*42+5ans=53(3*4)2+5ans=14927
4、(1/3)+32(0.2)ans=527(1/3)+320.2ans=5271/3+320.2ans=11The Assignment OperatorThe Assignment Operator = =n nTypingTypingx=3x=3assignsthevalue3tothevariableassignsthevalue3tothevariablex.x.n nWecanthentypeWecanthentypex=x+2.x=x+2.ThisassignsthevalueThisassignsthevalue3+2=5to3+2=5tox.x.Butinalgebrathisi
5、mpliesthat0=2.Butinalgebrathisimpliesthat0=2.n nInalgebrawecanwritex+2=20,butinMATLABweInalgebrawecanwritex+2=20,butinMATLABwecannot.cannot.n nInMATLABtheleftsideofthe=operatormustbeaInMATLABtheleftsideofthe=operatormustbeasinglevariable.singlevariable.n nTherightsidemustbeaTherightsidemustbeacomput
6、able computable value.value.Commands for managing the work session Table1.131-101-10CommandDescriptionclcClearstheCommandwindow.clearRemovesallvariablesfrommemory.clearv1v2Removesthevariablesv1andv2frommemory.(continued)Commands for managing the work sessionTable 1.13 (continued)whoListsthevariables
7、currentlyinmemory.whosListsthecurrentvariablesandsizes,andindicatesiftheyhaveimaginaryparts.:Colon;generatesanarrayhavingregularlyspacedelements.,Comma;separateselementsofanarray.;Semicolon;suppressesscreenprinting;alsodenotesanewrowinanarray.Ellipsis;continuesaline.Special Variables and Constants T
8、able1.14Command DescriptionansTemporaryvariablecontainingthemostrecentanswer.epsSpecifiestheaccuracyoffloatingpointprecision.i,jTheimaginaryunit-1.InfInfinity.NaNIndicatesanundefinednumericalresult.piThenumberp.Complex Number OperationsThenumberc1=12iisenteredasfollows:c1=12i.Anasteriskisnotneededbe
9、tweeniorjandanumber,althoughitisrequiredwithavariable,suchasc2=5i*c1.Becareful.Theexpressionsy=7/2*iandx=7/2igivetwodifferentresults:y = (7/2)i = 3.5iandx = 7/(2i) = 3.5i.Numeric Display Formats Table1.15Command Description and ExampleformatshortFourdecimaldigits(thedefault);13.6745.formatlong16digi
10、ts;17.27484029463547.formatshorteFivedigits(fourdecimals)plusexponent;6.3792e+03.formatlonge16digits(15decimals)plusexponent;6.379243784781294e04.ArraysThenumbers0,0.1,0.2,10canbeassignedtothevariableubytypingu=0:0.1:10.Tocomputew=5sinuforu=0,0.1,0.2,10,thesessionis;u=0:0.1:10;w=5*sin(u); Thesinglel
11、ine,w=5*sin(u),computedtheformulaw=5sinu101times.1-151-15Array Indexu(7)ans=0.6000w(7)ans=2.8232Usethelengthfunctiontodeterminehowmanyvaluesareinanarray.m=length(w)m=101Polynomial RootsTofindtherootsofx37x2+40x34=0,thesessionisa=1,7,40,34;roots(a)ans=3.0000+5.000i3.00005.000i1.0000Therootsarex=1andx
12、=35i.1-171-17Some Commonly Used Mathematical Functions Table1.31Function MATLAB syntax1exexp(x)xsqrt(x)lnxlog(x)log10xlog10(x)cosxcos(x)sinxsin(x)tanxtan(x)cos-1xacos(x)sin-1x asin(x)tan-1xatan(x)1TheMATLABtrigonometricfunctionsuseradianmeasure.A graphics window showing a plot. Figure1.31Some MATLAB
13、 plotting commands Table1.33(continued)plot(x,y)Generatesaplotofthearrayyversusthearrayxonrectilinearaxes.title(text)Putstextinatitleatthetopoftheplot.xlabel(text) Addsatextlabeltothehorizontalaxis(theabscissa).ylabel(text) Addsatextlabeltotheverticalaxis(theordinate).Solution of Linear Algebraic Eq
14、uations6x+12y+4z=707x2y+3z=52x+8y9z=64A=6,12,4;7,2,3;2,8,9;B=70;5;64;Solution=ABSolution=352Thesolutionisx=3,y=5,andz=2.The MATLAB Command window with the Editor/Debugger open. Figure1.411-291-29Example of a Script FileProblem:Thespeedvofafallingobjectdroppedwithnoinitialvelocityisgivenasafunctionof
15、timetbyv=gt.Plotvasafunctionoftfor0ttf,wheretfisthefinaltimeenteredbytheuser.1-361-36(continued)Example of a Script File (continued)%Programfalling_speed.m:%Plotsspeedofafallingobject.%CreatedonMarch1,2004byW.Palm%InputVariable:%tf=finaltime(inseconds)%OutputVariables:%t=arrayoftimesatwhichspeedis%c
16、omputed(inseconds)%v=arrayofspeeds(meters/second)%(continued)Example of a Script File (continued)%ParameterValue:g=9.81;%AccelerationinSIunits%Inputsection:tf=input(Enterfinaltimeinseconds:);%(continued)Example of a Script File (continued)%Calculationsection:dt=tf/500;%Createanarrayof501timevalues.t
17、=0:dt:tf;%Computespeedvalues.v=g*t;%Outputsection:Plot(t,v),xlabel(t(s),ylabel(vm/s)The MATLAB Help Browser. Figure1.511-421-42Help FunctionsHelp Functionsn nhelpfuncname:helpfuncname:DisplaysintheCommandDisplaysintheCommandwindowadescriptionofthespecifiedfunctionwindowadescriptionofthespecifiedfunc
18、tionfuncnamefuncname. .n nlookfortopic:lookfortopic:DisplaysintheCommandDisplaysintheCommandwindowabriefdescriptionforallfunctionswindowabriefdescriptionforallfunctionswhosedescriptionincludesthespecifiedkeywhosedescriptionincludesthespecifiedkeywordwordtopictopic. .n ndocfuncname:docfuncname:Openst
19、heHelpBrowsertoOpenstheHelpBrowsertothereferencepageforthespecifiedfunctionthereferencepageforthespecifiedfunctionfuncnamefuncname,providingadescription,additional,providingadescription,additionalremarks,andexamples.remarks,andexamples.1-43More?Seepages38-43.Relational operators Table1.611-441-44Rel
20、ationalMeaningoperatorLessthan.Greaterthan.=Greaterthanorequalto.=Equalto.=Notequalto.The if Statement ThegeneralformoftheifstatementisifexpressioncommandselseifexpressioncommandselsecommandsendTheelseandelseifstatementsmaybeomittedifnotrequired. 1-481-481-491-49Supposethatwewanttocomputey suchthat1
21、54x +10ifx 910x +10if0x 910ifx =9y=15*sqrt(4x)+10elseifx=0y=10*x+10elsey=10endNotethattheelseifstatementdoesnotrequireaseparateendstatement.y=More?Seepages47-48.LoopsTherearetwotypesofexplicitloopsinMATLAB;theforloop,usedwhenthenumberofpassesisknownaheadoftime,andthewhileloop,usedwhentheloopingproce
22、ssmustterminatewhenaspecifiedconditionissatisfied,andthusthenumberofpassesisnotknowninadvance. 1-501-50Asimpleexampleofaforloopism=0;x(1)=10;fork=2:3:11m=m+1;x(m+1)=x(m)+k2;endktakesonthevalues2,5,8,11.Thevariablemindicatestheindexofthearrayx.Whentheloopisfinishedthearrayxwillhavethevaluesx(1)=14,x(
23、2)=39,x(3)=103,x(4)=224. 1-511-51Asimpleexampleofawhileloopisx=5;k=0;whilex25k=k+1;y(k)=3*x;x=2*x1;endTheloopvariablexisinitiallyassignedthevalue5,anditkeepsthisvalueuntilthestatementx=2*x1isencounteredthefirsttime.Itsvaluethenchangesto9.Beforeeachpassthroughtheloop,xischeckedtoseeifitsvalueislessth
24、an25.Ifso,thepassismade.Ifnot,theloopisskipped.1-521-52Example of a for LoopWriteascriptfiletocomputethesumofthefirst15termsintheseries5k2 2k,k = 1,2,3,15.total=0;fork=1:15total=5*k22*k+total;enddisp(Thesumfor15termsis:)disp(total)Theansweris5960.Example of a for LoopWriteascriptfiletodeterminehowma
25、nytermsarerequiredforthesumoftheseries5k22k,k=1,2,3,toexceed10,000.Whatisthesumforthismanyterms?total=0;k=0;whiletotal1e+4k=k+1;total=5*k22*k+total;enddisp(Thenumberoftermsis:)disp(k)disp(Thesumis:)disp(total)Thesumis10,203after18terms.Example of a while LoopDeterminehowlongitwilltaketoaccumulateatleast$10,000inabankaccountifyoudeposit$500initiallyand$500attheendofeachyear,iftheaccountpays5percentannualinterest.amount=500;k=0;whileamount10000k=k+1;amount=amount*1.05+500;endamountkThefinalresultsareamount=1.0789e+004,or$10,789,andk=14,or14years.