Skip to content
Snippets Groups Projects

Compare revisions

Changes are shown as if the source revision was being merged into the target revision. Learn more about comparing revisions.

Source

Select target project
No results found

Target

Select target project
  • cprutean/sciprog2024
  • pclark3/sciprog2024
2 results
Show changes
Commits on Source (5)
%% Cell type:markdown id:d5db6b1d tags: %% Cell type:markdown id:d5db6b1d tags:
## Checkpoint 4 ## Checkpoint 4
### Aim ![ch4.jpg](ch4.jpg)
To write a Python program to read in data from a text file in a specified format, perform some simple processing and display the output using pyplot. ***“In the middle of the journey of our life I found myself within a dark woods where the straight way was lost.”***
― Dante Alighieri, Inferno
From a computing viewpoint this checkpoint demonstrates how to read data from a text file into suitable lists, perform some processing on these lists and then graphically display the results. Attempting to finish the course by jumping directly to the checkpoints may seem like a good idea, until it isn't. To get back on track, you may want to return to the [Week 4 tasklist](../WeeklyTasks/Week4.ipynb).
### Dataset
The voltage and current in an electronic circuit are sampled at a rate of 25kHz and the sampled data are written to a text file with the following format:
- The first three lines are comments detailing the equipment used and are not useful for plotting, these lines start with the "#" character.
- The following (several hundred) lines contain the data in the two floats separated by a " , ". The first data entry being voltage and the second the current.
### Task
The computing task is to write a Python program to read in data from the supplied file for voltage, V(t), and current, I(t), and to plot, via pyplot, the log power, being, p(t)=log(V(t)I(t)) where log is natural log to base e.
Your program should:
- Prompt for the filename to be read.
- Read in the required data from sample.txt file and display p(t) over the supplied range of t with suitable titles and axis labels.
- You must use a function of the form logpower(voltage,current) to form the p(t), and a main() program.
### Background
To successfully complete this checkpoint you should read carefully [Parsing Input](../CourseNotes/ParsingInput.ipynb).
### Input files
For this checkpoint there are 2 input files with which to test, and then run your code.
These are [short.txt](short.txt) for testing and [sample.txt](sample.txt) for the real run.
Both of these are in the current folder, so they can be read in and executed upon directly here. ***Be very careful not to WRITE into these files, as you may lose the measured data and will have to go to great lengths to recover it!***
### Checkpoint
- Test your program with both files, and make sure for each that the output is correcly displayed.
- You should prompt the user for the filename to use.
- Call a demonstrator, show them your code, and run your program for the two different datasets.
### Assessment
To pass this checkpoint:
- The programme works correctly for both input files.
- The programme uses a function of the form logpower(voltage,current) to form the p(t), and has a main() function.
- The program is well structured and laid out.
- It has sensible variable names that make clear their purpose.
The output graph has correct title and labels.
>Think carefully about what you are reading from the file and what you want to plot. Also, to deal with the comment line, remember that comment lines start with a ```#``` which you can test for. Look up on the web what the string function ```str.startswith()``` does, it may be useful!
### Next steps
Once you have completed this checkpoint, please continue with the [Week4](../WeeklyTasks/Week4.ipynb) tasklist.
%% Cell type:code id:c3ccaba4 tags: %% Cell type:code id:c3ccaba4 tags:
``` python ``` python
``` ```
......
Checkpoints/ch4.jpg

5.53 KiB

%% Cell type:markdown id:57457292 tags: %% Cell type:markdown id:57457292 tags:
# Checkpoint 4 Step-by-step guide to Writing a Computer Program # Checkpoint 4 Step-by-step guide to Writing a Computer Program
This checkpoint is different from the others to give you a break. You just have to work through it and make sure you understand and can run it. Then ask a demonstrator to check you off when you have completed it. This checkpoint is different from the others to give you a break. You just have to work through it and make sure you understand and can run it. Then ask a demonstrator to check you off when you have completed it.
## The problem ## The first problem
We want to write a program to display trignometric functions (sin, cos, tan, cot) and their first derivatives over their fundamental periods. The user should be allowed to choose interactively which function to plot. We want to write a program to display trignometric functions (sin, cos, tan, cot) and their first derivatives over their fundamental periods. The user should be allowed to choose interactively which function to plot.
## First steps ## First steps
Before writing any computer code, it is worth first thinking about how the logic of our program should work. Before writing any computer code, it is worth first thinking about how the logic of our program should work.
For this task, one may do the following: For this task, one may do the following:
- Prompt the user to choose between sin/cos/tan/cot - Prompt the user to choose between sin/cos/tan/cot
- For the function the user has chosen, determine fundamental period. - For the function the user has chosen, determine fundamental period.
- For the chosen function, calculate derivative. - For the chosen function, calculate derivative.
- Plot chosen function and derivative. - Plot chosen function and derivative.
The logic of this whole program will look as below: The logic of this whole program will look as below:
```python ```python
1. User chooses function, and we assign a value to each choice: sin=1, cos=2, tan=3, cot=4. 1. User chooses function, and we assign a value to each choice: sin=1, cos=2, tan=3, cot=4.
2. Calculation and Plotting 2. Calculation and Plotting
if user chose 1: if user chose 1:
determine fundamental period of sin(x) determine fundamental period of sin(x)
cos(x) is derivative of sin(x) cos(x) is derivative of sin(x)
plot sin(x) and cos(x) over the fundamental period plot sin(x) and cos(x) over the fundamental period
else if user chose 2: else if user chose 2:
determine fundamental period of cos(x) determine fundamental period of cos(x)
-sin(x) is derivative of cos(x) -sin(x) is derivative of cos(x)
plot cos(x) and -sin(x) over the fundamental period plot cos(x) and -sin(x) over the fundamental period
else if user chose 3: else if user chose 3:
determine fundamental period of tan(x) determine fundamental period of tan(x)
1/cos^2(x) is derivative of tan(x) #we worked out math separately 1/cos^2(x) is derivative of tan(x) #we worked out math separately
plot tan(x) and 1/cos^2(x) over the fundamental period plot tan(x) and 1/cos^2(x) over the fundamental period
else: #this is the only possible remaining option for the user, that is to choose 4 else: #this is the only possible remaining option for the user, that is to choose 4
determine fundamental period of cot(x) determine fundamental period of cot(x)
-1/sin^2(x) is derivative of cot(x) [we worked out math separately] -1/sin^2(x) is derivative of cot(x) [we worked out math separately]
plot cot(x) and -1/sin^2(x) over the fundamental period plot cot(x) and -1/sin^2(x) over the fundamental period
3. Done! 3. Done!
```` ````
>***Note***: For more complex programs it is worth doing this structure pen-and-paper before starting to write down any code in ```Python```. This style is called ***writing in pseudocode***, that is a universally understandable human-readable version of the logic of the program. There is an entire field called algorithmics which deals with this! >***Note***: For more complex programs it is worth doing this structure pen-and-paper before starting to write down any code in ```Python```. This style is called ***writing in pseudocode***, that is a universally understandable human-readable version of the logic of the program. There is an entire field called algorithmics which deals with this!
Let us now attempt to implement the above algorithm in ```Python```, below: Let us now attempt to implement the above algorithm in ```Python```, below:
%% Cell type:code id:2da1b363 tags: %% Cell type:code id:2da1b363 tags:
``` python ``` python
# Import necessary libraries and make our axes fonts and labels bold for a visually pleasing plot # Import necessary libraries and make our axes fonts and labels bold for a visually pleasing plot
import numpy as np import numpy as np
from matplotlib import pyplot as plt from matplotlib import pyplot as plt
plt.rcParams["font.weight"] = "bold" plt.rcParams["font.weight"] = "bold"
plt.rcParams["axes.labelweight"] = "bold" plt.rcParams["axes.labelweight"] = "bold"
# Allow user to choose which function to plot and also print a short description of what the program aims to achieve # Allow user to choose which function to plot and also print a short description of what the program aims to achieve
print("This is a short program to calculate and plot sin/cos/tan/cot and their derivatives over the fundamental period. \n") print("This is a short program to calculate and plot sin/cos/tan/cot and their derivatives over the fundamental period. \n")
user_choice = int(input("Please select your desired function: \n 1. sin(x) \n 2. cos(x) \n 3. tan(x) \n 4. cot(x) \n User choice: ")) user_choice = int(input("Please select your desired function: \n 1. sin(x) \n 2. cos(x) \n 3. tan(x) \n 4. cot(x) \n User choice: "))
# Calculate and plot # Calculate and plot
# Choice of sin # Choice of sin
if user_choice == 1: if user_choice == 1:
# Determine fundamental period of sin(x) # Determine fundamental period of sin(x)
period = np.arange(0, 2*np.pi, 0.001) period = np.arange(0, 2*np.pi, 0.001)
# Calculate sin(x) over the period # Calculate sin(x) over the period
function = np.sin(period) function = np.sin(period)
# Calculate derivative over the period # Calculate derivative over the period
derivative = np.cos(period) derivative = np.cos(period)
# Plot the results # Plot the results
plt.figure(figsize=(10,6)) plt.figure(figsize=(10,6))
plt.plot(period, function, label='sin(x)') plt.plot(period, function, label='sin(x)')
plt.plot(period, derivative, label='cos(x)') plt.plot(period, derivative, label='cos(x)')
plt.title("sin(x) and its derivative", fontsize='large', fontweight='bold') # Add title/lables plt.title("sin(x) and its derivative", fontsize='large', fontweight='bold') # Add title/lables
plt.xlabel("Angle (radians)") plt.xlabel("Angle (radians)")
plt.ylabel("f(x)") plt.ylabel("f(x)")
plt.legend() plt.legend()
plt.grid() plt.grid()
plt.show() plt.show()
# Choice of cos # Choice of cos
elif user_choice == 2: elif user_choice == 2:
# Determine fundamental period of cos(x) # Determine fundamental period of cos(x)
period = np.arange(0, 2*np.pi, 0.001) period = np.arange(0, 2*np.pi, 0.001)
# Calculate cos(x) over the period # Calculate cos(x) over the period
function = np.cos(period) function = np.cos(period)
# Calculate derivative over the period # Calculate derivative over the period
derivative = -np.sin(period) derivative = -np.sin(period)
# Plot the results # Plot the results
plt.figure(figsize=(10,6)) plt.figure(figsize=(10,6))
plt.plot(period, function, label='cos(x)') plt.plot(period, function, label='cos(x)')
plt.plot(period, derivative, label='-sin(x)') plt.plot(period, derivative, label='-sin(x)')
plt.title("cos(x) and its derivative", fontsize='large', fontweight='bold') # Add title/lables plt.title("cos(x) and its derivative", fontsize='large', fontweight='bold') # Add title/lables
plt.xlabel("Angle (radians)") plt.xlabel("Angle (radians)")
plt.ylabel("f(x)") plt.ylabel("f(x)")
plt.legend() plt.legend()
plt.grid() plt.grid()
plt.show() plt.show()
#Choice of tan #Choice of tan
elif user_choice == 3: elif user_choice == 3:
# Determine fundamental period of tan(x) [note this is different from that of sin & cos] # Determine fundamental period of tan(x) [note this is different from that of sin & cos]
period = np.arange(0, np.pi, 0.001) period = np.arange(0, np.pi, 0.001)
# Calculate tan(x) over the period # Calculate tan(x) over the period
function = np.tan(period) function = np.tan(period)
# Calculate derivative over the period # Calculate derivative over the period
derivative = 1/np.cos(period)**2 derivative = 1/np.cos(period)**2
# Plot the results # Plot the results
plt.figure(figsize=(10,6)) plt.figure(figsize=(10,6))
plt.plot(period, function, label='tan(x)') plt.plot(period, function, label='tan(x)')
plt.plot(period, derivative, label='1/cos^2(x)') plt.plot(period, derivative, label='1/cos^2(x)')
plt.title("tan(x) and its derivative", fontsize='large', fontweight='bold') # Add title/lables plt.title("tan(x) and its derivative", fontsize='large', fontweight='bold') # Add title/lables
plt.xlabel("Angle (radians)") plt.xlabel("Angle (radians)")
plt.ylabel("f(x)") plt.ylabel("f(x)")
plt.ylim([-10,10]) # Note tan has a discontinuity, so in order for plt.ylim([-10,10]) # Note tan has a discontinuity, so in order for
# the plot to look meaningful, we restrict the plotting range for the y-axis # the plot to look meaningful, we restrict the plotting range for the y-axis
plt.legend() plt.legend()
plt.grid() plt.grid()
plt.show() plt.show()
#Choice of cot #Choice of cot
else: else:
# Determine fundamental period of cot(x) [note this is different from that of sin & cos] # Determine fundamental period of cot(x) [note this is different from that of sin & cos]
period = np.arange(0, np.pi, 0.001) period = np.arange(0, np.pi, 0.001)
# Calculate cos(x) over the period # Calculate cos(x) over the period
function = 1 / np.tan(period) # Note NumPy does not have a cot function, but it is just 1/tan function = 1 / np.tan(period) # Note NumPy does not have a cot function, but it is just 1/tan
# Calculate derivative over the period # Calculate derivative over the period
derivative = - 1/np.sin(period)**2 derivative = - 1/np.sin(period)**2
# Plot the results # Plot the results
plt.figure(figsize=(10,6)) plt.figure(figsize=(10,6))
plt.plot(period, function, label='cot(x)') plt.plot(period, function, label='cot(x)')
plt.plot(period, derivative, label='-1/sin^2(x)') plt.plot(period, derivative, label='-1/sin^2(x)')
plt.title("cot(x) and its derivative", fontsize='large', fontweight='bold') # Add title/lables plt.title("cot(x) and its derivative", fontsize='large', fontweight='bold') # Add title/lables
plt.xlabel("Angle (radians)") plt.xlabel("Angle (radians)")
plt.ylabel("f(x)") plt.ylabel("f(x)")
plt.ylim([-10,10]) # Note cot has a discontinuity, so in order for plt.ylim([-10,10]) # Note cot has a discontinuity, so in order for
# the plot to look meaningful, we restrict the plotting range for the y-axis # the plot to look meaningful, we restrict the plotting range for the y-axis
plt.legend() plt.legend()
plt.grid() plt.grid()
plt.show() plt.show()
``` ```
%% Output %% Output
This is a short program to calculate and plot sin/cos/tan/cot and their derivatives over the fundamental period. This is a short program to calculate and plot sin/cos/tan/cot and their derivatives over the fundamental period.
Please select your desired function: Please select your desired function:
1. sin(x) 1. sin(x)
2. cos(x) 2. cos(x)
3. tan(x) 3. tan(x)
4. cot(x) 4. cot(x)
User choice: 1 User choice: 1
%% Cell type:markdown id:004db4eb tags: %% Cell type:markdown id:004db4eb tags:
## Some afterthoughts ## Some afterthoughts
Our program uses the simplest possible logic to perform the required tasks, without much thought. There are of course ways to optimise this, and so reduce the amount of code needed. However, this way of doing things gives one a very good and practical example of ***code reusability***, that is copy-pasting a part of code several times and only needing to perform minor alterations to change the results. Our program uses the simplest possible logic to perform the required tasks, without much thought. There are of course ways to optimise this, and so reduce the amount of code needed. However, this way of doing things gives one a very good and practical example of ***code reusability***, that is copy-pasting a part of code several times and only needing to perform minor alterations to change the results.
> Our program above has a **small bug**, which a user can easily detect while playing with the program. You should always be careful about situations like this, as any program should be bug-free, which is especially important given that some bugs can actually cause the program to halt and so fail to fulfil its intended purpose. > Our program above has a **small bug**, which a user can easily detect while playing with the program. You should always be careful about situations like this, as any program should be bug-free, which is especially important given that some bugs can actually cause the program to halt and so fail to fulfil its intended purpose.
Let us attempt below some minor changes to our code, that aim to achieve 2 things: Let us attempt below some minor changes to our code, that aim to achieve 2 things:
1. Reduce the amount of code and make it more efficient. 1. Reduce the amount of code and make it more efficient.
2. Eliminate the annoying bug. 2. Eliminate the annoying bug.
%% Cell type:code id:5fe8788b tags: %% Cell type:code id:5fe8788b tags:
``` python ``` python
# Import necessary libraries and make our axes fonts and labels bold for a visually pleasing plot # Import necessary libraries and make our axes fonts and labels bold for a visually pleasing plot
import numpy as np import numpy as np
from matplotlib import pyplot as plt from matplotlib import pyplot as plt
plt.rcParams["font.weight"] = "bold" plt.rcParams["font.weight"] = "bold"
plt.rcParams["axes.labelweight"] = "bold" plt.rcParams["axes.labelweight"] = "bold"
# Allow user to choose which function to plot and also print a short description of what the program aims to achieve # Allow user to choose which function to plot and also print a short description of what the program aims to achieve
print("This is a short program to calculate and plot sin/cos/tan/cot and their derivatives over the fundamental period. \n") print("This is a short program to calculate and plot sin/cos/tan/cot and their derivatives over the fundamental period. \n")
user_choice = int(input("Please select your desired function: \n 1. sin(x) \n 2. cos(x) \n 3. tan(x) \n 4. cot(x) \n User choice: ")) user_choice = int(input("Please select your desired function: \n 1. sin(x) \n 2. cos(x) \n 3. tan(x) \n 4. cot(x) \n User choice: "))
# Calculate and plot # Calculate and plot
# Choice of sin # Choice of sin
if user_choice == 1: if user_choice == 1:
# Determine fundamental period of sin(x) # Determine fundamental period of sin(x)
period = np.arange(0, 2*np.pi, 0.001) period = np.arange(0, 2*np.pi, 0.001)
# Calculate sin(x) over the period # Calculate sin(x) over the period
function = np.sin(period) function = np.sin(period)
# Calculate derivative over the period # Calculate derivative over the period
derivative = np.cos(period) derivative = np.cos(period)
#Define labels and bounds to use for plotting #Define labels and bounds to use for plotting
fn_label = "sin(x)" fn_label = "sin(x)"
deriv_label = "cos(x)" deriv_label = "cos(x)"
y_bound = 1.1 y_bound = 1.1
# Choice of cos # Choice of cos
elif user_choice == 2: elif user_choice == 2:
# Determine fundamental period of cos(x) # Determine fundamental period of cos(x)
period = np.arange(0, 2*np.pi, 0.001) period = np.arange(0, 2*np.pi, 0.001)
# Calculate cos(x) over the period # Calculate cos(x) over the period
function = np.cos(period) function = np.cos(period)
# Calculate derivative over the period # Calculate derivative over the period
derivative = -np.sin(period) derivative = -np.sin(period)
#Define labels and bounds to use for plotting #Define labels and bounds to use for plotting
fn_label = "cos(x)" fn_label = "cos(x)"
deriv_label = "-sin(x)" deriv_label = "-sin(x)"
y_bound = 1.1 y_bound = 1.1
#Choice of tan #Choice of tan
elif user_choice == 3: elif user_choice == 3:
# Determine fundamental period of tan(x) [note this is different from that of sin & cos] # Determine fundamental period of tan(x) [note this is different from that of sin & cos]
period = np.arange(0, np.pi, 0.001) period = np.arange(0, np.pi, 0.001)
# Calculate tan(x) over the period # Calculate tan(x) over the period
function = np.tan(period) function = np.tan(period)
# Calculate derivative over the period # Calculate derivative over the period
derivative = 1/np.cos(period)**2 derivative = 1/np.cos(period)**2
#Define labels and bounds to use for plotting #Define labels and bounds to use for plotting
fn_label = "tan(x)" fn_label = "tan(x)"
deriv_label = "1/cos^2(x)" deriv_label = "1/cos^2(x)"
y_bound = 10 y_bound = 10
#Choice of cot #Choice of cot
else: else:
# Determine fundamental period of cot(x) [note this is different from that of sin & cos] # Determine fundamental period of cot(x) [note this is different from that of sin & cos]
period = np.arange(0.001, np.pi, 0.001) period = np.arange(0.001, np.pi, 0.001)
# Calculate cos(x) over the period # Calculate cos(x) over the period
function = 1 / np.tan(period) # Note NumPy does not have a cot function, but it is just 1/tan function = 1 / np.tan(period) # Note NumPy does not have a cot function, but it is just 1/tan
# Calculate derivative over the period # Calculate derivative over the period
derivative = - 1/np.sin(period)**2 derivative = - 1/np.sin(period)**2
#Define labels and bounds to use for plotting #Define labels and bounds to use for plotting
fn_label = "cot(x)" fn_label = "cot(x)"
deriv_label = "-1/sin^2(x)" deriv_label = "-1/sin^2(x)"
y_bound = 10 y_bound = 10
# Plot the results # Plot the results
plt.figure(figsize=(10,6)) plt.figure(figsize=(10,6))
plt.plot(period, function, label=fn_label) plt.plot(period, function, label=fn_label)
plt.plot(period, derivative, label=deriv_label) plt.plot(period, derivative, label=deriv_label)
title_choice = fn_label + " and its derivative" #Build the title of the plot to match the chosen function title_choice = fn_label + " and its derivative" #Build the title of the plot to match the chosen function
plt.title(title_choice, fontsize='large', fontweight='bold') # Add title/lables plt.title(title_choice, fontsize='large', fontweight='bold') # Add title/lables
plt.xlabel("Angle (radians)") plt.xlabel("Angle (radians)")
plt.ylabel("f(x)") plt.ylabel("f(x)")
plt.ylim([-y_bound, y_bound]) plt.ylim([-y_bound, y_bound])
plt.legend() plt.legend()
plt.grid() plt.grid()
plt.show() plt.show()
``` ```
%% Output %% Output
This is a short program to calculate and plot sin/cos/tan/cot and their derivatives over the fundamental period. This is a short program to calculate and plot sin/cos/tan/cot and their derivatives over the fundamental period.
Please select your desired function: Please select your desired function:
1. sin(x) 1. sin(x)
2. cos(x) 2. cos(x)
3. tan(x) 3. tan(x)
4. cot(x) 4. cot(x)
User choice: 4 User choice: 4
%% Cell type:markdown id:66c48d22 tags: %% Cell type:markdown id:66c48d22 tags:
## More afterthoughts ## More afterthoughts
Now we seem to have removed the bug that existed (so one aim is checked - ***please read carefully the 2 codes to see the difference***) and have also reduced the amount of code that needed writing. We achieved the latter by decoupling the calculation and the plotting of the results, having devised a way in which the calculations produce standardised outputs that can then be used in a general way to plot them irrespective of the user's choice. Now we seem to have removed the bug that existed (so one aim is checked - ***please read carefully the 2 codes to see the difference***) and have also reduced the amount of code that needed writing. We achieved the latter by decoupling the calculation and the plotting of the results, having devised a way in which the calculations produce standardised outputs that can then be used in a general way to plot them irrespective of the user's choice.
Let us try now to improve the code even further, trying both to reduce the written amount and to make it (slightly) more efficient and general. Let us try now to improve the code even further, trying both to reduce the written amount and to make it (slightly) more efficient and general.
%% Cell type:code id:fea833cb tags: %% Cell type:code id:fea833cb tags:
``` python ``` python
# Import necessary libraries and make our axes fonts and labels bold for a visually pleasing plot # Import necessary libraries and make our axes fonts and labels bold for a visually pleasing plot
import numpy as np import numpy as np
from matplotlib import pyplot as plt from matplotlib import pyplot as plt
plt.rcParams["font.weight"] = "bold" plt.rcParams["font.weight"] = "bold"
plt.rcParams["axes.labelweight"] = "bold" plt.rcParams["axes.labelweight"] = "bold"
# Allow user to choose which function to plot and also print a short description of what the program aims to achieve # Allow user to choose which function to plot and also print a short description of what the program aims to achieve
print("This is a short program to calculate and plot sin/cos/tan/cot and their derivatives over the fundamental period. \n") print("This is a short program to calculate and plot sin/cos/tan/cot and their derivatives over the fundamental period. \n")
user_choice = int(input("Please select your desired function: \n 1. sin(x) \n 2. cos(x) \n 3. tan(x) \n 4. cot(x) \n User choice: ")) user_choice = int(input("Please select your desired function: \n 1. sin(x) \n 2. cos(x) \n 3. tan(x) \n 4. cot(x) \n User choice: "))
#Define matching labels and bounds to use for plotting #Define matching labels and bounds to use for plotting
fn_label = ["sin(x)","cos(x)","tan(x)","cot(x)"] fn_label = ["sin(x)","cos(x)","tan(x)","cot(x)"]
deriv_label = ["cos(x)","-sin(x)","1/cos^2(x)","-1/sin^2(x)"] deriv_label = ["cos(x)","-sin(x)","1/cos^2(x)","-1/sin^2(x)"]
y_bound = [1.1, 1.1, 10, 10] y_bound = [1.1, 1.1, 10, 10]
title_choice = fn_label[user_choice-1] + " and its derivative" #Build the title of the plot to match the chosen function title_choice = fn_label[user_choice-1] + " and its derivative" #Build the title of the plot to match the chosen function
# Define appropriate periods and calculate functions and matching derivatives # Define appropriate periods and calculate functions and matching derivatives
periods = [np.arange(0, 2*np.pi, 0.001), np.arange(0, 2*np.pi, 0.001), np.arange(0, np.pi, 0.001), np.arange(0.001, np.pi, 0.001)] periods = [np.arange(0, 2*np.pi, 0.001), np.arange(0, 2*np.pi, 0.001), np.arange(0, np.pi, 0.001), np.arange(0.001, np.pi, 0.001)]
functions = [lambda x: np.sin(x),lambda x: np.cos(x),lambda x: np.tan(x),lambda x: 1/np.tan(x)] functions = [lambda x: np.sin(x),lambda x: np.cos(x),lambda x: np.tan(x),lambda x: 1/np.tan(x)]
derivs = [lambda x: np.cos(x), lambda x: -np.sin(x), lambda x: 1/np.cos(x)**2, lambda x: -1/np.sin(x)**2] derivs = [lambda x: np.cos(x), lambda x: -np.sin(x), lambda x: 1/np.cos(x)**2, lambda x: -1/np.sin(x)**2]
# Calculate for the user's choice (note array are numbered 0-> n-1, while user has a choice of 1->n) # Calculate for the user's choice (note array are numbered 0-> n-1, while user has a choice of 1->n)
period = periods[user_choice-1] period = periods[user_choice-1]
function = functions[user_choice-1](period) function = functions[user_choice-1](period)
derivative = derivs[user_choice-1](period) derivative = derivs[user_choice-1](period)
# Plot the results # Plot the results
plt.figure(figsize=(10,6)) plt.figure(figsize=(10,6))
plt.plot(period, function, label=fn_label[user_choice-1]) plt.plot(period, function, label=fn_label[user_choice-1])
plt.plot(period, derivative, label=deriv_label[user_choice-1]) plt.plot(period, derivative, label=deriv_label[user_choice-1])
plt.title(title_choice, fontsize='large', fontweight='bold') # Add title/lables plt.title(title_choice, fontsize='large', fontweight='bold') # Add title/lables
plt.xlabel("Angle (radians)") plt.xlabel("Angle (radians)")
plt.ylabel("f(x)") plt.ylabel("f(x)")
plt.ylim([-y_bound[user_choice-1], y_bound[user_choice-1]]) plt.ylim([-y_bound[user_choice-1], y_bound[user_choice-1]])
plt.legend() plt.legend()
plt.grid() plt.grid()
plt.show() plt.show()
``` ```
%% Output %% Output
This is a short program to calculate and plot sin/cos/tan/cot and their derivatives over the fundamental period. This is a short program to calculate and plot sin/cos/tan/cot and their derivatives over the fundamental period.
Please select your desired function: Please select your desired function:
1. sin(x) 1. sin(x)
2. cos(x) 2. cos(x)
3. tan(x) 3. tan(x)
4. cot(x) 4. cot(x)
User choice: 4 User choice: 4
%% Cell type:markdown id:c4a0e13b tags: %% Cell type:markdown id:c4a0e13b tags:
## Final thoughts ## Final thoughts
In the final code example above, we made use of the fact that the program has a very rigid structure. This begins from the limited user choice, and the fact that each choice is independent of others, which can allow us to produce matching arrays that make use of anonymous functions to calculate the desired function and its derivative and plot the result. You will see more detailed examples of all the concepts used in this section as you advance through the course. In the final code example above, we made use of the fact that the program has a very rigid structure. This begins from the limited user choice, and the fact that each choice is independent of others, which can allow us to produce matching arrays that make use of anonymous functions to calculate the desired function and its derivative and plot the result. You will see more detailed examples of all the concepts used in this section as you advance through the course.
> Please note that the final code is significantly less human-friendly than the very first iteration we tried, and this is to be expected. **Programming is generally not a linear process**, and one never aims to write the final version of a code on the first try. It is best practice to adopt a trial and error approach (which most programmers do), where you interatively improve on the code, rather then hope to produce the final version from the first attempt. This practice also turns out to be significantly faster and more efficient than the alternatives. > Please note that the final code is significantly less human-friendly than the very first iteration we tried, and this is to be expected. **Programming is generally not a linear process**, and one never aims to write the final version of a code on the first try. It is best practice to adopt a trial and error approach (which most programmers do), where you interatively improve on the code, rather then hope to produce the final version from the first attempt. This practice also turns out to be significantly faster and more efficient than the alternatives.
### Next steps
## The second problem
Write a `Python` program to read in data from a text file in a specified format, perform some simple processing and display the output using pyplot.
More precisely, we want to write a `Python` program to read in data from the supplied file for voltage, V(t), and current, I(t), and to plot the log power, being, p(t)=log(V(t)I(t)) where log is natural log to base e.
From a computing viewpoint this problem demonstrates how to read data from a text file into suitable lists, perform some processing on these lists and then graphically display the results.
>### Dataset
>The voltage and current in an electronic circuit are sampled at a rate of 25kHz and the sampled data are written to a text file with the following format:
>- The first three lines are comments detailing the equipment used and are not useful for plotting, these lines start with the "#" character.
>- The following (several hundred) lines contain the data in the two floats separated by a " , ". The first data entry being voltage and the second the current.
>### Input files
>For this exercise there are 2 input files with which to test, and then run our code.
>These are [short.txt](short.txt) for testing and [sample.txt](sample.txt) for the real run.
>Both of these are in the current folder, so they can be read in and executed upon directly here. ***Be very careful not to WRITE into these files, as you may lose the measured data and will have to go to great lengths to recover it!***
## First steps
Let us think how our program might be structured:
```python
1. User is prompted for the filename to be read.
2. Read in the required data from the file.
3. Create a function of the form logpower(voltage,current) to form the p(t).
4. Display p(t) over the supplied range of t with suitable titles and axis labels.
5. Done!
````
>### Background
>- To successfully complete this we should read carefully [Parsing Input](../CourseNotes/ParsingInput.ipynb).
>- Think carefully about what we are reading from the file and what we want to plot. Also, to deal with the comment line, we have to remember that comment lines start with a ```#``` which we can test for. Look up on the web what the string function ```str.startswith()``` does, it may be useful!
%% Cell type:code id:d7786a8f tags:
``` python
# Define function to calculate logpower
def logpower(voltage,current):
p = math.log(voltage*current)
return p
# Construct our program to read in the data and plot it appropriately
# Note we do this by creating a function that takes a filename as its parameter
def PlottingProgram(filename):
print('Plotting file: {}'.format(filename))
filein = open(filename,"r")
voltage_data = []
current_data = []
logpower_data = []
for line in filein.readlines():
if not line.startswith('#'):
tokens = line.split(",")
voltage_data.append(float(tokens[0]))
current_data.append(float(tokens[1]))
logpower_data.append(logpower(float(tokens[0]),float(tokens[1])))
filein.close()
# Now go on and process time_data and voltage_data as two lists
time_s = np.arange(len(logpower_data))/25e3
time_ms = time_s * 1e3
# Plot the results
plt.plot(time_s,logpower_data,"r") # Plot in red
plt.title("Power vs. time") # Add title/lables
plt.xlabel("Time (s)")
plt.ylabel("Power")
plt.grid()
plt.show()
file_name = input("Filename: ")
PlottingProgram(file_name)
```
%% Cell type:markdown id:744ab8ea tags:
Below, we can test automatically for both files we are interested in. ***Building tests for all posibilities of a program is a useful practice when dealing with multiple inputs and use cases!***
%% Cell type:code id:690f1836 tags:
``` python
PlottingProgram('short.txt')
PlottingProgram('sample.txt')
```
%% Cell type:markdown id:d7bbdc86 tags:
## Next steps
1. Make sure you have fully understood all of the above (that is all you have to do for this checkpoint). 1. Make sure you have fully understood all of the above (that is all you have to do for this checkpoint).
2. Get Checkpoint 4 checked off by a TA. 2. Get Checkpoint 4 checked off by a TA.
3. Please continue with the [Week4](../WeeklyTasks/Week4.ipynb) tasklist. 3. Please continue with the [Week4](../WeeklyTasks/Week4.ipynb) tasklist.
%% Cell type:code id:63a15de4 tags: %% Cell type:code id:63a15de4 tags:
``` python ``` python
``` ```
......
File moved
File moved