Programming Assignment 1: Simple Pendulum

ESM 3124 - Fall 2020

Due: 10-26-2020

This assignment will focus on numerically solving the equation of motion for a simple pendulum.

The assignment is broken into 2 main parts. All submissions need to be in a single .zip folder labeled

'Your_Name_Simple_Pendulum.zip'.

0.1 Euler's method (10 pts)

At any instant in time, the state of the pendulum is completely defined by its angle and angular

velocity.

1. Write a local function with call: dSdt = my_ode(t, S, ...) that converts the 2nd order

nonlinear equation of motion θ¨ + (g/L) sin θ = 0 into two 1st order ODEs that are returned

in the array dSdt. Input variables are the current time t, the current state S, and any other

parameters you need to complete the calculations. The function has a single output dSdt,

which is the rate of change of the state at the current instant in time. This function will be

called by your own eulers_method() function and Matlab's built-in ode23() to simulate

the pendulum.

2. Write a local function with call:

[S_array,t_array] = eulers_method(@(t, S) my_ode(t, S, ...),tspan, S0, delta_t)

that simulates the system trajectory using Euler's method. The output variables are both

n-row arrays, where n is the number of simulated time values. Each row of S_array con-

tains the state of the system at an instant in time, and each row of t_array is the corre-

sponding time value. The input variables include a function handle to your local function

@(t, S) my_ode(t, S, ...) defined in the previous step, the initial / final times in the

(1 x 2) array tspan, the initial state array S0, and the time step size delta_t (a single-

value array).

0.2 Simulate Motion and Animate Trajectory (10 pts)

Using the two functions described above you will simulate the motion of a 1 m long pendulum with

1 kg mass, for 2 different initial states:

State 1: θ0 = 10

◦

, ω0 = 0 (rad/s)

State 2: θ0 = 80

◦

, ω0 = 0 (rad/s)

1. Simulate 10 seconds of motion using Matlab's built-in ode23() function. Use a relative

tolerance of RelTol=1e-6

2. Simulate 10 seconds of motion using your eulers_method() function.

3. Simulate 10 seconds of motion using the exact solution for the linearized equations of motion.

These can be coded using either a local function or anonymous function.

For each initial state, determine an appropriate step size delta_t so that your eulers_method()

function appears close to Matlab's ode23() results. Once determined, for each initial state:

1. Make a plot of θ(t) that compares each of the three methods used above. See Figure 1

2. Print a 200 dpi .png image for each initial state. Name the file theta_vs_time_state_#.png

(replace `#' with the appropriate number for each state)

3. Animate the motion simulated with your eulers_method() function for state 2 and save

an .avi video file named animated_simple_pendulum.avi

* For plots / animations, appropriately label the x and y axes, including units. Provide a

legend that clearly labels each line.

1

(a) Results for initial state 1 (b) Results for initial state 2

Figure 1: Simulations of motion for initial state 1 and state 2.

* Add a title that indicates what state is being graphed, and what the step size delta_t is

equal to.

* Make sure the axis limits are constant during the animation (i.e., the axis should not change

during the entire animation)

* The use of pre-existing animation functions (e.g., animatedline(), comet(), or comet3())

are expressly prohibited. Use of any such function will result in a score of 0 for the entire

assignment.

Submission Format

While we are not strictly grading your code for proper format and comments, you should abso-

lutely provide useful comments in your code to explain what each portion of your code does, and

how. This will be useful to you for the 2nd and 3rd programming assignments, and aid the TA

and Professor Domann as they grade your code.

In a single .zip folder named `Your_Name_Simple_Pendulum.zip' you will submit:

1. A single .m file that contains your code and local functions described in Part 0.1. The TA

and Professor Domann should be able to load this code into Matlab, press Run, and have

your code execute successfully.

2. A 200 dpi .png image named theta_vs_time_state_#.png for each initial state that com-

pares your eulers_method() to Matlab's ODE23() and the exact analytic solution for the

linearized problem.

3. An '.avi' movie file named animated_simple_pendulum.avi, containing an animation of

your solution.

2

Recommended Code Outline:

General code structure:

Break your code up into several big picture steps / parts. In Matlab these steps will likely be

the titles of different `cells' in your code created using two comment symbols `%% Step Name'

You will be building onto this code during your 2nd coding assignment, so make sure this

one is well organized and commented.

For each function provide:

Comments that concisely described what the function does

A description of the input / output variables including variable type and size (e.g., a (m x

3) matrix of doubles)

* It's hard to over-emphasize how challenging it is to write code when you can't provide clear

statements for the items listed above. That's a strong indication you have little idea what

the function / code you're writing is attempting to do.

** To see how to format the header of a function, in Matlab, right click in the Current Folder

and click `New File'→ `Function'. You can copy / paste the function template for your local

functions. Update the commented sections as appropriate.

3

欢迎咨询51作业君