Lab 5: Realistic Integration

21
Lab 5: Realistic Integration CS 282

description

Lab 5: Realistic Integration. CS 282. Questions?. Any question about… SVN Permissions? General Usage? Doxygen Remember that Project 1 will require it However, Assignment 2 is good practice place Assignment 2 Is posted on the class website. The Plan. Go over methods of integration - PowerPoint PPT Presentation

Transcript of Lab 5: Realistic Integration

Page 1: Lab 5: Realistic Integration

Lab 5: Realistic Integration

CS 282

Page 2: Lab 5: Realistic Integration

Any question about…◦ SVN

Permissions? General Usage?

◦ Doxygen Remember that Project 1 will require it However, Assignment 2 is good practice place

◦ Assignment 2 Is posted on the class website

Questions?

Page 3: Lab 5: Realistic Integration

Go over methods of integration◦ Euler’s method◦ Analytical◦ Approximation

Examine the boat problem

Program Runge-Kutta◦ Compare against analytical and euler methods◦ Plot position vs. time (due next week’s lab)

The Plan

Page 4: Lab 5: Realistic Integration

We use integration to move our system forward in time given a force/control

If you remember the falling ball lab, we used an Euler method (kind of like cheating) to integrate◦ velocity = old_velocity + delta_time * acceleration◦ position = old_position + delta_time * velocity

There exists better methods, however, for integration.

Integration

Page 5: Lab 5: Realistic Integration

Let’s say we are trying to solve the equationx – 5 = 0

Analytical methods solve for the exact answer◦ In this case it’s easy to come up the analytical answer

x = 5 Let’s assume we weren’t so smart. Then we could

develop an algorithm to numerically solve this problem on a computer◦ The algorithm could have the computer “guess” the

answer by testing different values of x. We could specify our “error tolerance” to speed this process up.

Analytical vs. Numerical

Page 6: Lab 5: Realistic Integration

Forces on the boat:B = Buoyancy T = ThrustW = Weight R = Resistance

If B = W, we can ignore the movement in y

The Boat Problem

Page 7: Lab 5: Realistic Integration

new_velocity = old_velocity + acceleration * dt new_position = old_position + new_velocity * dt

Pros:◦ Easy to compute◦ Easy to understand◦ This means it’s also easy to program

Cons:◦ Highly erroneous◦ Not stable (i.e. tends to not converge to the true solution)

Euler Solution

Page 8: Lab 5: Realistic Integration

Open up this week’s framework Examine rigid_body.cpp

◦ Notice that it is conveniently modeled like a boat◦ Take a look at the function “euler_update_state”

Make sure you understand each line in this function This function is the Euler method of updating the

boat’s state Compile the code Run the executable

Now let’s examine the analytical solution

Exercise: Euler’s Method

Page 9: Lab 5: Realistic Integration

Here we see the equations for the velocity, distance traveled, and acceleration of the boat

Pro◦ The one, true solution

Cons:◦ Often hard to derive◦ For complex systems, infeasible to implement

Analytical Solution

Page 10: Lab 5: Realistic Integration

Why not take advantage of the fact that we’re computer scientists?

Pros◦ More realistic compared to Euler’s method◦ Utilizes computational power◦ We do not have to analytically solve (which for

some systems is infeasible) Con

◦ Not as accurate since its an approximation◦ Power depends on skill of programmer and the

computational capacity of the computer

Numerical Method

Page 11: Lab 5: Realistic Integration

The numerical method we will be using today is called “Runge-Kutta”

Essentially, Runge-Kutta uses Euler’s Method and splits up the integration into four parts. It then takes the average of these parts, and computes the new integration.

The reason this works better is because after each split, it uses a newly computed control to re-integrate.

What’s Runge-Kutta?

Page 12: Lab 5: Realistic Integration

Overview of Runge-Kutta First, we compute k1 using Euler’s method of integration.

After this, we integrate while adding half of k1 to our velocity (or state). This allows us to solve for k2.

Next we integrate while adding half of k2, and solve for k3.

Finally, we integrate by completely adding k3 and solve for k4.

After averaging together all kx, we now have a new velocity (or state).

Page 13: Lab 5: Realistic Integration

4th-orderRunge-Kutta Method

xi xi + h/2

xi + h

k1

k2

k3

k4

sizesteph

kyhxfhk

kyhxfhk

kyhxfhk

yxfhk

kkkkf

_,

21,

21

21,

21

,

2261

34

23

12

1

4321

f

Credit to:Dr. E.W. SandtCivil Engineering DepartmentTexas A&M University

Page 14: Lab 5: Realistic Integration

Runge-Kutta Method (4th Order) Example

Consider Exact Solution

The initial condition is:

The step size is:

2xydxdy

x222 exxy

10 y

1.0h

Credit to:Dr. E.W. SandtCivil Engineering DepartmentTexas A&M University

Page 15: Lab 5: Realistic Integration

The 4th order Runge-Kutta The example of a single step:

104829.12261

109499.0104988.1,1.01.0,

104988.02/.1,05.01.021,

21

10475.005.1,05.01.021,

21

1.0011.01,01.0,

4321n1n

34

223

12

21

kkkkyy

fkyhxfhk

kfkyhxfhk

fkyhxfhk

fyxfhk

Credit to:Dr. E.W. SandtCivil Engineering DepartmentTexas A&M University

Page 16: Lab 5: Realistic Integration

The goal for today is to implement Runge-Kutta for simulating the movement of a boat.◦ And if you finish early, to start working on the graph

due next week.

At the beginning of next week’s lab, you must turn in a graph comparing the position of the boat over time using◦ the Euler Method (already implemented)◦ the Analytical Method (plug into the equations)◦ Runge-Kutta

Exercise: Implement Runge-Kutta

Page 17: Lab 5: Realistic Integration

Let’s do the first step of Runge-Kutta◦ Follow the Euler method and compute k1◦ This will involve you computing the Force and

Acceleration This gives us k1 = delta_time * acceleration

Now that we have k1, we can use this to compute the halves (k2 and k3)

Exercise: Implement Runge-Kutta

Page 18: Lab 5: Realistic Integration

Step two starts off nearly the same◦ However, when computing force, be sure to add

half of k1 to your velocity F = thrust - (drag * (velocity + k1 /2) )

◦ Solve for acceleration◦ Compute k2 like before

Step three is exactly the same as step two, except use k2 now.

Exercise: Implement Runge-Kutta

Page 19: Lab 5: Realistic Integration

Step four (surprise!) is exactly the same◦ Except, since we are integrating over the entire period, we

do not halve our duration◦ Solve for k4 now

We need to average together our kx’s◦ k2 and k3 account for double because we integrated only

half of them.◦ So, we have 6 parts in total to average.

Finally, add the average to your velocity, and compute position like normal.

Exercise: Implement Runge-Kutta

Page 20: Lab 5: Realistic Integration

Go to lab5.cpp, and look in the DrawGLScene◦ Comment out the euler method, and add the

appropriate call to the runge-kutta method

Examine Runge-Kutta running

It will be hard to visually notice a difference. Instead, run the Euler method for a short period, and compare the numbers.

Exercise: Implement Runge-Kutta

Page 21: Lab 5: Realistic Integration

For next week, you will be turning in a graph comparing the three methods.◦ Euler Method (already implemented), Analytical

Method (plug into the equations), Runge-Kutta◦ Output the position at each time step to a file◦ Copy those numbers into excel (or the software of

your choice) and plot a graph over time comparing the positions for each method.

◦ For the analytical method, you can a) Program the equations and run it or b) Plug in time, solve them, and put the numbers

into the graph

Comparison