Computation as a tool(Part 1):Basics of simulations,falling balls and Euler's algorithm

Hey Brilliant!In this note I will try to introduce the basic techniques of computer simulations and give a few elementary examples to illustrate the power of computation.

\[\huge{\text{Introduction}}\]

Scientists of many disciplines are often fond of saying that mathematics is a 'tool' they use for solving problems.While 'tool' is not a name most mathematicians are comfortable with,I would like to introduce an equally powerful but lesser known 'tool',computation.

Computers were used to simulate the behaviour of this little guy Computers were used to simulate the behaviour of this little guy

Computation is now an integral part of contemporary science and it is having a profound effect on the way we do science. We are starting to advance from traditional 'wet labs' where physical objects are used for performing experiments,towards 'dry labs' where equivalent experiments are crunched using a computer model.

We are now at a time where scientists have managed to simulate complete organisms,portions of the universe and even brains!While traditional tools such as numerical analysis and symbolic manipulation are important in computation,the key difference is that simulations are done with a minimum of analysis,they focus on explanatory modes of learning

Choosing a language\huge{\text{Choosing a language}}

Python Python

There is no single best programming language like there is no single best natural language. However I will be using python in this note series.Sure, it might be a relatively slow language,but it is also very readable.

Python enthusiasts like to say that languages like C and C++ were made to make life easier for the computer but python was made to make life easier for the programmer.We will also be using external python libraries like pygame and matplotlib for creating animations and visuals to go along with the simulations.Now that we are done with the prerequisites,let's get cracking.

2D Motion\huge{\text{2D Motion}}

projectile projectile

Consider a particle near the surface of the Earth exhibiting projectile motion and subject to a single force,the force of gravity.If we assume that air resistance is negligible,from Newton's second law,we can get the particle's position as a function of time.

g=d2xdt2g=-\frac{d^{2}x}{dt^{2}}

This is a statement of a model of the motion of the ball.You are probably familiar with it and know the analytical solutions:

x(t)=vx(0)tx(t) = v_{x}(0)t

vx(t)=vx(0)v_x(t) = v_x(0)

y(t)=y(0)+uyt12gt2y(t)=y(0) + u_{y}t - \frac{1}{2}gt^{2}

vy(t)=vy(0)gtv_y(t) = v_y(0) - gt

Where x,y,vx,vyx,y,v_x,v_y are the horizontal position,vertical position,horizontal velocity and vertical velocity respectively.Also vx(t)=vx(t)cos(θ)v_x(t)=v_x(t)cos(\theta) and vy(t)=vy(t)sin(θ)v_y(t)=v_y(t)sin(\theta).

You are probably familiar with the model above but nevertheless,we will determine the motion of the particle numerically in order to introduce the tools that we will need in a familiar context. We begin by expressing the equations as first order differential equations:

dxdt=ux\frac{dx}{dt}=u_x

dydt=vy\frac{dy}{dt}=v_y

dvydt=g\frac{dv_y}{dt}=-g

dvxdt=0\frac{dv_x}{dt}=0

We next approximate the derivatives by small finite differences.

y(t+Δt)y(t)Δt=v(t) \frac{y(t+\Delta t) - y(t)}{\Delta t} = v(t)

v(t+Δt)v(t)Δt=g \frac{v(t+\Delta t) - v(t)}{\Delta t} = -g

Note that in the limit Δt \Delta t approaches 00. We can rewrite the above as. y(t+Δt)=y(t)+v(t)Δty(t+\Delta t)=y(t) + v(t)\Delta t

v(t+Δt)=v(t)gΔtv(t+\Delta t)=v(t) - g \Delta t We can write our equations in this form y(t+Δt)=y(t)+vy(t)Δty(t+\Delta t)=y(t) + v_y(t)\Delta t

vy(t+Δt)=vy(t)gΔtv_y(t+\Delta t)=v_y(t) - g \Delta t

x(t+Δt)=x(t)+vx(t)Δtx(t+\Delta t)=x(t) + v_x(t)\Delta t

vx(t+Δt)=vx(t)v_x(t+\Delta t)=v_x(t) The finite difference approximation we used to obtain these equations is an example of the Euler algorithm.

In general, for the first order differential: dydx=f(x)\frac{dy}{dx}=f(x) Where f(x)f(x) is a function of xx yn+1=yn+f(xn)Δxy_{n+1}=y_{n}+f(x_n)\Delta x

In computation we can only deal with a countable number of input(as continuous input would require infinite computing power),thus such types of numerical algorithms,where we can obtain a desired value from previous values,are essential when running simulations.

Coding it all up\huge{\text{Coding it all up}}

Our equations above are examples of finite difference equations and Δt\Delta t is the time step.We can now follow y(t),x(t),vx(t)y(t),x(t),v_x(t) and vy(t)v_y(t) in time. We begin with initial values and then iterate.If Δt\Delta t is sufficiently small we will obtain numerical answers close to the solutions of the original differential equations.

Euler's method Euler's method

The image above demonstrates the discrepancy between the curve and its polygonal approximation.

I shall describe the syntax used in each line of the program using comments.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
from math import *
theta = pi/4 #Initial angle of launch
dt = 0.0001 #The time step of the motion
u = 10 #Launch velocity
g = 9.8 #Gravitational Acceleration
t = 0 #Starting time
x , y = 0,0 #Starting coordinates
vx,vy=u*cos(theta),u*sin(theta) #Initial vertical and horizontal speeds
Max_Height = 0 #Maximum Height,initially set to zero


while y >= 0: #Follow the ball while it hasn't gone below the y position of its initial position
     y = y + vy*dt #use Euler's algorithm
     x = x + vx*dt
     vy = vy - g*dt
     t = t + dt #Step up the time
     if y > Max_Height: #Update the value of the Maximum Height whenever a larger value of y is found
          Max_Height = y

print """Total flight time:%.3f seconds
Range of projectile:%.3f meters
Maximum height is:%.3f meters
""" %(t,x,Max_Height)

In the code above a projectile is launched at an angle θ\theta. The while loop iterates through each time interval dtdt and computes the position and velocity of the ball at that time.It stops when the ball returns to the ground(y<0y<0).I used Δt=0.0001\Delta t=0.0001 which gave results accurate to three decimal places.

Just by tracking the ball's position and velocity we have found information such as the total flight time,the range of the projectile,its mean height throughout the motion and its maximum height without involving ourselves too much in the mathematics.

Though doing the math itself is very easy here,such simulations are really helpful for more realistic situations where there are too many factors(drag,altitude,even the gravitational attraction of the moon) that affect the system.In those cases we can easily extend our simulation to get acceptable approximations in an easy way.

Using external libraries we can obtain plots of the (x,y)(x,y) coordinates as well as animations of the simulation.

Plot of the height of the projectile Plot of the height of the projectile

Summary\huge{\text{Summary}}

The goal of our investigation here was to show how simulations can be used to simplify problems.Computer simulations,like laboratory experiments are not substitutes for thinking but are tools we can use to understand natural phenomena.

Hopefully I will soon write notes on simulations of more realistic physical systems(Motion with drag,Damped oscillatory motion,chaotic systems..etc). Thank you for bearing with me through out this long note and I hope I managed to give you a glimpse of how cool computation can be.

#Physics #Mechanics #ComputerScience #ComputationalPhysics #ComputerSimulations

Note by Thaddeus Abiy
7 years ago

No vote yet
1 vote

  Easy Math Editor

This discussion board is a place to discuss our Daily Challenges and the math and science related to those challenges. Explanations are more than just a solution — they should explain the steps and thinking strategies that you used to obtain the solution. Comments should further the discussion of math and science.

When posting on Brilliant:

  • Use the emojis to react to an explanation, whether you're congratulating a job well done , or just really confused .
  • Ask specific questions about the challenge or the steps in somebody's explanation. Well-posed questions can add a lot to the discussion, but posting "I don't understand!" doesn't help anyone.
  • Try to contribute something new to the discussion, whether it is an extension, generalization or other idea related to the challenge.
  • Stay on topic — we're all here to learn more about math and science, not to hear about your favorite get-rich-quick scheme or current world events.

MarkdownAppears as
*italics* or _italics_ italics
**bold** or __bold__ bold

- bulleted
- list

  • bulleted
  • list

1. numbered
2. list

  1. numbered
  2. list
Note: you must add a full line of space before and after lists for them to show up correctly
paragraph 1

paragraph 2

paragraph 1

paragraph 2

[example link](https://brilliant.org)example link
> This is a quote
This is a quote
    # I indented these lines
    # 4 spaces, and now they show
    # up as a code block.

    print "hello world"
# I indented these lines
# 4 spaces, and now they show
# up as a code block.

print "hello world"
MathAppears as
Remember to wrap math in \( ... \) or \[ ... \] to ensure proper formatting.
2 \times 3 2×3 2 \times 3
2^{34} 234 2^{34}
a_{i-1} ai1 a_{i-1}
\frac{2}{3} 23 \frac{2}{3}
\sqrt{2} 2 \sqrt{2}
\sum_{i=1}^3 i=13 \sum_{i=1}^3
\sin \theta sinθ \sin \theta
\boxed{123} 123 \boxed{123}

Comments

Combining Physics and Computers ... That makes already cool Physics cooler !!!

Very nice!

Michael Mendrin - 7 years ago

HI, try visiting www.sciencefront.webs.com for cool ideas and interesting news and facts of science

Kshitij Khandelwal - 7 years ago

Please keep writing. This was really nice and interesting.

Himanshu Arora - 7 years ago

very good work my son is 17 and use simulations in physics

aliki patsalidou - 7 years ago

Really helpful thanks

Mardokay Mosazghi - 7 years ago

Log in to reply

Glad you think so. :)

Thaddeus Abiy - 7 years ago

Awesome note-Keep up the good work!!

Can you please tell me how to add the block of code in the note??

Eddie The Head - 7 years ago

Log in to reply

You can add code by placing your code in between two lines of ```. Look at this for clarification

Thaddeus Abiy - 7 years ago

Great Note!

Ameya Salankar - 7 years ago

Very nice! Congrats!

Bruna Torman - 7 years ago

Great post! Awaiting part 2.

Anthony Georgilas - 7 years ago

Good

Platinum Grieger - 7 years ago

I am trying to find a new language for programming, as you didn't mention java in your note, what do you think would best if I want to pursue programming for making apps.

Hardipinder Singh - 7 years ago

Log in to reply

It depends on what platform you are coding for.Android or IOS?

Thaddeus Abiy - 7 years ago

Log in to reply

I am more interested in android but if time comes I would like to go for ios also

Hardipinder Singh - 7 years ago

Log in to reply

@Hardipinder Singh I'm no expert on this but I have heard that Java is Definitely the way to go for Android development and Objective-C is absolutely necessary for IOS development. Of-course there are other options..

Thaddeus Abiy - 7 years ago

Log in to reply

@Thaddeus Abiy Thank you sir, and if you may please elaborate these other options.

Hardipinder Singh - 7 years ago

Log in to reply

@Hardipinder Singh Following the release of the ASE platform,it was made possible to code for Android in Python,Perl,Lua,Beanshell...etc..

Thaddeus Abiy - 7 years ago

Log in to reply

@Thaddeus Abiy Thank you again :-)

Hardipinder Singh - 7 years ago

I'mso damn cconfused

Äräháàñ Shâh - 7 years ago

Physics with computer, world is getting digital now. Oh yeah!

Nirupam Singh - 7 years ago

This was info of great value. Thanx

Shekhar Lolage - 6 years, 12 months ago

(y)

Dhanush Wali - 7 years ago
×

Problem Loading...

Note Loading...

Set Loading...