Skip to content
Snippets Groups Projects
Commit 13d35f1d authored by cprutean's avatar cprutean
Browse files

Update 6 files

- /CodeExamples/AppendFunction.ipynb
- /CodeExamples/BasicComplex.ipynb
- /CodeExamples/ComplexImpedance.ipynb
- /CodeExamples/ComplexQuadrant.ipynb
- /CodeExamples/CosineRule.ipynb
- /CodeExamples/CosPlot.ipynb
parent dbfd05f4
No related branches found
No related tags found
No related merge requests found
%% Cell type:markdown id:059058d7 tags: %% Cell type:markdown id:059058d7 tags:
# Append Function # Append Function
%% Cell type:code id:d0b2e63b tags: %% Cell type:code id:d0b2e63b tags:
``` python ``` python
""" """
Demonstration of Append function that works for any variable type Demonstration of Append function that works for any variable type
""" """
def append(first,second): def append(first,second):
""" """
A generic append function A generic append function
""" """
l = first + second l = first + second
return l return l
def main(): # Form two lists
# Form two lists fl = [1,2,3,5]
fl = [1,2,3,5] sl = [6,7,8,9,10]
sl = [6,7,8,9,10]
# Append with a function # Append with a function
nl = append(fl,sl) nl = append(fl,sl)
print(str(fl)) print(str(fl))
print(sl) print(sl)
print(str(nl)) print(str(nl))
# Can also be used to append any variable type # Can also be used to append any variable type
x = 10.0 x = 10.0
y = 11.0 y = 11.0
z = append(x,y) z = append(x,y)
print("Value of z is : " + str(z)) print("Value of z is : " + str(z))
main()
``` ```
......
%% Cell type:markdown id:6838b2de tags: %% Cell type:markdown id:6838b2de tags:
# Hello # Hello
%% Cell type:code id:4200d243 tags: %% Cell type:code id:4200d243 tags:
``` python ``` python
""" """
Example program, to read in two complex from the keyboard, form product and ratio Example program, to read in two complex from the keyboard, form product and ratio
and print oout also form. and print out also form.
Extperiment with what type of input is accepted. Experiment with what type of input is accepted.
Note a complex number MUST NOT contain spaces. Note a complex number MUST NOT contain spaces.
""" """
import cmath # Import ccomplex maths import cmath # Import complex maths
def main(): # Read in two complex, these must be is a+bj format with NO spaces.
a = complex(input("Complex a : "))
# Read in two complex, these must be is a+bj format with NO spaces. b = complex(input("Complex b : "))
a = complex(input("Complex a : "))
b = complex(input("Complex b : ")) # Form product and ratio
c = a*b
# Form pooduct and ratio d = a/b
c = a*b
d = a/b # Print out in default format
# Print out in default format print("Product is : " + str(c) + " Ratio is : " + str(d))
print("Product is : " + str(c) + " Ratio is : " + str(d))
# Get complex in polar radius / phase format. Note it returns TWO values
# Get complex in polar radius / phase format. Note it returns TWO values r,phi = cmath.polar(c)
r,phi = cmath.polar(c) # Print out what we get
# Print out what we get print("Product in polars, r: " + str(r) + " phi: " + str(phi))
print("Product in polars, r: " + str(r) + " phi: " + str(phi))
main()
``` ```
......
%% Cell type:markdown id:6838b2de tags: %% Cell type:markdown id:6838b2de tags:
# Hello # Hello
%% Cell type:code id:4200d243 tags: %% Cell type:code id:4200d243 tags:
``` python ``` python
""" """
Exmaple to deminstrate the use of complex maths to calcalate the impedance Exmaple to demonstrate the use of complex maths to calculate the impedance
of a LRC circuit. of a LRC circuit.
See fully plotting program ImpedancePlot.py in Plotting section. See fully plotting program ImpedancePlot.py in Plotting section.
You will also see the theory for this in Physics of Fields in semesterv 2 You will also see the theory for this in Physics of Fields in semester 2
""" """
import cmath # Import complex import cmath # Import complex
def main(): # Get values as floats
resistor = float(input("Resistor : "))
# Get values as floats capacitor = float(input("Capacitor : "))
resistor = float(input("Resistor : ")) inductor = float(input("Inductor : "))
capacitor = float(input("Capicitor : ")) frequency = float(input("Frequency : "))
inductor = float(input("Inductor : ")) omega= 2*cmath.pi*frequency # angular frequency (note cmath.pi is a float)
frequency = float(input("Frequency : "))
omega= 2*cmath.pi*frequency # angular freqiency (note cmath.pi is a float) # Form complex impedance using complex numbers
z = resistor + complex(0,-1.0/(capacitor*omega)) + complex(0,inductor*omega)
# Form complex impedance using complex numbers print("Complex impedance is : " + str(z))
z = resistor + complex(0,-1.0/(capacitor*omega)) + complex(0,inductor*omega)
print("Complex impedance is : " + str(z)) # Calculate modulus and phase of complex number
modulus = abs(z)
# Calculate modulus and phase of complex number phase = cmath.phase(z)
modulus = abs(z) print("Modulus is : " + str(modulus) + " phase is : " + str(phase))
phase = cmath.phase(z)
print("Modulus is : " + str(modulus) + " phase is : " + str(phase))
# Run the program
main()
``` ```
......
%% Cell type:markdown id:1d2b1bb3 tags: %% Cell type:markdown id:1d2b1bb3 tags:
# Complex Quadrant # Complex Quadrant
%% Cell type:code id:19d70e52 tags: %% Cell type:code id:19d70e52 tags:
``` python ``` python
""" """
Program to read in a complex number and print out which Program to read in a complex number and print out which
quadrant it is in using a if: elif chain inside a function quadrant it is in using a if: elif chain inside a function
""" """
import cmath # Add complex maths libary import cmath # Add complex maths library
def quadrant(z): def quadrant(z):
""" """
Function to determine which quadrant a complex number is in. Function to determine which quadrant a complex number is in.
""" """
if z.real >= 0 and z.imag >= 0 : if z.real >= 0 and z.imag >= 0 :
return 1 return 1
elif z.real < 0 and z.imag >= 0 : elif z.real < 0 and z.imag >= 0 :
return 2 return 2
elif z.real < 0 and z.imag < 0 : elif z.real < 0 and z.imag < 0 :
return 3 return 3
else: else:
return 4 return 4
# Test the program
z = complex(input("Give a complex number : "))
def main(): print("Typed number is : " + str(z))
z = complex(input("Give a complex number : ")) # We are able to just use the fuction inside the str()
print("Typed number is : " + str(z)) print("It is in quadrant : " + str(quadrant(z)))
# We are able to just use the fuction inside the str()
print("It is in quadrant : " + str(quadrant(z)))
main()
``` ```
......
%% Cell type:markdown id:46844364 tags: %% Cell type:markdown id:46844364 tags:
# Cos Plot # Cos Plot
%% Cell type:code id:a64ddd0e tags: %% Cell type:code id:a64ddd0e tags:
``` python ``` python
""" """
Form a cos(theta) plot in range 0 -> 4pi Form a cos(theta) plot in range 0 -> 4pi
""" """
import math import math
import matplotlib.pyplot as plt # Import the plot package as plt import matplotlib.pyplot as plt # Import the plot package as plt
def main(): plot_range = 4.0 * math.pi # Range of plot
plot_range = 4.0*math.pi # Range of plot plot_points = 100 # Number of points
plot_points = 100 # Number of point increment = plot_range / plot_points # Increment between points
increment = plot_range/plot_points # Increment between points
theta_data = [] # lists to hold the data
theta_data = [] # lists to hold the data cos_data = []
cos_data = []
for i in range(0,plot_points): # populate the lists with theta, cos(theta)
for i in range(0,plot_points): # polulate the lists with theta, cos(theta) theta = increment * i # value of theta
theta = increment*i # value of theta cos = math.cos(theta)
cos = math.cos(theta) theta_data.append(theta)
theta_data.append(theta) cos_data.append(cos)
cos_data.append(cos)
plt.plot(theta_data,cos_data,"g") # Default plot in green
plt.plot(theta_data,cos_data,"g") # Default plot in green plt.title("Plot of cosine") # Add title/lables
plt.title("Plot of cosine") # Add title/lables plt.xlabel("Angle theta")
plt.xlabel("Angle theta") plt.ylabel("Cos(theta)")
plt.ylabel("Cos(theta)") plt.show() # show the actual plot
plt.show() # show the actual plot
main()
``` ```
......
%% Cell type:markdown id:6838b2de tags: %% Cell type:markdown id:6838b2de tags:
# Hello # Hello
%% Cell type:code id:4200d243 tags: %% Cell type:code id:4200d243 tags:
``` python ``` python
""" """
Python programm to calculate the third side of a triangle by the Cosine Rule Python programm to calculate the third side of a triangle by the Cosine Rule
and the three angle of the triangle by the Sin Rule. and the three angles of the triangle by the Sin Rule.
""" """
import math import math
def main(): # Get the lengths of two sides and angle between them
a = float(input("First side of triangle : "))
b = float(input("Second side of triangle : "))
theta_c = float(input("Angle between them : "))
# Convert theta_c to radians
theta_c = math.radians(theta_c)
# Calculate length of side c by the cosine rule
# Note the order of operators and minimal use of brackets.
c = math.sqrt(a**2 + b**2 - 2*a*b*math.cos(theta_c))
print("Length of third side by cosine rule is : " + str(c))
# Use sin rule to calcuate angle.
# Note all angles converted back to degree to make them humanly readable.
ratio = c/math.sin(theta_c)
theta_a = math.degrees(math.asin(a/ratio))
theta_b = math.degrees(math.asin(b/ratio))
theta_c = math.degrees(theta_c)
# Get the lengths of two sides and angle between them print("Angles are A: " + str(theta_a) + " B:" + str(theta_b) + " C: " + str(theta_c))
a = float(input("First side of triangle : "))
b = float(input("Second side of triangle : "))
theta_c = float(input("Angle between them : "))
# Convert theta_c to radians
theta_c = math.radians(theta_c)
# Calculate length of side c by the cosine rule
# Note the order of operators and minimal use of brackets.
c = math.sqrt(a**2 + b**2 - 2*a*b*math.cos(theta_c))
print("Length of third side by cosine rule is : " + str(c))
# Use sin rule to calcuate angle.
# Note all angle converted back to degree to make them humanly readable.
ratio = c/math.sin(theta_c)
theta_a = math.degrees(math.asin(a/ratio))
theta_b = math.degrees(math.asin(b/ratio))
theta_c = math.degrees(theta_c)
print("Angles are A: " + str(theta_a) + " B:" + str(theta_b) + " C: " + str(theta_c))
# Run the program
main()
``` ```
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment