Page - 46 - in Programming for Computations β Python - A Gentle Introduction to Numerical Simulations with Python
Image of the Page - 46 -
Text of the Page - 46 -
46 2 BasicConstructions
Thevertices (βcornersβ) of the polygonhave coordinates .x1;y1/, .x2;y2/, : : :,
.xn;yn/, numberedeither in a clockwiseor counter clockwise fashion. TheareaA
of the polygoncan amazingly be computedby just knowing the boundary coordi-
nates:
AD 1
2 j.x1y2Cx2y3C Cxn 1ynCxny1/
.y1x2Cy2x3C Cyn 1xnCynx1/j :
Write a functionpolyarea(x, y) that takes two coordinate arrayswith the ver-
tices as arguments and returns the area. Assume that x and y are either lists or
arrays.
Test the function on a triangle, a quadrilateral, and a pentagonwhere you can
calculate theareabyalternativemethods forcomparison.
Hint SincePython lists andarrayshas0as theirfirst index, it iswise to rewrite the
mathematical formula in termsofvertex coordinatesnumberedasx0;x1;:: :;xn 1
andy0;y1;:: :;yn 1.
Filename:polyarea.py.
Exercise2.6:Averageof integers
Writeaprogramthatgetsan integerN >1 fromtheuserandcomputes theaverage
of all integers i D 1;:: :;N . The computation should be done in a function that
takesN as input parameter. Print the result to the screenwith an appropriate text.
Run theprogramwithN D5andconfirmthatyouget thecorrectanswer.
Filename:average_1_to_N.py.
back to the
book Programming for Computations β Python - A Gentle Introduction to Numerical Simulations with Python"
Programming for Computations β Python
A Gentle Introduction to Numerical Simulations with Python
- Title
- Programming for Computations β Python
- Subtitle
- A Gentle Introduction to Numerical Simulations with Python
- Authors
- Svein Linge
- Hans Petter Langtangen
- Publisher
- Springer Open
- Date
- 2016
- Language
- English
- License
- CC BY-NC 4.0
- ISBN
- 978-3-319-32428-9
- Size
- 17.8 x 25.4 cm
- Pages
- 248
- Keywords
- Programmiersprache, Informatik, programming language, functional, imperative, object-oriented, reflective
- Category
- Informatik