Seite - 46 - in Programming for Computations – Python - A Gentle Introduction to Numerical Simulations with Python
Bild der Seite - 46 -
Text der Seite - 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.
Programming for Computations – Python
A Gentle Introduction to Numerical Simulations with Python
- Titel
- Programming for Computations – Python
- Untertitel
- A Gentle Introduction to Numerical Simulations with Python
- Autoren
- Svein Linge
- Hans Petter Langtangen
- Verlag
- Springer Open
- Datum
- 2016
- Sprache
- englisch
- Lizenz
- CC BY-NC 4.0
- ISBN
- 978-3-319-32428-9
- Abmessungen
- 17.8 x 25.4 cm
- Seiten
- 248
- Schlagwörter
- Programmiersprache, Informatik, programming language, functional, imperative, object-oriented, reflective
- Kategorie
- Informatik