Welcome to Scribd, the world's digital library. Read, publish, and share books and documents. See more
Download
Standard view
Full view
of .
Save to My Library
Look up keyword
Like this
1Activity
0 of .
Results for:
No results containing your search query
P. 1
Sci Programing 2

Sci Programing 2

Ratings: (0)|Views: 20 |Likes:
Published by Victor Miclovich
Just something about scientific computing in Python
Just something about scientific computing in Python

More info:

Published by: Victor Miclovich on Dec 02, 2009
Copyright:Attribution Non-commercial

Availability:

Read on Scribd mobile: iPhone, iPad and Android.
download as PDF, TXT or read online from Scribd
See more
See less

12/02/2009

pdf

text

original

 
Follow-up on Scientific programmingas of Wednesday(or last week)
Victor MiclovichNovember 11, 2009
y
(
t
) =
v
0
t
12
gt
2
The above expression that says a lot about the trajectory of a ball.It actually computes the height of a ball in vertical motion. What if wethrow the ball with an initial velocity having an angle
θ
with the horizontal
plane
”?This kind of problem has been encountered by many of you... at least if you went to a high school physics class. The ball will follow a trajectory
y
=
(
x
) through the air, where
(
x
) =
x
tan
θ
12
v
2
o
gx
2
cos
2
θ
+
y
0
In this expression, is a horizontal coordinate... the distance in the direc-tion of the
x
axis
,
g
is the acceleration of gravity,
v
0
is the size
1
of the initialvelocity which makes an angle
θ
with the
x
axis, and (0
,y
0
) is the initialposition of the ball. Our programming goal is to make a program evaluatingequation above. The program should write out the value of all the involvedvariables and what their units are.
Solution:
We use the
SI
System and assume that
v
0
has been given inkm/h;
g
= 9
.
81
m/s
2
;
x
,
y
, and
y
0
are measure in meters; and
θ
in degrees.The program has got four parts; that are natural.
Initialization of input data
Importation of functions and
π
from
math
conversion of 
v
0
and
θ
to m/s and radians, respectively.
1
I use size or magnitude here, velocity has speed and direction, don’t forget simplePhysics!
1
 
And, finally, the evaluation of the right hand-side of the expression.The complete program is shown below
2
:
program 1
Python solving for
y
g = 9.81 #m/s**2v0 = 15 # km/htheta = 60 # degreesx = 0.5 # my0 = 1 # mprint """\v0 = %.1f km/htheta = %d degreesy0 = %.1f m\""" % (v0, theta, y0, x)from math import pi, tan, cos# convert v0 to m/s# and theta to radians:v- = v0/3.6theta = theta*pi/180y = (x*tan(theta) - 1/(2*v0**2)*g*x**2 / ((cos(theta))**2) + y0)print ’y = %.1f m’ % y
Explanation of code
The backslash in the triple-quoted multi-line string makes the string con-tinue on the next line without a newline. This means that removing thebackslash results in a blank line above the v0 line and a blank line betweenx and y lines in t he output on the screen. Another point to note is theexpression 1/(2*v0**2), which might seem as a candidate for unintendedinteger division. However, the conversion of v0 to m/s involves a division by3.6, which results in v0 being
float
, and therefore 2*v0**2 being
float
.We shall execute this scripts using Spyder
3
.
2
We choose to write out all numerical values with one decimal
3
I hope to configure all these machines with PyLab (plotting), SciPy(signal and imageprocessing), Matplotlib (2D and 3D graphing) and Numpy (linear Algebra) packages...so you won’t have any excuses about heavy course works when you can do things withPython.
2
 
Some pointers to take note of:
Understand the problem you want to solve
Understand what the program is supposed to do
Understand how to translate the problem description into a series of Python statements
Another equally important step is: Testing!A complete solution to programming exercises therefore consists of twoparts: the program text and a demonstration that the program works cor-rectly.
Some Python words to know
object: anything that a variable (name) can refer to
4
(number, string,function, module,...)
variable: name of an object
statement: an instruction to the computer, usually written on a linein a Python program (multiple statements on a line must be separatedby semicolons)
expression: a combination of numbers, text, variables, and operatorsthat results in a new object, when being evaluated
assignment: a statement binding an evaluated expression (object) toa variable (name)
algorithm: detailed recipe for how to solve a problem by programming
code: program text (or synonym for program)
implementation: same as code
executable: the file we run to start a program
verification: providing evidence that the program works correctly
debugging: locating and correcting errors in a program.
4
But objects can exist without being bound to a name:
print ’Hello!’
first makes astring object of the text in quotes and then the contents of this string object, without aname, is printed
3

You're Reading a Free Preview

Download
/*********** DO NOT ALTER ANYTHING BELOW THIS LINE ! ************/ var s_code=s.t();if(s_code)document.write(s_code)//-->