tutor-notes-2005-sheet-1

tutor-notes-2005-sheet-1 - M11115 ‘5 VTJWKS The...

Info icon This preview shows pages 1–6. Sign up to view the full content.

View Full Document Right Arrow Icon
Image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Image of page 2
Image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Image of page 4
Image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Image of page 6
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: M11115 ‘5? VTJWKS The University of Queensland School of Engineering MECH27OO Engineering Analysis I (2005) Exercise Sheet 1 This exercise is designed to work through the process of editing and running two Python programs using the Mechanical/Civil computers that run MS-Windows. It is assumed that you have logged into your lab computer and that you have access to a H: drive which is the same storage area as your ITS email account. Python as a calculator First, we will use the Python environment as a calculator and attempt to estimate the numerical value of 7r using only simple arithmetic operations. Step 1. Make a mech2700 directory in your H: drive. This will be your working directory for the exercise. Step 2. Start the IDLE programming environment and you should be greeted with an interactive session containing something like the following: ‘ Python 2.3.4 (#1, Feb 2 2005, 12:11:53) [GCC 3.4.2 20041017 (Red Hat 3.4.2—6.fc3)] on linux2 Type "copyright", "credits" or "license()" for more information. **************************************************************** Personal firewall software may warn about the connection IDLE makes to its subprocess using this computer’s internal loopback interface. This connection is not visible on any external interface and no data is sent to or received from the Internet. **************************************************************** IDLE 1.0.3 >>> The triple-character prompt at the bottom of the text indicates that the Python interpreter is waiting for your input. Step 3. Instead of giving the Python interpreter commands directly, open a new editing window using the menu entry File~> New Window and enter the code listed here. Remember to customise the introduc- tory string to reflect your name and the date that you actually typed the code. If you save the text with the menu command File—>Save as... and supply the file name as h:\mech2700\machinl.py, the edit window will be labelled (in the top bar) with that file name and the content will be syntax- highlighted' in colour. Note that, depending on network conditions, it may be faster to do all of your work on drive C: which is the local hard-disk. The C: drive will be wiped clean each night so you should move your work files to network drive H: at the end of your session. # file: machin1.py """MECH2700: Using Python as a calculator. Compute and display an estimate of pi using Machin’s formula. Use only three terms in the arctangent expansions. Check against another estimate obtained via the math library. P. A. Jacobs 7 mar 2005' Step 4. School of Engineering, UQ. 09-Sep—03 from math import * print "Begin program Machinl..." a1 = 1.0 / 5 a2 pow(a1,3) / 3 a3 = pow(a1,5) / 5 bi = 1.0 / 239 b2 = pow(b1,3) / 3 b3 = pow(b1,5) / 5 pi_1 = 4 * (4 * (a1 - a2 + a3) — (b1 — b2 + b3)) print "Truncated series estimate is ", pi_1 pi_2 = 4.0 * atan(1.0) print "Math library estimate is ", pi_2 print "Difference is ", pi_1 - pi_2 print "End of program Machinl." Run your Python script using the Run—éRun module menu entry in the edit window and you should get the following in the original intrepreter session. >>> ================================ RESTART ================================ >>> Begin program Machinl... Truncated series estimate is 3.14162102933 Math library estimate is 3.14159265359 Difference is 2.83757352415e-O5 End of program Machinl. >>> Programming Python We will now generalise the calculation by allowing the arc-tangent series to be truncated at some number of terms that will be specified at run-time rather than at program design time. Step 1. Step 2. Create a new file machin2 .py and enter the code: # file: machin2.py """Sample Python program for MECH2700 Computes and displays an estimate of pi using Machin’s and a series expansion for the arctangent function. Check against another estimate obtained via the math library. P. A. Jacobs School of Engineering, UQ. 09-Sep-03 I! II II import math # __________________________________________________________________ def atan_series(x, n): ' "Computes atan(x) with a truncated series expansion of n terms." xpower = x sum = x sign = 1 for i in range(1,n): xpower = xpower * x * x sign = -1 * sign term - sign * xpower / (2 * i + 1) sum = sum + term return sum def machin(n) : "Computes pi using Machin’s formula." return 4 * (4 * atan_series(0.2,n) - atan_series(1.0/239,n)) # _______________________________________________________________ print "Begin program Machin2..." text = raw_input("Enter a value for the number of terms: ") i = int(text) pi_1 = machin(i) print "Truncated series estimate is ", pi_1, " for n=", i pi_2 = 4.0 * math.atan(1.0) print "Math library estimate is ", pi_2 print "Difference is ", pi_1 - pi_2 print "End of program Machin2." Be careful to get the indentation of the lines exactly as shown; the block structure of the code is indicated to the interpreter via this indentation. Each indent level here is 4 characters. IDLE will handle the details for you if you press TAB while the cursor is at the start of the line. Note that the BA CKSPA C'E key will take you back an indent level of 4 spaces. Execute the program mentioned in the previous section. This time, you will need to supply an integer value at the program’s prompt. Step 3. Print the output. This can be done via the File—er'nt window menu entry. You may also save the window’s contents. Notes and Questions 1. Python allows us to inspect its namespace. In the interpreter window, try the command dir() to see the list of named objects that are visible at the top level. After running a module as we did above for machin2 .py, You should see name of the function ’machin’ as one of the items in the list. Try the command help(machin) to see what information is available. 2. Try to invoke the 7r-estimation function with the command: >>> machin(5) and note the error in the numerical estimation. Note, also, that the programming environment gave you hints about what arguments are required for the machin function once you typed the open-parenthesis character. 3. Invoke the arctangent function using the command >>> atan_series(1.0,5) How good is this approximation to 7r / 4? How many terms are needed to get it accurate to 6 decimal places? Python 2.3.4 (#1, Feb 2 2005, 12:11:53) [GCC 3.4.2 20041017 (Red Hat 3.4.2—6.fc3)] on linuxZ Type "copyright", "credits" or "license()" for more information. **********************************************k***************** Personal firewall software may warn about the connection IDLE makes to its subprocess using this computer's internal loopback interface. This connection is not visible on any external interface and no data is sent to or received from the Internet. **************************************************************** IDLE 1.0.3 >>> ====== ————— ====#—==== ————— ====—— RESTART =— =—=== ~=—— —==== >>> Begin program Machinl... Truncated series estimate is 3.14162102933 Math library estimate is 3.14159265359 Difference is 2.83757352415e—05 End of program Machinl. >>> #~——===————==== ————— ====———===—~ RESTART ==== — ==== —==== ——==—— === >>> Begin program MachinZ... Enter a value for the number of terms: 4 Truncated series estimate is 3.14159177218 for n= 4 Math library estimate is 3.14159265359 Difference is —8.8l407615783e—07 End of program MachinZ. >>> # file: machin2.py """Sample Python program for MECH27OO Computes and displays an estimate of pi using Machin's and a series expansion for the arctangent function. Check against another estimate obtained via the math library. P. A. Jacobs School of Engineering, UQ. O9~Sep—O3 I! " 1' import math def atan_series(x, n): "Computes atan(x) with a truncated series expansion of n terms." xpower = x sum = x sign = l for i in range(1,n): xpower = xpower * x * x sign = ~l * sign term = Sign * xpower / (2 * i + 1) sum = sum + term return sum def machin(n): "Computes pi using Machin's formula." return 4 * (4 * atan_series(0.2,n) — atan_series(l.O/239,n)) print “Begin program MachinZ..." text = raw_input("Enter a value for the number of terms: ") i = int(text) pi_1 = machin(i) print "Truncated series estimate is ", pi_l, " for n=", i pi_2 = 4.0 * math.atan(1.0) print "Math library estimate is ", pi_2 print "Difference is ", pi_l — pi_2 print "End of program MachinZ." ...
View Full Document

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern