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.
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.
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
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.
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.
This is a statement of a model of the motion of the ball.You are probably familiar with it and know the analytical solutions:
Where are the horizontal position,vertical position,horizontal velocity and vertical velocity respectively.Also and .
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:
We next approximate the derivatives by small finite differences.
Note that in the limit approaches . We can rewrite the above as.
We can write our equations in this form
The finite difference approximation we used to obtain these equations is an example of the Euler algorithm.
In general, for the first order differential: Where is a function of
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.
Our equations above are examples of finite difference equations and is the time step.We can now follow and in time. We begin with initial values and then iterate.If is sufficiently small we will obtain numerical answers close to the solutions of the original differential equations.
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
In the code above a projectile is launched at an angle . The while loop iterates through each time interval and computes the position and velocity of the ball at that time.It stops when the ball returns to the ground().I used 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 coordinates as well as animations of the simulation.
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.