Week 8

  • Uploaded by: Victor Miclovich
  • 0
  • 0
  • June 2020
  • PDF

This document was uploaded by user and they confirmed that they have the permission to share it. If you are author or own the copyright of this book, please report to us by using this DMCA report form. Report DMCA


Overview

Download & View Week 8 as PDF for free.

More details

  • Words: 615
  • Pages: 3
Python Review... Victor Miclovich November 4, 2009

Noise • What’s happening? • I expect lots of questions by now... including working programs which non of you has ever sent me

Select Exercises • functions... we are all going to design functions – A recursive function of your choice; check http://wikipedia.org if you don’t know any... The aim of this exercise is to see you design functions from observing math! f (x) = x(x − 1)!, 0! = 1 ∀x ⊂ Z • Classful programming: but first, a re-discussion of procedural programming

Introduction to Scientific computing with Python C = 21 F = ( 9 / 5 ) ∗C + 32 print F 1

If you have done some C programming... we could have written: #include <s t d i o . h> int main ( ) { int C = 2 1 ; int F = ( 9 / 5 ) ∗ C + 3 2 ; 1

This is integer division... for practice, force it to be true division

1

p r i n t f ( ”F i s %f ” ,F ) ; return 0 ; } I will show you guys two examples (C and Python)... I guess it is because I want you guys to get used to multi-language programming. Later, you might have to extend the power of a program by writing using a faster language.

Problems Using the squareroot function2 . Consider the vertical motion of a ball. How much time does it take for the ball to reach the height yc ? The answer is straightforward. When y = yc we have 1 yc = v0 t − gt2 2 That equation is a quadratic function... which we solve with respect to t. Rearranging we have: 1 2 gt − v0 t + yc = 0 2 And using the well-known formula3 for the two solutions of a quadratic equation, we find     p p v0 + v02 − 2gyc v0 − vo2 − 2gyc , t2 = t1 = g g There are two solutions because the ball reaches the height yc on its way up (t = t1 ) and on its way down (t = t2 > t1 ). The Python program??? We can use functions found in the math library v0 = 5 g = 9.81 yc = 0 . 2 import math t 1 = ( v0 − math . s q r t ( v0 ∗∗2 − 2∗ g∗ yc ) ) / g t 2 = ( v0 + math . s q r t ( v0 ∗∗2 − 2∗ g∗ yc ) ) / g p r i n t ’ At t=%g s and %g s , t h e h e i g h t i s %g m’ % ( t1 , t2 , yc ) The output from this program is At t=0.0417064 s and 0.977662 s, the height is 0.2 m. 2

>>>import math >>>math.sqrt(4) 3 buffalo method

2

Importing modules in Python Modules are just collections of functions and hence also known as libraries; We can import like sos... import math and then access individual functions in the module with the module name as prefix as in x = math . s q r t ( y ) The other alternative is to import only the functions that you want... from math import s q r t # you can a l s o import e v e r y t h i n g from math import ∗ 4

4

When you import everything, you may just write function names including the arguments/values everything function takes as input

3

Related Documents

Week 8
December 2019 21
Week-8
November 2019 16
Week 8
April 2020 10
Week 8
December 2019 15
Week 8
June 2020 11
Week 8
December 2019 19

More Documents from ""

Mobile Apps In Health
June 2020 13
Sci Programing 2
June 2020 11
Lecture 1; Python
December 2019 14