Visit to download the full and correct content document: https://ebookmass.com/product/dynamic-system-modelling-and-analysis-with-matlaband-python-1st-edition-jongrae-kim/
More products digital (pdf, epub, mobi) instant download maybe you interests ...
Optimizations and Programming: Linear, Non-linear, Dynamic, Stochastic and Applications with Matlab Abdelkhalak El Hami
https://ebookmass.com/product/optimizations-and-programminglinear-non-linear-dynamic-stochastic-and-applications-withmatlab-abdelkhalak-el-hami/
Solar Photovoltaic System Modelling and Analysis: Design and Estimation (River Publishers Series in Power) 1st Edition Mariprasath
https://ebookmass.com/product/solar-photovoltaic-systemmodelling-and-analysis-design-and-estimation-river-publishersseries-in-power-1st-edition-mariprasath/
Systems Biology Modelling and Analysis 1st Edition Elisabetta De Maria
https://ebookmass.com/product/systems-biology-modelling-andanalysis-1st-edition-elisabetta-de-maria/
Dynamic Macroeconomic Models in Emerging Market
Economies: DSGE Modelling with Financial and Housing Sectors 1st ed. Edition Daniel Lukui Jia
https://ebookmass.com/product/dynamic-macroeconomic-models-inemerging-market-economies-dsge-modelling-with-financial-andhousing-sectors-1st-ed-edition-daniel-lukui-jia/
Dynamic Macroeconomic Models in Emerging Market Economies: DSGE Modelling with Financial and Housing Sectors Daniel
Lukui Jia
https://ebookmass.com/product/dynamic-macroeconomic-models-inemerging-market-economies-dsge-modelling-with-financial-andhousing-sectors-daniel-lukui-jia/
Elementary Vector Calculus and Its Applications with MATLAB Programming (River Publishers Series in Mathematical, Statistical and Computational Modelling for Engineering) 1st Edition Nita H. Shah
https://ebookmass.com/product/elementary-vector-calculus-and-itsapplications-with-matlab-programming-river-publishers-series-inmathematical-statistical-and-computational-modelling-forengineering-1st-edition-nita-h-shah/
The classical Stefan problem: basic concepts, modelling and analysis with quasi-analytical solutions and methods Gupta S.C.
https://ebookmass.com/product/the-classical-stefan-problem-basicconcepts-modelling-and-analysis-with-quasi-analytical-solutionsand-methods-gupta-s-c/
Fundamental Chemistry with MATLAB 1st Edition Daniele Mazza
https://ebookmass.com/product/fundamental-chemistry-withmatlab-1st-edition-daniele-mazza/
Signals and systems : analysis using transform methods and MATLAB Third Edition Michael J. Roberts
https://ebookmass.com/product/signals-and-systems-analysis-usingtransform-methods-and-matlab-third-edition-michael-j-roberts/
DynamicSystemModellingandAnalysiswithMATLABandPython
IEEEPress
445HoesLane
Piscataway,NJ08854
IEEEPressEditorialBoard
SarahSpurgeon, EditorinChief
JónAtliBenediktssonAndreasMolischDiomidisSpinellis
AnjanBoseSaeidNahavandiAhmetMuratTekalp
AdamDrobot
Peter(Yong)Lian
JeffreyReed
ThomasRobertazzi
DynamicSystemModellingandAnalysis withMATLABandPython ForControlEngineers JongraeKim
UniversityofLeeds Leeds,UK
IEEEPressSeriesonControlSystemsTheoryandApplications MariaDomenicaDiBenedetto,SeriesEditor
Copyright©2023byTheInstituteofElectricalandElectronicsEngineers,Inc.Allrights reserved.
PublishedbyJohnWiley&Sons,Inc.,Hoboken,NewJersey.
PublishedsimultaneouslyinCanada.
Nopartofthispublicationmaybereproduced,storedinaretrievalsystem,ortransmittedinany formorbyanymeans,electronic,mechanical,photocopying,recording,scanning,orotherwise, exceptaspermittedunderSection107or108ofthe1976UnitedStatesCopyrightAct,without eitherthepriorwrittenpermissionofthePublisher,orauthorizationthroughpaymentofthe appropriateper-copyfeetotheCopyrightClearanceCenter,Inc.,222RosewoodDrive,Danvers, MA01923,(978)750-8400,fax(978)750-4470,oronthewebatwww.copyright.com.Requeststo thePublisherforpermissionshouldbeaddressedtothePermissionsDepartment,JohnWiley& Sons,Inc.,111RiverStreet,Hoboken,NJ07030,(201)748-6011,fax(201)748-6008,oronlineat http://www.wiley.com/go/permission.
LimitofLiability/DisclaimerofWarranty:Whilethepublisherandauthorhaveusedtheirbest effortsinpreparingthisbook,theymakenorepresentationsorwarrantieswithrespecttothe accuracyorcompletenessofthecontentsofthisbookandspecificallydisclaimanyimplied warrantiesofmerchantabilityorfitnessforaparticularpurpose.Nowarrantymaybecreatedor extendedbysalesrepresentativesorwrittensalesmaterials.Theadviceandstrategiescontained hereinmaynotbesuitableforyoursituation.Youshouldconsultwithaprofessionalwhere appropriate.Further,readersshouldbeawarethatwebsiteslistedinthisworkmayhave changedordisappearedbetweenwhenthisworkwaswrittenandwhenitisread.Neitherthe publishernorauthorshallbeliableforanylossofprofitoranyothercommercialdamages, includingbutnotlimitedtospecial,incidental,consequential,orotherdamages. Forgeneralinformationonourotherproductsandservicesorfortechnicalsupport,please contactourCustomerCareDepartmentwithintheUnitedStatesat(800)762-2974,outsidethe UnitedStatesat(317)572-3993orfax(317)572-4002. Wileyalsopublishesitsbooksinavarietyofelectronicformats.Somecontentthatappearsin printmaynotbeavailableinelectronicformats.FormoreinformationaboutWileyproducts, visitourwebsiteatwww.wiley.com.
LibraryofCongressCataloging-in-PublicationDataAppliedfor: Hardback:9781119801627
CoverDesign:Wiley
CoverImages:©BocskaiIstvan/Shutterstock
Setin9.5/12.5ptSTIXTwoTextbyStraive,Chennai,India
ToMiyoung Contents
Preface xiii
Acknowledgements xv
Acronyms xvii
AbouttheCompanionWebsite xix
1Introduction 1
1.1ScopeoftheBook 1
1.2MotivationExamples 2
1.2.1Free-FallingObject 2
1.2.1.1FirstPrograminMatlab 4
1.2.1.2FirstPrograminPython 10
1.2.2Ligand–ReceptorInteractions 14
1.3OrganizationoftheBook 21 Exercises 21 Bibliography 22
2AttitudeEstimationandControl 23
2.1AttitudeKinematicsandSensors 23
2.1.1SolveQuaternionKinematics 26
2.1.1.1MATLAB 26
2.1.1.2Python 29
2.1.2GyroscopeSensorModel 33
2.1.2.1Zero-MeanGaussianWhiteNoise 33
2.1.2.2GenerateRandomNumbers 34
2.1.2.3StochasticProcess 40
2.1.2.4MATLAB 41
2.1.2.5Python 45
2.1.2.6GyroscopeWhiteNoise 49
2.1.2.7GyroscopeRandomWalkNoise 50
2.1.2.8GyroscopeSimulation 53
2.1.3OpticalSensorModel 57
2.2AttitudeEstimationAlgorithm 64
2.2.1ASimpleAlgorithm 64
2.2.2QUESTAlgorithm 65
2.2.3KalmanFilter 66
2.2.4ExtendedKalmanFilter 75
2.2.4.1ErrorDynamics 76
2.2.4.2BiasNoise 77
2.2.4.3NoisePropagationinErrorDynamics 78
2.2.4.4StateTransitionMatrix, Φ 84
2.2.4.5VectorMeasurements 84
2.2.4.6Summary 86
2.2.4.7KalmanFilterUpdate 86
2.2.4.8KalmanFilterPropagation 87
2.3AttitudeDynamicsandControl 88
2.3.1DynamicsEquationofMotion 88
2.3.1.1MATLAB 91
2.3.1.2Python 94
2.3.2ActuatorandControlAlgorithm 95
2.3.2.1MATLABProgram 98
2.3.2.2Python 101
2.3.2.3AttitudeControlAlgorithm 103
2.3.2.4AltitudeControlAlgorithm 105
2.3.2.5Simulation 106
2.3.2.6MATLAB 107
2.3.2.7RobustnessAnalysis 107
2.3.2.8ParallelProcessing 110 Exercises 113 Bibliography 115
3AutonomousVehicleMissionPlanning 119
3.1PathPlanning 119
3.1.1PotentialFieldMethod 119
3.1.1.1MATLAB 122
3.1.1.2Python 126
3.1.2GraphTheory-BasedSamplingMethod 126
3.1.2.1MATLAB 128
3.1.2.2Python 129
3.1.2.3Dijkstra’sShortestPathAlgorithm 130
3.1.2.4MATLAB 130
3.1.2.5Python 131
3.1.3ComplexObstacles 134
3.1.3.1MATLAB 135
3.1.3.2Python 141
3.2MovingTargetTracking 145
3.2.1UAVandMovingTargetModel 145
3.2.2OptimalTargetTrackingProblem 148
3.2.2.1MATLAB 149
3.2.2.2Python 151
3.2.2.3Worst-CaseScenario 153
3.2.2.4MATLAB 157
3.2.2.5Python 159
3.2.2.6OptimalControlInput 164
3.3TrackingAlgorithmImplementation 167
3.3.1Constraints 167
3.3.1.1MinimumTurnRadiusConstraints 167
3.3.1.2VelocityConstraints 169
3.3.2OptimalSolution 172
3.3.2.1ControlInputSampling 172
3.3.2.2InsidetheConstraints 175
3.3.2.3OptimalInput 177
3.3.3VerificationSimulation 180 Exercises 182 Bibliography 182
4BiologicalSystemModelling 185
4.1BiomolecularInteractions 185
4.2DeterministicModelling 185
4.2.1GroupofCellsandMultipleExperiments 186
4.2.1.1ModelFittingandtheMeasurements 188
4.2.1.2FindingAdaptiveParameters 190
4.2.2 E.coli TryptophanRegulationModel 191
4.2.2.1Steady-StateandDependantParameters 194
4.2.2.2PadéApproximationofTime-Delay 195
4.2.2.3State-SpaceRealization 196
4.2.2.4Python 205
4.2.2.5ModelParameterRanges 206
4.2.2.6ModelFittingOptimization 213
4.2.2.7OptimalSolution(MATLAB) 221
4.2.2.8OptimalSolution(Python) 223
4.2.2.9AdaptiveParameters 226
4.2.2.10Limitations 226
x Contents
4.3BiologicalOscillation 227
4.3.1Gillespie’sDirectMethod 231
4.3.2SimulationImplementation 234
4.3.3RobustnessAnalysis 241 Exercises 245 Bibliography 246
5BiologicalSystemControl 251
5.1ControlAlgorithmImplementation 251
5.1.1PIController 251
5.1.1.1IntegralTerm 252
5.1.1.2ProportionalTerm 253
5.1.1.3SummationoftheProportionalandtheIntegralTerms 253
5.1.1.4ApproximatedPIController 253
5.1.1.5ComparisonofPIControllerandtheApproximation 254
5.1.2ErrorCalculation: ΔP 260
5.2RobustnessAnalysis: �� -Analysis 269
5.2.1SimpleExamples 269
5.2.1.1 �� UpperBound 272
5.2.1.2 �� LowerBound 275
5.2.1.3ComplexNumbersinMATLAB/Python 278
5.2.2SyntheticCircuits 280
5.2.2.1MATLAB 281
5.2.2.2Python 281
5.2.2.3 �� -UpperBound:GeometricApproach 290 Exercises 291 Bibliography 292
6FurtherReadings 295
6.1BooleanNetwork 295
6.2NetworkStructureAnalysis 296
6.3Spatial-TemporalDynamics 297
6.4DeepLearningNeuralNetwork 298
6.5ReinforcementLearning 298 Bibliography 298
AppendixASolutionsforSelectedExercises 301
A.1Chapter1 301
Exercise1.4 301
Exercise1.5 301
A.2Chapter2 302
Exercise2.5 302
A.3Chapter3 302
Exercise3.1 302
Exercise3.6 303
A.4Chapter4 303
Exercise4.1 303
Exercise4.2 303
Exercise4.7 304
A.5Chapter5 304
Exercise5.2 304
Exercise5.3 304
Index 307
Preface ThisbookisforcontrolengineerstolearndynamicsystemmodellingandsimulationandcontroldesignandanalysisusingMATLABorPython.Thereaders areassumedtohavetheundergraduatefinal-yearlevelofknowledgeonordinary differentialequations,vectorcalculus,probability,andbasicprogramming.
WehaveverifiedalltheMATLABandPythoncodesinthebookusingMATLAB R2021aandPython3.8inSpyder,thescientificPythondevelopmentenvironment. Toreducetheconfusioninrunningaparticularprogram,mostoftheprogramsare independentontheirown.Organizingprogrammingwithmultiplefilesisleftas anadvancedskillforreaderstolearnafterreadingthisbook.
Leeds,WestYorkshire,England,UK 30November2021
JongraeKim
Acknowledgements Ihavelearneddynamicmodellingandsimulationthroughmyundergraduate andpost-graduateeducationandresearchprojectsinthepast30years.Hence, thisbookwillnotbepossiblewithouthavingmyteachers,supervisors,and collaborators.IthankDrJinhoKim,ProfessorJohnL.Crassidis,Professor JoãoP.Hespanhna,ProfessorDeclanG.Bates,DrDaizhanCheng,Professor Kwang-HyunCho,ProfessorFrankPollick,andDrRajeevKrishnadas.
JongraeKim
Acronyms DCMdirectioncosinematrix
DNAdeoxyribonucleicacid
EKFextendedKalmanfilter
KFKalmanfilter
LHSleft-handside
LTIlineartime-invariant
mRNAmessengerRNA
mRNAPmessengerRNApolymerase
N2LNetwton’ssecondlawofmotion
ODEordinarydifferentialequation
pdfprobabilitydensityfunction
PIproportionalintegral
QUESTquaternionestimationalgorithm
RHSright-handside
RNAribonucleicacid
AbouttheCompanionWebsite Thisbookisaccompaniedbyacompanionwebsite.
www.wiley.com/go/kim/dynamicmodeling
Thiswebsiteincludes:
● Thesolutionsfortheproblemslistedinthechaptersandtheprogramcodesused inPythonandMATLABsoftwares.
1.1ScopeoftheBook Thisbookisforadvancedundergraduatestudents,post-graduatestudents,orengineerstoacquireprogrammingskillsfordynamicsystemmodellingandanalysis usingcontroltheory.Thereadersareassumedtohaveabasicunderstandingof computerprogramming,ordinarydifferentialequations(ODE),vectorcalculus, andprobability.
Mostengineeringcurriculaattheundergraduatelevelincludeonlyan elementary-levelprogrammingcourseintheearlyoftheundergraduateyears. Onlyahandfulofself-motivatedengineeringstudentsacquireadvancedlevel programmingskillsmainlyfromself-studythroughtedioustime-consuming practicesandtrivialmistakes.Asmodernengineeringsystemssuchasaircraft, satellite,automobile,orautonomousrobotsareimplementedthroughinseparable tightintegrationofhardwaresystemsandsoftwarealgorithms,thedemandfor engineershavingfluentskillsindynamicsystemmodellingandalgorithmdesign isincreasing.Inaddition,theemergenceofinterdisciplinaryareasmergingthe experimentaldomainwithmathematicalandcomputationalapproachessuch assystemsbiology,syntheticbiology,orcomputationalneurosciencefurther increasesthenecessityoftheengineerswhounderstanddynamicsandare capableofcomputationalimplementationsofdynamicmodels.
Thisbookaimstofillthegapinlearningpracticaldynamicmodelling,simulation,andanalysisskillsinaerospaceengineering,robotics,andbiology.Learningprogrammingintheengineeringorbiologydomainrequiresnotonlydomain knowledgebutalsoarobustconceptualunderstandingofalgorithmdesignand implementation.Itisnot,ofcourse,theskillstolearnin14daysorlessasmany onlinecoursesclaim.Tobeconfidentindynamicsystemmodellingandanalysis takesmorethanseveralyearsofpracticeanddedication.Thisbookprovidesthe startingpointofthelongjourneyforthereaderstoequipandpreparebetterfor realengineeringandscientificproblems.
DynamicSystemModellingandAnalysiswithMATLABandPython:ForControlEngineers, FirstEdition.JongraeKim.
©2023TheInstituteofElectricalandElectronicsEngineers,Inc.Published2023byJohnWiley&Sons,Inc. CompanionWebsite:www.wiley.com/go/kim/dynamicmodeling
1.2MotivationExamples 1.2.1Free-FallingObject
Newton’ssecondlawofmotionisgivenby
where Fi isthe i-thexternalforceinNewtons(N)actingontheobjectcharacterized bythemass, m,inkg, d∕dt isthetimederivative, t isthetimeinseconds, �� isthe velocityinm/s,and m�� isthemomentumoftheobject.Newton’ssecondlawstates that thesumofallexternalforcesisequaltothemomentumchangeperunitoftime Considerafree-fallingobjectshowninFigure1.1.Thereexistsonlyoneexternalforce,i.e.thegravitationalforceactingdownwardsinthefigure.Hence,the left-handsideof(1.1)issimplygivenby ∑i Fi = Fg ,where Fg isthegravitational force.Introducetheadditionalassumptionthattheobjectiswithinthereasonable rangefromthesealevel.Withtheassumption,thegravitationalforce, Fg ,isknown tobeproportionaltothemass,andtheproportionalconstantisthegravitational accelerationconstant, g,whichisequalto9.81m/s2 inthesealevel.Therefore, Fg = mg.Replacetheleft-handsideof(1.1),i.e.
Fg
wherethedownwarddirectionissettothepositivedirection,whichistheopposite oftheusualconvention. Ithighlightsthatestablishingaconsistentcoordinatesystem atthebeginningofmodellingisvitalindynamicsystemsimulation.
Fromthekinematicrelationshipbetweenthevelocity, ��,andthedisplacement, x ,wehave dx dt = ��
wheretheoriginof x isattheinitialpositionoftheobject, m,andthepositive directionof x isdownwardsinthefigure.Theright-handsideof(1.2)becomes
mg = Fg =
Finally,theleftmostandtherightmosttermsareequaltoeachotherasfollows:
mg = d dt (m dx dt ) anditisexpandedasfollows:
mg = dm dt dx dt + m d2 x dt2
Usingtheshortnotations, m = dm∕dt, x = dx ∕dt,and x = d2 x ∕dt2 ,andafter rearrangements,thegoverningequationisgivenby ̈ x = g m m ̇
Forpurelyeducationalpurposes,assumethatthemasschangerateisgivenby ̇ m =−m + 2(1.4)
Wecanidentifynowthattherearethreeindependenttime-varyingstates,which aretheposition, x ,thevelocity, x ,andthemass, m.Alltheothertime-varying states,forexample, x and m,canbeexpressedusingtheindependentstatevariables.Definethestatevariablesasfollows:
x1 = x x2 = ̇ x x3 = m
Obtainthetimederivativeofeachstateexpressedinthestatevariableasfollows:
andthisiscalled thestate-spaceform.
Lettheinitialconditionsbeequalto x1 (0)= x (0)= 0.0m, x2 (0)= x (0)= 0.5m/s, and x3 (0)= m(0)= 5kg.Equation(1.5)canbewritteninacompactformusingthe
matrix–vectornotations.Definethestatevector, x,asfollows:
andthecorrespondingstate-spaceformiswrittenas
Thesecond-orderdifferentialequation,(1.3),andthefirst-orderdifferential equation,(1.4),arecombinedintothefirst-orderthree-dimensionalvector differentialequation,(1.6).Anyhigherorderdifferentialequationscanbe transformedintothefirst-ordermulti-dimensionalvectordifferentialequation,
̇ x = f(x).NumericalintegrationmethodssuchasRunge–Kuttaintegration (Pressetal.,2007)solvesthefirst-orderODE.Theycansolveanyhigh-order differentialequationsbytransformingthemintothecorrespondingfirst-order multi-dimensionaldifferentialequation.
1.2.1.1FirstPrograminMatlab
Wearereadytosolve(1.6)withtheinitialconditionequalto x(0)=[0.00.55.0]T , wherethesuperscript T isthetransposeofthevector.Wesolvethedifferentialequationfrom t = 0to t = 5secondsusingMatlab.Matlabincludesmany numericalfunctionsandlibrariestobeusedfordynamicsimulationandanalysis. AnumericalintegratorisoneofthefunctionsalreadyimplementedinMatlab. Hence,theonlytaskwehavetodoforsolvingthedifferentialequationisto learnhowtousetheexistingfunctionsandlibrariesinMatlab.Thecomplete programmetosolvethefree-fallingobjectproblemisgiveninProgram1.1. ProducingFigure1.2isleftasanexerciseinExercise1.1.
1 clear ; 2
3grv_const=9.81; %[m/s^2]
4init_pos=0.0; %[m]
5init_vel=0.5; %[m/s]
6init_mass=5.0; %[kg]
7
8init_time=0; %[s]
9final_time=5.0; %[s]
10time_interval=[init_timefinal_time];
11
12x0=[init_posinit_velinit_mass]; 13[tout,xout]= ode45 (@(time,state)free_falling_obj(time,state, grv_const),time_interval,x0);
14
15 figure (1); 16 plot (tout,xout(:,1))
17 ylabel (’position[m]’);
18 xlabel (’time[s]’); 19
20 figure (2);
21 plot (tout,xout(:,2))
22 ylabel (’velocity[m/s]’); 23 xlabel (’time[s]’); 24
25 figure (3); 26 plot (tout,xout(:,3))
27 ylabel (’m(t)[kg]’);
28 xlabel (’time[s]’); 29
30 function dxdt=free_falling_obj(time,state,grv_const)
31x1=state(1); 32x2=state(2); 33x3=state(3); 34
35dxdt= zeros (3,1); 36dxdt(1)=x2; 37dxdt(2)=grv_const+(x3 2) ∗ (x2/x3); 38dxdt(3)= x3+2; 39 end
Program1.1 (Matlab)Free-fallingobject (a)figure(1)(b)figure(2)
Figure1.2 Free-fallingobjectposition,velocity,andmasstimehistories.
Now,westudythefirstprogramlinebyline.Them-scriptstartswiththe command‘clear’.Theclearcommandremovesallvariablesintheworkspace. Intheworkspace,therewouldbesomevariablesdefinedandusedinprevious activities.Theymayhavethesamenamesbutdifferentmeaningsandvaluesin thecurrentcalculation.Forexample,thegravitationalacceleration‘grv_const’in thethirdlineisundefinedinthecurrentprogramandusesavariableofthesame nameusedtoanalyseobjectsfallingonthemoon.Afallingobjectprogramin theMoonwasexecutedearlier,and‘grv_const’isstillintheworkspace.Without theclearcommand,theincorrectconstantisusedintheprogramproducing wrongresults.Hence,itisrecommendedtocleartheworkspacebeforestarting newcalculations.Wemustbecareful,however,thattheclearcommanderases allvariablesintheworkspace.Beforetheclearcommand,wecheckifallvalues, whichmightbegeneratedfromalongcomputersimulation,weresaved.
Fromline3toline12,severalconstantsaredefined.Basedontheequationswe haveseenearlier,itistemptingtowriteacodeasfollows:
g=9.81
x=0.0
v=0.5
t=[05]
x0=[xvm]
Program1.2 (Matlab)Poorstyleconstantdefinitions
Theseseemtolookcompactandclosertotheequationswederived.Itisabad habittowriteaprograminthisway.Thelistofproblemsintheaboveprogramming styleisasfollows:
● Itdefinesavariablewithasinglecharacter,‘g’,‘x’,‘v’,etc.Usingasinglecharactervariablemightcauseconfusiononthemeaningofthevariableandleadto usingtheminwrongplaceswithincorrectinterpretations.
● Numericalnumbersarewrittenwithoutunits.Thereisnoindicationofunitsof thenumericalvalues,e.g.9.81,isitm/s2 orft/s2 ?
● Itusesmagicnumbers.Whatdothenumbers,0and5,meanindefining‘t’?
Program1.1usesabetterstyle.Theinitialpositionisdefinedusingthevariable name,‘init_pos’,whosevalueis0.0andtheunitisinmetres.Appropriatelynamed variablesreducemistakesandconfusionintheprogram.Program1.1indicatesthe correspondingunitforeachnumericalvalue,e.g.the‘initmass’value5.0isinkg. Weunderstandthemeaningofeachvariablebyitsname.Thetextsafter‘%’are thecomments,wherewecouldaddvariousinformationsuchastheunitofeach numericalvalue.
Inline13,thebuilt-inRunge–Kuttaintegrator, ode45(),isusedtointegratethe differentialequationprovidedbythefunction,‘free_falling_obj’,attheendofthe m-script.Frequently,eachfunctionissavedasaseparatem-script.Itcouldalso beincludedinthem-scriptforthecasesthatthefunctionsmightbeusedinthe specificm-scriptonly.Toincludefunctionsinthem-script,theymustbeplacedat theendofthem-scriptasinthisexample.
FunctionsinMatlabbeginwiththekeyword function andclosewiththe keyword end.Inline30,‘dxdt’isthereturnvariableofthefunctionand ‘free_falling_obj’isthefunctionname.Thefunctionhasthreeinputarguments. Afunctioncanhaveanyinputargumentusedbythefunction.Thisparticular function,‘free_falling_obj’,isnotanordinaryfunction,however.Thisisthe functiontodescribetheODE.Thefunctionistobepassedintothebuilt-in integrator, ode45.Thefirsttwoargumentsofthefunctionfor ode45 mustbetime andstates,i.e. t and x in(1.6).
Inlines31–33,thevariable‘state’isassumedtobeathree-dimensionalvector, andeachelementofthevectorcorrespondstothestates, x1 , x2 ,and x3 .Inline35, thereturnvariable‘dxdt’isinitializedas [000] bythebuilt-infunction zeros(3,1). zeros(m,n) createsthem × nmatrixfilledinzeros.Lines36,37,and38definethe state-spaceformODE,(1.6).
Thefunctionworksperfectlywellwithouttheinitializationlinefor‘dxdt’, line35.However,itisnotgoodprogrammingifline35isremoved.Withoutthe initialization,‘dxdt’inline36isaone-dimensionalscalarvalue.Inthenextlines, itbecomesatwo-dimensionalvalueandathree-dimensionalvalue.Eachline,the sizeof‘dxdt’changes,andthisrequiresthecomputertofindadditionalmemory tostoretheadditionalvalue.Thiscouldincreasethetotalcomputationtime longerandcouldbenoticeablylongerifthisfunctioniscalledamilliontimesor more.Hence,itisbettertoacquirealltherequiredmemoryaheadasinline35.
Efficiencyvs.developmentcycle:Westrivetocreateefficientprograms,but theprototypingphaserequiresafastdevelopmentcycle.
Itisvitaltohavethehabitofbeingconsciousoftheefficiencyofalgorithmimplementation.Ontheotherhand,trynottooverthinktheefficiencyoftheprogram. ScriptlanguagessuchasMatlabandPythonareforrapidimplementationand testing.Hence,itneedsaproperbalancebetweenoptimizingcodesandsavingthe developmenttime.
Now,wearereadytosolvethedifferentialequationusingthebuilt-innumerical integrator, ode45. ode45 standsforODEwithRunge–Kuttafourth-andfifth-order
methods.DetailsoftheRunge–KuttaintegrationmethodscanbefoundinPress etal.(2007).
Recall,thefollowinglinefromProgram1.1:
13[tout,xout]= ode45 (@(time,state)free_falling_obj(time,state, grv_const),time_interval,x0);
Whenweuseode45,theinputargumentstartswith@symbol,whichisthe functionhandle.Thefunctionhandle,@,isusedwhenwepassfunctionA,e.g. ‘free_falling_obj’,tofunctionB,e.g. ode45,wherefunctionBwouldcallfunction Amultipletimes.Withthefunctionhandle,wecancontrolorconstructthe functiontobepassedwithsomeflexibility.‘@(time,state)’explicitlyindicates thatthefunctiontobepassedhastwoarguments,‘time’and‘state’,andtheywill bepassedbetween ode45 and‘free_falling_obj’functioninthespecificorder,i.e. ‘time’bethefirstand‘state’bethesecondargument.Thisorderisrequiredbythe integrator,ode45.
Withthefunctionhandle,wecantakesomefreedomtoorderthefunctionargumentsdifferentlyinthefunctiondefinitionof‘free_falling_obj’.Forexample,we couldwritethefunctionasfollows:
function dxdt=free_falling_obj(time,grv_const,state) x1=state(1); x2=state(2); x3=state(3);
dxdt= zeros (3,1);
dxdt(1)=x2;
dxdt(2)=grv_const+(x3 2) ∗ (x2/x3); dxdt(3)= x3+2; end
andtheintegrationpartisupdatedtofollowtheupdatedfunctiondefinitionas follows:
[tout,xout]= ode45 (@(time,state)free_falling_obj(time,grv_const, state),time_interval,x0);
Theprogramworksthesameastheonesbeforethemodifications.Also,we noticethatwehaveanadditionalinputargument,‘grv_const’.Similarly,wecould addmoreinputparametersiftheyarenecessary.Aslongasthefirstargument, ‘time’,andthesecondargument,‘state’,areindicatedinthefunctionhandle,the functioncanhaveanynumberofinputargumentsinanyordertopasstothe integrator, ode45
Oncetheintegrationiscompleted,theresultsreturntotwooutputvariables, ‘tout’and‘xout’.Executethecommand, whos,intheMatlabcommandprompt, thefollowinginformationisdisplayed: