Skip to content

This study contains a series of cases in order to go over all Python features

Notifications You must be signed in to change notification settings

SeckinTataroglu/Python-Case-Studies-2

Repository files navigation

Python-Case-Studies-2

Task 1. Plot your data.

𝑥 is a variable defined in the limits between 0<𝑥<6𝜋. Define an array of 𝑥 values to span this range, and produce two data sets, 𝑦(𝑥) and 𝑧(𝑥), which are given by

equation

equation

here 𝜉 is a uniform random number between 0 and 1. Plot 𝑦(𝑥) and 𝑧(𝑥) versus 𝑥 (together, in one panel), making sure your plot looks professional.

Task 2. A vector ratio function.

Define a Python function 'ratio' of two arrays 𝑔 and ℎ :

equation

where 𝑖=0,1,2...,𝑁−1 and 𝑁 is the number of points in the arrays. Write your function in the best Python practice, e.g., with a docstring, and also such that it prints an error but does not crash when the function cannot be executed.

Task 3. Conditional area calculation

Write a code that performs the following tasks:

  • (1) Defines a function 𝑓(𝑥,𝑦) for 0≤𝑥≤1 and 0≤𝑦≤1 .
  • (2) Method 1. Using the general python programming approach (Workshop sections 1 to 5), write a piece of code that calculates the area, 𝐴1 , inside the square 0≤𝑥,𝑦≤1 within which 𝑓(𝑥,𝑦)>0 . To do this, create a grid of 𝑥 and 𝑦 values with number of points 𝑁>1 , and use 'for' or 'while' loops to calculate the area. Your code should be general to work with any function 𝑓(𝑥,𝑦).
  • (3) Method 2. Now write another piece of code that uses Numpy for the same task. Do not use for or while loops in this case. Hint: work with a 𝑁×𝑁 matrix representing your (𝑥,𝑦) coordinate grid.
  • (4) Calculate the execution times 𝑡1 and 𝑡2 that it takes the two codes to obtain the result. Print the results (values of both 𝐴 and 𝑡 for both tasks). Use '.format' statement.
  • (5) Now repeat the task for a range of values for N from 𝑁=3 to some very large value, such as 𝑁=10,000 . Make a plot showing how the execution times 𝑡1 and 𝑡2 depend on 𝑁 . Note: consult Section 8 of the workshop for Matplot library use, if needed.
  • (6) Based on the plots, describe approximately how the execution times depend on 𝑁 for 𝑁≫1 for method 1 and method 2. Give simple arguments that explain the obtained scaling with 𝑁.

Note that both Methods 1 and 2 should work for any function 𝑓(𝑥,𝑦).

Hints:

The following meausures the execution time:

import time

start_time = time.clock()
main()
Execution_time =  time.clock() - start_time

where main() is the piece of the code which execution time you are measuring.

About

This study contains a series of cases in order to go over all Python features

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published