Web-Books
in the Austria-Forum
Austria-Forum
Web-Books
Informatik
Programming for Computations – Python - A Gentle Introduction to Numerical Simulations with Python 3.6, Volume Second Edition
Page - 99 -
  • User
  • Version
    • full version
    • text only version
  • Language
    • Deutsch - German
    • English

Page - 99 - in Programming for Computations – Python - A Gentle Introduction to Numerical Simulations with Python 3.6, Volume Second Edition

Image of the Page - 99 -

Image of the Page - 99 - in Programming for Computations – Python - A Gentle Introduction to Numerical Simulations with Python 3.6, Volume Second Edition

Text of the Page - 99 -

4.3 Exercises 99 Exercise4.6:FunctionforAreaofaRectangle Write a program that computes the area A = bc of a rectangle. The values of b and c should be user input to the program. Also, write the area computation as a function that takes b and c as input parameters and returns the computed area. Let the program print the result to screen along with an appropriate text. Run the programwithb=2andc=3 toconfirmcorrectprogrambehavior. Filename:function_area_rectangle.py. Exercise4.7:Averageof Integers Writeaprogramthatgetsan integerN>1fromtheuserandcomputestheaverage of all integers i = 1,.. .,N. The computation should be done in a function that takesN as input parameter. Print the result to the screen with an appropriate text. Run theprogramwithN =5andconfirmthatyouget thecorrectanswer. Filename:average_1_to_N.py. Exercise4.8:When DoesPythonCheckFunctionSyntax? You are told that, when Python reads a functiondefinition for the first time, it does notexecute the function,but still checks thesyntax. Now,youcomeupwith somecode lines toconfirmthat this is thecase. Hint You may, for example, use a print command and a deliberate syntax error in a modification ofball_function.py (note that the modified code is one of those quick β€œone-time” tests you might make for yourself, meant to be deleted once you have theanswer). Filename:when_check_function_syntax.py. Exercise4.9:Find CrossingPointsofTwo Graphs Consider two functionsf(x)=x andg(x)=x2 on the interval [βˆ’4,4]. Write a program that, by trial and error, finds approximately for which values of x the two graphs cross, i.e., f(x) = g(x). Do this by considering N equally distributedpointsontheinterval,ateachpointcheckingwhether |f(x)βˆ’g(x)|< , where is somesmallnumber.LetN and beuser input to theprogramandlet the resultbeprinted to screen.RunyourprogramwithN =400and =0.01.Explain the output from the program. Finally, try also other values ofN, keeping the value of fixed.Explainyourobservations. Filename:crossing_2_graphs.py. Exercise4.10:Linear Interpolation Some measurements yi, i = 0,1,.. .,N, of a quantity y have been collected regularly, once every minute, at times ti = i, i = 0,1,.. .,N. We want to find the valuey in between the measurements, e.g., at t = 3.2 min. Computing suchy values is called interpolation. Letyourprogramuse linearinterpolation tocomputeybetweentwoconsecutive measurements: 1. Find i such that ti ≀ t≀ ti+1.
back to the  book Programming for Computations – Python - A Gentle Introduction to Numerical Simulations with Python 3.6, Volume Second Edition"
Programming for Computations – Python A Gentle Introduction to Numerical Simulations with Python 3.6, Volume Second Edition
Title
Programming for Computations – Python
Subtitle
A Gentle Introduction to Numerical Simulations with Python 3.6
Volume
Second Edition
Authors
Svein Linge
Hans Petter Langtangen
Publisher
Springer Open
Date
2020
Language
English
License
CC BY 4.0
ISBN
978-3-319-32428-9
Size
17.8 x 25.4 cm
Pages
356
Keywords
Programmiersprache, Informatik, programming language, functional, imperative, object-oriented, reflective
Category
Informatik
Web-Books
Library
Privacy
Imprint
Austria-Forum
Austria-Forum
Web-Books
Programming for Computations – Python