Computational Mechanics Featuring Matlab DRAFT EDITION $Revision: 1.52
Total Page:16
File Type:pdf, Size:1020Kb
Computational Mechanics featuring Matlab DRAFT EDITION $Revision: 1.52 Richard Sonnenfeld November 4, 2013 c 2012 Richard Sonnenfeld Published by: New Mexico Tech Press All rights reserved Contents 0.1 Introduction–forthestudent. i 0.1.1 Howtoreadthisbook..................... i 0.1.2 Extra credit for corrections! . i 0.1.3 Why computer programming in physics . ii 0.1.4 Otherreferences ........................ ii 0.2 Introduction – for the instructor . iii 0.2.1 To the instructor who is new to Matlab or programming . iii 0.2.2 Intended audience . iii 0.2.3 Coursedesign.......................... iv 0.2.3.1 Lecture portion . iv 0.2.3.2 Lab portion . iv 0.2.3.3 Facilities required . v 0.3 Featuresofthisbook.......................... v 0.3.1 Generalfeatures ........................ v 0.3.2 Course timing . vi 0.3.3 Projects............................. vi 0.3.4 Additional topics . vii 0.3.5 Chapterbychaptercontent . vii 1 Hello World! 1 1.1 Stepsto“HelloWorld”......................... 1 1.2 “HelloWorld”.............................. 1 1.3 Gettinghelp............................... 2 1.4 BrowsingHelp ............................. 3 1.5 Matlab interactivemode ........................ 3 1.6 Operatorsandhowtocalculate . 5 1.6.0.1 Exercise 2A . 6 1.6.0.2 Exercise 2B . 6 1.6.0.3 Exercise 2C . 6 1.7 VariablesandMemory ......................... 6 1.7.1 “=”doesnotmean“Equal”. 7 1.7.2 VariableTypes ......................... 8 3 4 CONTENTS 1.7.3 Scientific Notation . 8 1.8 Usingformattedoutput ........................ 8 1.9 Combiningshortscripts . .. .. .. .. .. .. .. .. .. .. 11 1.9.1 script and m-file ........................ 12 1.10 Afirstinteractiveprogram. 12 1.11 ∗Variables,Memory,andbinarycodes . 12 1.12 Review of commands introduced in this chapter . 14 1.13 EndofChapterProblems . 14 2 IWillnotThrowMassiveProjectilesinClass 17 2.1 Getting homework done quickly with loops . 17 2.2 Usingloopstomakeatable . .. .. .. .. .. .. .. .. .. 18 2.3 Projectile motion for heavy projectiles . 19 2.3.1 Using loops to make a table of projectile motion . 20 2.4 Conditionalstatements. 20 2.4.1 Using conditional statements to stop a loop . 20 2.4.2 Using conditional statements to allow choices . 22 2.4.3 Using conditional statements in a while loop ........ 23 2.5 CreatingASCIIdatafiles .. .. .. .. .. .. .. .. .. .. 24 2.6 LogicalorRelationaloperators . 25 2.7 Morebuilt-infunctions. .. .. .. .. .. .. .. .. .. .. 26 2.7.1 Checking the size of an error – Absolute value . 26 2.7.2 Checking for divisibility – fix() and mod() ......... 27 2.7.3 Roundingfunctions.. .. .. .. .. .. .. .. .. .. 27 2.8 What’snext? .............................. 27 2.9 Reviewofcommandsintroducedinthischapter . 28 2.10 EndofChapterProblems . 29 2.11 Codeexamplesforthischapter . 32 3 Arrays, Matrices and Functions 35 3.1 ArraysandMatrices .......................... 35 3.1.1 Arrays.............................. 36 3.1.1.1 Defining and referencing arrays . 36 3.1.1.2 Using the diary() function to log your exploration 37 3.1.2 RowandColumnArrays(1-DMatrices) . 37 3.1.2.1 Managing matrices with the size() function . 38 3.1.3 UsingArraystorepresentvectors. 39 3.1.3.1 Finding the angle between vectors of arbitrary di- mension........................ 40 3.1.4 Using Arrays to represent sets of measurements . 40 3.1.5 Comparing vectorized calculations to element-by-element calculations........................... 42 3.1.5.1 Initialization . 42 3.1.5.2 Assignment...................... 42 3.1.6 More documentation about arrays and matrices . 43 3.2 Userdefinedfunctions . .. .. .. .. .. .. .. .. .. .. 44 3.2.1 HelponUserDefinedfunctions . 45 CONTENTS 5 3.2.2 Differencebetweenascriptandafunction . 45 3.3 Reviewofcommandsintroducedinthischapter . 46 3.4 EndofChapterProblems . .. .. .. .. .. .. .. .. .. 47 3.5 Codeexamplesforthischapter . 50 4 Working with Scientific Data 53 4.1 Workingwithrealdata ........................ 54 4.1.1 Reading and Writing Data files . 54 4.1.1.1 AboutMetadata . .. .. .. .. .. .. .. 56 4.1.2 Working with matrices . 56 4.2 Plotting data with Matlab ....................... 59 4.2.1 Basic plotting . 59 4.2.2 Improvingtheappearanceofbasicplots . 60 4.2.3 Savingfiguresasimages . 61 4.2.4 Advanced plotting: Objects and handle graphics . 62 4.3 NumericalDerivatives . .. .. .. .. .. .. .. .. .. .. 65 4.4 Reviewofcommandsintroducedinthischapter . 66 4.5 EndofChapterProblems . .. .. .. .. .. .. .. .. .. 67 4.6 Codeexamplesforthischapter . 74 5 Projectile Motion with Drag 81 5.1 Trajectoryandrangeofaheavyprojectile . 82 5.2 More Plotting data with Matlab .................... 83 5.2.1 Making publication quality figures . 84 5.2.2 Adding authorship information . 86 5.3 Document, Define, Derive, Display (D4)............... 87 5.4 Simple animation with Matlab ..................... 88 5.5 ViscosityandStoke’sLaw . 90 5.5.1 Defining Viscosity . 90 5.5.2 Examples of Viscosity . 91 5.5.3 Twotypesofviscosity . .. .. .. .. .. .. .. .. 92 5.5.4 WhatcausesViscosity? . 92 5.5.5 Stokes’Law........................... 93 5.6 Linear Drag in One-Dimension, Terminal Velocity . 93 5.7 Linear Drag in Two Dimensions . 96 5.8 Reviewofcommandsintroducedinthischapter . 96 5.9 EndofChapterProblems . .. .. .. .. .. .. .. .. .. 97 5.10 Codeexamplesforthischapter . .100 6 QuadraticDragandtheEulerMethod 103 6.1 InertialDrag ..............................104 6.1.1 Whatcausesinertialdrag? . .104 6.2 ReynoldsNumber............................106 6.2.1 Fluid mechanics and dimensionless parameters . 106 6.2.2 Reynoldsnumberanddragregimes. .107 6.3 One-dimensional (1-D) analytic solution for quadratic drag . 108 6.3.1 Hyperbolic functions . 109 6 CONTENTS 6.4 EulerMethod..............................109 6.5 ApplyingEulerMethodtoQuadraticdrag . .110 6.5.1 Onedimensionalcase . .110 6.5.2 Matlabcodefor1-DEulerMethod . .111 6.5.3 Using sprintf() toannotateplots . .112 6.5.4 Quadratic drag in 2-Dimensions . 112 6.5.5 Getting position from velocity . 113 6.6 EndofChapterProblems . .114 6.7 Codeexamplesforthischapter . .117 7 Newton’s Universal Law of Gravitation 119 7.1 Introduction...............................120 7.1.1 Linear and Angular motion . 120 7.2 Newton’sLawofUniversalGravitation. .120 7.2.1 VectorformofLawofGravitation . .122 7.3 Kepler’sLaws..............................123 7.3.1 Introduction ..........................123 7.3.2 Planets move in ellipses with the sun at one focus . 123 7.3.3 Kepler’s Period Law . 124 7.3.3.1 Fictitious forces – What holds the planets up? . 124 7.3.3.2 Inertialreferenceframes. .126 7.3.3.3 Uniform circular motion . 126 7.3.3.4 Orbits in inertial reference frames . 128 7.3.3.5 Kepler’s period law for circular orbits . 129 7.3.4 Planetssweepout equalareasin equaltimes . 129 7.3.4.1 Rotational form of Newton’s 2nd Law . 130 7.3.4.2 Conservation of Angular Momentum . 130 7.3.4.3 Equivalence of Kepler’s second law and angular momentumconservation. .131 7.4 GravitationalPotential. .132 7.4.1 EscapeVelocity.........................133 7.4.1.1 Example: Calculating escape velocity from Earth 133 7.5 GravitationofExtendedBodies. .134 7.6 CenterofMass .............................137 7.6.1 Example: Calculating center of mass of four point-masses . 137 7.6.2 Correcting Kepler’s period law . 138 7.6.3 Reduced Mass (µ) .......................139 7.6.4 TheBohrAtom.........................140 7.7 Review..................................142 7.8 EndofChapterProblems . .142 8 Runge-KuttaMethodandOrbitalSimulation 149 8.1 Advantagesofhigher-ordermethods . .149 8.2 Firstorder Runge-Kutta(RK1 orEuler method) . 150 8.3 SecondorderRunge-Kutta(RK2) . .151 8.3.1 RK2AlgebraicInterpretation . .152 8.3.2 RK2GraphicalInterpretation. .153 CONTENTS 7 8.4 FourthorderRunge-Kutta(RK4). .153 8.5 Applying Runge-Kutta methods to quadratic drag . 154 8.6 Applying Runge-Kutta methods to gravitation . 157 8.6.1 Sample listing for Runge-Kutta2 applied to gravitation . 157 8.7 EndofChapterProblems . .161 8.8 PossibleProjectsforPhysics241 . .168 8.8.1 Preproposal...........................168 8.8.2 Proposal.............................168 8.8.3 Difficulty and Grading . 168 8.8.4 TheProjects ..........................168 8.8.4.1 Modeling . 168 8.8.4.2 Data Analysis . 171 8 CONTENTS 0.1. INTRODUCTION – FOR THE STUDENT i 0.1 Introduction – for the student 0.1.1 How to read this book In this age of hyperlinks and non-linear thinking, I believe learning can still be best served by an engaging story that starts at the beginning and progresses logically to the end. Hyperlinks are good when you are already an expert in an area and you know just what you are looking for. The story is best when exploring a world that is new to you; you do not yet know what you are looking for. While some of the students taking this course may have computer programming experience, none is expected in advance. The best way to read a physics (or math, or engineering text) is with pencil and paper, so that you repeat (or fill in) all the details of derivations and examples that are given in the book. Psychologists have long known that “active learning” is the only way to keep your mind engaged in a new and difficult subject. Just scanning the words on the page is fine for a novel, but not for learning a technical subject. When the subject is explicitly computer programming, one should have, in lieu of pencil and paper, an open terminal, or in this case, and open Matlab command line. This book in no way tries to be exhaustive in its coverage of the Matlab language itself. In fact Matlab is huge, and it grows every year. Since it is a commercial program, it is in the interests