Integrating Equations of Motion in Mathematica

Similar documents
This notebook "MathematicaDemo.nb" can be downloaded from the course web page.

Arithmetic expressions can be typed into Maple using the regular operators:

Getting Started With Mathematica

Getting Started With Mathematica

Basic stuff -- assignments, arithmetic and functions

Dynamics and Vibrations Mupad tutorial

Basic Exercises about Mathematica

Unit II Graphing Functions and Data

Ph3 Mathematica Homework: Week 1

Mathematica Assignment 1

Representations of Curves and Surfaces, and of their Tangent Lines, and Tangent Planes in R 2 and R 3 Robert L.Foote, Fall 2007

Choose the file menu, and select Open. Input to be typed at the Maple prompt. Output from Maple. An important tip.

The MathType Window. The picture below shows MathType with all parts of its toolbar visible: Small bar. Tabs. Ruler. Selection.

ü 1.1 Getting Started

Troubleshooting Maple Worksheets: Common Problems

Applied Calculus. Lab 1: An Introduction to R

Introduction to MCS 220 and L A TEX

Adjusting the Display Contrast (Making the Screen Lighter or Darker)

INTRODUCTION TO MATHEMATICA FOR PHYSICS 103

P-Set 1 Solution Set. Solutions created by Melissa Diskin. Part 1 (3 points) Part 2 (4 points) ê H1 ê 2 + xl^hx ê 2L, xdd

Beginner s Mathematica Tutorial

Partial Derivatives (Online)

Dynamical Systems - Math 3280 Mathematica: From Algebra to Dynamical Systems c

MATLAB Programming for Numerical Computation Dr. Niket Kaisare Department Of Chemical Engineering Indian Institute of Technology, Madras

Contents. Foreword. Examples of GeoGebra Applet Construction 1 A Straight Line Graph... 1 A Quadratic Graph... 6 The Scalar Product...

Getting Started with Mathematica

HANDS-ON START TO WOLFRAM MATHEMATICA. and Programming with the Wolfram Language. Cliff Hastings Kelvin Mischo Michael Morrison.

Math 1191 Mathematica Introduction

Introduction. What is Maple? How to use this tutorial. Where can I find Maple?

A Mathematica Tutorial

But a vertex has two coordinates, an x and a y coordinate. So how would you find the corresponding y-value?

Quickstart: Mathematica for Calculus I (Version 9.0) C. G. Melles Mathematics Department United States Naval Academy September 2, 2013

Getting Started With Linux and Fortran Part 3

GETTING STARTED WITH MATHEMATICA

1. How Mathematica works

1 Introduction to Matlab

Getting Started with Mathematica

Math 25 and Maple 3 + 4;

Appendix A. Introduction to MATLAB. A.1 What Is MATLAB?

Computer Graphics Prof. Sukhendu Das Dept. of Computer Science and Engineering Indian Institute of Technology, Madras Lecture - 14

Mathematical Experiments with Mathematica

TU Bergakademie Freiberg. Computer Algebra System Mathematica

The Mathcad Workspace 7

MATLAB Project: Getting Started with MATLAB

APPM 2460 PLOTTING IN MATLAB

Here are a couple of warnings to my students who may be here to get a copy of what happened on a day that you missed.

Project 2: How Parentheses and the Order of Operations Impose Structure on Expressions

ECON 502 INTRODUCTION TO MATLAB Nov 9, 2007 TA: Murat Koyuncu

John's Tutorial on Everyday Mathcad (Version 9/2/09) Mathcad is not the specialist's ultimate mathematical simulator

Functions and Graphs. The METRIC Project, Imperial College. Imperial College of Science Technology and Medicine, 1996.

Basics of Computational Geometry

1 Maple Introduction. 1.1 Getting Started. 1.2 Maple Commands

EXCEL SPREADSHEET TUTORIAL

MATH 162 Calculus II Computer Laboratory Topic: Introduction to Mathematica & Parametrizations

L A TEX Primer. Randall R. Holmes. August 17, 2018

NetLogo Lab 3 Tutorial: Finding Peaks

MAT 090 Brian Killough s Instructor Notes Strayer University

Introduction to Mathematica and Graphing in 3-Space

Calculus II - Math 1220 Mathematica Commands: From Basics To Calculus II - Version 11 c

Appendix H: Introduction to Maple

MATH (CRN 13695) Lab 1: Basics for Linear Algebra and Matlab

Matlab Tutorial 1: Working with variables, arrays, and plotting

MTH 122 Calculus II Essex County College Division of Mathematics and Physics 1 Lecture Notes #11 Sakai Web Project Material

4. Mathematica. 4.1 Introduction. 4.2 Starting Mathematica Starting Mathematica from an X-terminal (Maths)

open a text line to annotate your work is Alt-7. From Eq. 2.5 we use the "sum" function in Mathematica to consider the (finite) sum

Ph3 Mathematica Homework: Week 6

Parametric Curves, Polar Plots and 2D Graphics

Calculus III. 1 Getting started - the basics

Essentials for the TI-83+

Linear algebra deals with matrixes: two-dimensional arrays of values. Here s a matrix: [ x + 5y + 7z 9x + 3y + 11z

STEPHEN WOLFRAM MATHEMATICADO. Fourth Edition WOLFRAM MEDIA CAMBRIDGE UNIVERSITY PRESS

Matlab for FMRI Module 1: the basics Instructor: Luis Hernandez-Garcia

CALCULUS II. Parametric Equations and Polar Coordinates. Paul Dawkins

Mathematica Proficiency and motivation for 6.1 and 6.2

Binary, Hexadecimal and Octal number system

Using the Equation Palette

Maple Quick Start. Maplesoft, a division of Waterloo Maple Inc.

Class #10 Wednesday, November 8, 2017

Constraint-based Metabolic Reconstructions & Analysis H. Scott Hinton. Matlab Tutorial. Lesson: Matlab Tutorial

Linear Methods of Applied Mathematics Classification of PDEs

Table of Laplace Transforms

Generating Functions

The first thing we ll need is some numbers. I m going to use the set of times and drug concentration levels in a patient s bloodstream given below.

How to prepare interactive Mathematica demonstrations for classroom

Introduction to the workbook and spreadsheet

The notebook interface is the usual one and is quite sophisticated. (It was used to create these notes, for instance.)

Introduction to L A TEX for MCS-236

2.9 Linear Approximations and Differentials

INTRODUCTION TO DERIVE - by M. Yahdi

2.2 Limit of a Function and Limit Laws

Matrix Inverse 2 ( 2) 1 = 2 1 2

Math 1206 Calculus Sec. 5.6: Substitution and Area Between Curves (Part 2) Overview of Area Between Two Curves

Variable Definition and Statement Suppression You can create your own variables, and assign them values using = >> a = a = 3.

Spring Pendulum * Muhammad Umar Hassan and Muhammad Sabieh Anwar

Vertical Line Test a relationship is a function, if NO vertical line intersects the graph more than once

Using Parameter Queries

MATLAB Basics. Configure a MATLAB Package 6/7/2017. Stanley Liang, PhD York University. Get a MATLAB Student License on Matworks

Name: Dr. Fritz Wilhelm Lab 1, Presentation of lab reports Page # 1 of 7 5/17/2012 Physics 120 Section: ####

Mathematica Primer.nb 1

INDEX INTRODUCTION...1

Transcription:

MmaGuide-GLG.nb Integrating Equations of Motion in Mathematica Gary L. Gray Assistant Professor Engineering Science and Mechanics The Pennsylvania State University 227 Hammond Building University Park, PA 6802 gray@engr.psu.edu Double-click the cell brackets on the right to open and close them. Introduction There is really only one thing you need to know about Mathematica it can do almost anything you would want to do mathematically. Of course, getting it to do anything is quite another matter. What you will learn here is probably much less than % of what Mathematica is capable of. This is a quick tutorial on how to use Mathematica for many of problems you will be solving this semester. For example, you will numerically solve ordinary differential equations (equations of motion), solve systems of algebraic equations, and plot many types of functions. If you have suggestions, comments, or corrections, please send them to me at the above email address. Ÿ Executing Commands in Mathematica Commands are entered in "cells". The vertical lines you see on the right are cell brackets and they define boundaries between groups of objects. Each cell has associated with it a certain set of attributes, but you only need be concerned with a couple of them. The cell containing this paragraph is a text cell as you can see in the toolbar at the top of the window. A text cell is used for adding comments and explanation to a notebook and is not executable. You can only do mathematics in Mathematica withing executable cells. Any time you start a new cell, it is an "Input cell" by default and Mathematica commands can be executed there since it is executable. Below you see an example of an input cell with a Mathematica command and the resulting output cell. The command is executed by placing the cursor anywhere within the cell and pressing either Shift-Return or Enter.

MmaGuide-GLG.nb 2 2 + 2 Cos@3.D 0.007297 When you execute the very first command in a Mathematica notebook (this is a Mathematica notebook and it runs in an application called the "front end"), Mathematica must start a program called the "kernel" which is the application that does all the computation. Therefore, you may experience a small delay the first time you execute an input cell as you wait for the kernel to start up. Note that Mathematica knows about constants such as p and E (the N command tells Mathematica to return a numerical value). You can get the "pi" from the palettes on the right or using some special keystrokes. N@pD 3.459 N@ED 2.7828 Without the N, you simply get p-e -E +p since Mathematica thinks of these as exact constants. This is an important thing to keep in mind about Mathematica. Mathematica Syntax You can use any name you like as a variable name, but it cannot have spaces, dashes, or begin with a number. The equals sign "=" assigns one thing to another. For example, this command sets q equal to 4. A semicolon suppresses the output. q = 4; We now set p equal to 5. p = 5;

MmaGuide-GLG.nb 3 If you ask Mathematica what p is, it returns what you would expect. p 5 p + q 9 Mathematica never forgets these values for p and q until you quit and restart the program. (Actually, there are ways to get it to forget them, but we won't get into them here.) In addition, I highly recommend that you start all variable names with a lower case letter since all Mathematica commands begin with capital letters and then there is no was you can use a variable name that is also a Mathematica command. Multiplication is represented by a space or an asterisk. pq 20 p * q 20 á Fancy Formatting Mathematica is capable of some pretty fancy formatting. Note that all Mathematica commands start with capital letters (e.g., cosine) and also note that the argument of each Mathematica command is contained in square brackets (more on this later). z = à - Cos@a xd x 2 +!!!!!!!!!!!! y - z 3 âx You can use the Palettes submenu of the File menu to get a whole bunch of palettes for formatting.

MmaGuide-GLG.nb 4 Ÿ The Double Equals Sign Mathematica uses the double equals sign "==" to equate one thing to another. This is not the same as an assignment given by the single equals sign. For example, say way want to solve the simultaneous equations x + y = 3 and x y = 4. You would enter the equations in the following way (notice that I have put more than one command in a single cell bracket): eq = x + y == eq2 = x - y == 2 x + y == x - y == 2 I then solve the equations using the Solve command: xysoln = Solve@8eq, eq2<, 8x, y<d 99x 3 2,y - 2 == The output from the solution is a list of replacement rules. A replacement rule has the form: left -> right That is, anywhere left appears, replace it with right. Here is how you use them. x. xysoln 9 3 2 = y. xysoln 9-2 = xy. xysoln 9-3 4 = This says to multiply x by y. Then the /. command tells Mathematica to substitute the replacement rule xysoln into x y. For now, don't worry about the extra square brackets that are hanging around.

MmaGuide-GLG.nb 5 Let's have a look at one more example. In[]:= list = 8d -> 4, e -> 3.2, f ->.2< Out[]= 8d 4, e 3.2, f.2< In[2]:= d. list Out[2]= 4 In[3]:= d + e + f + g. list Out[3]= 8.4 + g Notice it didn't substitute anything for g since it was not in our list. Ÿ You can get information about any command by entering a question mark followed by the command. Here is information on the Solve command we used above.? Solve Solve@eqns, varsd attempts to solve an equation or set of equations for the variables vars. Solve@eqns, vars, elimsd attempts to solve the equations for vars, eliminating the variables elims. Even more information can be obtained by using the double question mark.?? Solve Solve@eqns, varsd attempts to solve an equation or set of equations for the variables vars. Solve@eqns, vars, elimsd attempts to solve the equations for vars, eliminating the variables elims. Attributes@SolveD = 8Protected< Options@SolveD = 8InverseFunctions -> Automatic, MakeRules -> False, Method -> 3, Mode -> Generic, Sort -> True, VerifySolutions -> Automatic, WorkingPrecision -> Infinity<

MmaGuide-GLG.nb 6 Turn off annoying Mathematica warnings. fred = 2 2 freda = 3 General::spell : Possible spelling error: new symbol name "freda" is similar to existing symbol "fred". 3 These keep Mathematica from complaining when you name a variable freda when you already have a variable named fred. I execute these commands in almost every Mathematica notebook I create. Off@General::spellD Off@General::spellD Now watch what happens: joe = 3 3 joey = Doing Vector Operations To do the type of operations we need to do in this class, you first need to load some external packages. These are packages that ship with Mathematica, but are not loaded automatically. Needs@"Calculus`VectorAnalysis`"D Ÿ Defining a Vector Vectors are defines as lists enclosed in curly brackets.

MmaGuide-GLG.nb 7 f = 8, 2, 3< 8, 2, 3< g = 84, 5, 6< 84, 5, 6< dot = DotProduct@f, gd 32 cross = CrossProduct@f, gd 8-3, 6, -3< These operations also work on more than numbers. DotProduct@8l, m, n<, 8r, s, t<d lr+ ms+ nt CrossProduct@8l, m, n<, 8r, s, t<d 8-n s+ mt,nr- lt,-m r+ ls< á Picking out components of a vector or list. You can pick out one of the components of the vector (or, equivalently, a part of a list) by using the following notation. cross@@dd -3 cross@@2dd 6 You get the idea.

MmaGuide-GLG.nb 8 Now, recall xysoln. xysoln 99x 3 2,y - 2 == It is really a list of lists. Therefore: xysoln@@dd 9x 3 2,y - 2 = Therefore we need to do the following: xysoln@@, DD x 3 2 xysoln@@, 2DD y - 2 xysoln@@2, DD Part::partw : Part 2 of 99x 3 2,y - == does not exist. 2 99x 3 2,y - ==P2, T 2 Get it? One more example. Form a another type of list (actually, this is a 2 by 2 matrix): In[4]:= mat = 88, 2<, 83, 4<< Out[4]= 88, 2<, 83, 4<< In[5]:= mat@@dd Out[5]= 8, 2<

MmaGuide-GLG.nb 9 In[6]:= mat@@, 2DD Out[6]= 2 In[7]:= mat@@2dd Out[7]= 83, 4< In[8]:= mat@@2, DD Out[8]= 3 Solving Systems of Algebraic Equations We have already seen this, but Mathematica is very powerful!!! Ÿ Define 8 equations. These eight equations are the equations of motion of the slider-crank mechanism inside an IC engine.

MmaGuide-GLG.nb 0 eq = N + C x == 0 eq2 = C y - P == m C a C eq3 = B y - C y == m BC a Dy eq4 = B x - C x == m BC a Dx eq5 = C x L Cos@fD - C y L Sin@fD == I BC a BC + m BC a Dy a Sin@fD - m BC a Dx a Cos@fD eq6 = A x - B x == 0 eq7 = A y - B y == 0 eq8 = B x r Cos@qD + B y r Sin@qD + T == 0 N + C x == 0 -P + C y == a C m C B y - C y == a Dy m BC B x - C x == a Dx m BC L Cos@fD C x - L Sin@fD C y == -a Cos@fD a Dx m BC + a Sin@fD a Dy m BC + I BC a BC A x - B x == 0 A y - B y == 0 T + r Cos@qD B x + r Sin@qD B y == 0 Ÿ Solve the 8 equations of motion. These are the various forces in the problem.

MmaGuide-GLG.nb soln = Solve@8eq, eq2, eq3, eq4, eq5, eq6, eq7, eq8<, 8A x,b x,a y,b y,c x,c y,n,t<d 99T r Sin@qD H-P - a Dy m BC - a C m C L + L Hr Cos@qD Haa D x m BC - La Dx m BC - Sec@fD I BC a BC - L P Tan@fD - aa Dy m BC Tan@fD - La C m C Tan@fDLL, A x - L Ha a Dx m BC - La Dx m BC - Sec@fD I BC a BC - L P Tan@fD - aa Dy m BC Tan@fD - La C m C Tan@fDL, A y P + a Dy m BC + a C m C, N - -aa D x m BC + Sec@fD I BC a BC + L P Tan@fD + aa Dy m BC Tan@fD + La C m C Tan@fD,B x - L L Ha a Dx m BC - La Dx m BC - Sec@fD I BC a BC - L P Tan@fD - aa Dy m BC Tan@fD - La C m C Tan@fDL, C x - aa D x m BC - Sec@fD I BC a BC - L P Tan@fD - aa Dy m BC Tan@fD - La C m C Tan@fD, L B y P + a Dy m BC + a C m C,C y P + a C m C == If I wanted to assign the one of the eight solutions to a variable, I would do it as before (get used to this, because you will be doing it a lot). bx = B x. soln@@dd - aa D x m BC - La Dx m BC - Sec@fD I BC a BC - L P Tan@fD - aa Dy m BC Tan@fD - La C m C Tan@fD L Notice I didn't get the curly braces this time. Solving a Single Second-Order Ordinary Differential Equation. Ÿ You begin by defining the equation to be solved. For a single second-order ODE, you simply define the equation in the manner shown below. Notice, that you can include the initial conditions in the list (for numerical solutions, you must include the initial conditions). This equation happens to be a form of an equation called Duffing's equation. duffing = 9x''@tD +gx'@td - x@td + x@td 3 == A Cos@tD, x@0d == 0.6, x'@0d ==.25= 9-x@tD + x@td 3 +gx @td + x ² @td == A Cos@tD, x@0d == 0.6, x @0D ==.25= Notice that derivatives are indicated by "primes" and we have indicated that x is a function of time.

MmaGuide-GLG.nb 2 Ÿ Now use the command NDSolve to get the solution. á I now get the solution. Notice that I must assign values to the constants using replacement rules. The nice thing about using replacements rules is that the variables are not permanently assigned the numerical values. duffsoln = NDSolve@duffing. 8g -> 0.5, A -> 0.3<, x, 8t, 0, 00<D NDSolve::mxst : Maximum number of 000 steps reached at the point t == 58.94644205487398`. 88x InterpolatingFunction@880., 58.9464<<, <>D<< Mathematica has default limits on almost everything. We can integrate out past 58.9 seconds by setting the MaxSteps variable to a higher value. duffsoln = NDSolve@duffing. 8g -> 0.5, A -> 0.3<, x, 8t, 0, 00<, MaxSteps -> 5000D 88x InterpolatingFunction@880., 00.<<, <>D<< Mathematica returns an InterpolatingFunction as the solution. You can work with InterpolatingFunction's as you would with Sin or Cos or any other function. Ÿ Plot the solution versus time. We use the Plot command to plot the solution. First, let's look at a couple of simple examples.

MmaGuide-GLG.nb 3 á 2D Plot Example PlotAx 2 Cos@xD, 8x, 0, 20<E 300 200 00-00 5 0 5 20-200 Graphics

MmaGuide-GLG.nb 4 á 3D Plot Example Plot3D@Cos@xD Sin@yD, 8x, -p, p<, 8y, -p, p<d 0.5 0-0.5 - -2 0 2 0 2-2 SurfaceGraphics We can increase the resolution.

MmaGuide-GLG.nb 5 Plot3D@Cos@xD Sin@yD, 8x, -p, p<, 8y, -p, p<, PlotPoints -> 30D 0.5 0-0.5 - -2 0 2 0 2-2 SurfaceGraphics á Plot the Solution to the Differential Equation Remember that duffsoln is an InterpolatingFunction. Note that you must wrap the replacement with Evaluate when working with InterpolatingFunctions in this way.

MmaGuide-GLG.nb 6 Plot@Evaluate@x@tD. duffsolnd, 8t, 0, 00<D.5 0.5-0.5 20 40 60 80 00 - -.5 Graphics á Do a phase plot of the solution. Now we are plotting x versus x (x is on the y axis and x is on the x axis). Notice that we can take the derivative of x by simply a "prime" on it. ParametricPlot@Evaluate@8x@tD, x'@td<. duffsolnd, 8t, 0, 00<, PlotRange -> AllD.5 0.5 -.5 - -0.5 0.5.5-0.5 - -.5 Graphics

MmaGuide-GLG.nb 7 That was a pretty boring solution. Here is a mathematically chaotic one! We integrate again with adifferent set of parameters. Now we see why the replacement rules are so useful. duffsoln2 = NDSolve@duffing. 8g -> 0.3, A -> 0.3<, x, 8t, 0, 00<, MaxSteps -> 5000D 88x InterpolatingFunction@880., 00.<<, <>D<< Plot@Evaluate@x@tD. duffsoln2d, 8t, 0, 00<D.5 0.5-0.5 20 40 60 80 00 - -.5 Graphics

MmaGuide-GLG.nb 8 ParametricPlot@Evaluate@8x@tD, x'@td<. duffsoln2d, 8t, 0, 00<, PlotRange -> AllD 0.5 -.5 - -0.5 0.5.5-0.5 - Graphics Now on to a more complicated example. A System of Second-Order Equations. Ÿ For a system of second-order ODEs, you can also put the equations and initial conditions in a list. Now, the list just gets longer. These happen to be the equations of motion for a damped, forced, elastic pendulum. pendulum = 9q''@tD + 2 R'@tD q'@td R@tD + c q'@td m - g Cos@q@tDD R@tD == A Cos@w td ImR@tD 2 M, R''@tD + cr'@td m - R@tD Hq'@tDL 2 + k HR@tD - L0L m - g Sin@q@tDD == 0, q@0d == 0, q'@0d == 2.0, R@0D ==.2, R'@0D == 0= 9- g Cos@q@tDD R@tD + c q @td m + 2R @td q @td +q ² @td == R@tD A Cos@t wd mr@td 2, k H-L0 + R@tDL - g Sin@q@tDD + cr @td - R@tD q @td 2 + R ² @td == 0, q@0d == 0, m m q @0D == 2., R@0D ==.2, R @0D == 0=

MmaGuide-GLG.nb 9 á Set up a vector of the parameters and their values. params = 8L0 -> 0.5, m -> 0.25, k -> 0, c -> 0.06, g -> 9.8, A ->, w->3.6< 8L0 0.5, m 0.25, k 0, c 0.06, g 9.8, A, w 3.6< á Numerically solve the equations of motion after substituting in the parameters. pendsoln = NDSolve@ pendulum. params, 8q, R<, 8t, 0, 20<, MaxSteps -> 20000D 88q InterpolatingFunction@880., 20.<<, <>D, R InterpolatingFunction@880., 20.<<, <>D<< á Plot the trajectory. ParametricPlot@Evaluate@8 R@tD Cos@q@tDD, - R@tD Sin@q@tDD<. pendsolnd, 8t, 0, 20<, PlotPoints -> 000, PlotRange -> All, AspectRatio -> D 0.5 -.5 - -0.5 0.5.5-0.5 - -.5 Graphics

MmaGuide-GLG.nb 20 Ÿ More on Interpolating Functions We can combine them to form other quantities. For example, here is the velocity of the pendulum versus time. velocity = "#################################################### HR'@tDL 2 + HR@tD q'@tdl 2. pendsoln 9, IInterpolatingFunction@880., 20.<<, <>D@tD 2 InterpolatingFunction@880., 20.<<, <>D@tD 2 + InterpolatingFunction@880., 20.<<, <>D@tD 2 M= Plot@Evaluate@ velocityd, 8t, 0, 20<D 7 6 5 4 3 2 5 0 5 20 Graphics Mathematica can be quite wonderful!