diff --git a/CodeExamples/AppendFunction.ipynb b/CodeExamples/AppendFunction.ipynb index 4451b038a11ee9a74282f420743409f2d77aaf7f..3968bfb026cc849e4dc1b508fae21de51f5bb454 100644 --- a/CodeExamples/AppendFunction.ipynb +++ b/CodeExamples/AppendFunction.ipynb @@ -26,23 +26,21 @@ " l = first + second\n", " return l\n", "\n", - "def main():\n", - " # Form two lists\n", - " fl = [1,2,3,5]\n", - " sl = [6,7,8,9,10]\n", - " # Append with a function\n", - " nl = append(fl,sl)\n", - " print(str(fl))\n", - " print(sl)\n", - " print(str(nl))\n", + "# Form two lists\n", + "fl = [1,2,3,5]\n", + "sl = [6,7,8,9,10]\n", "\n", - " # Can also be used to append any variable type\n", - " x = 10.0\n", - " y = 11.0\n", - " z = append(x,y)\n", - " print(\"Value of z is : \" + str(z))\n", + "# Append with a function\n", + "nl = append(fl,sl)\n", + "print(str(fl))\n", + "print(sl)\n", + "print(str(nl))\n", "\n", - "main()" + "# Can also be used to append any variable type\n", + "x = 10.0\n", + "y = 11.0\n", + "z = append(x,y)\n", + "print(\"Value of z is : \" + str(z))\n" ] } ], diff --git a/CodeExamples/BasicComplex.ipynb b/CodeExamples/BasicComplex.ipynb index 3e6459b89d52c94634bee1fcad31ced3a045ab3a..c607bc950a35683c257dfce07a58e5fb70bf17ef 100644 --- a/CodeExamples/BasicComplex.ipynb +++ b/CodeExamples/BasicComplex.ipynb @@ -17,33 +17,30 @@ "source": [ "\"\"\"\n", "Example program, to read in two complex from the keyboard, form product and ratio\n", - "and print oout also form.\n", + "and print out also form.\n", "\n", - "Extperiment with what type of input is accepted.\n", + "Experiment with what type of input is accepted.\n", "Note a complex number MUST NOT contain spaces.\n", "\n", "\"\"\"\n", "\n", - "import cmath # Import ccomplex maths\n", + "import cmath # Import complex maths\n", "\n", - "def main():\n", + "# Read in two complex, these must be is a+bj format with NO spaces.\n", + "a = complex(input(\"Complex a : \"))\n", + "b = complex(input(\"Complex b : \"))\n", "\n", - " # Read in two complex, these must be is a+bj format with NO spaces.\n", - " a = complex(input(\"Complex a : \"))\n", - " b = complex(input(\"Complex b : \"))\n", + "# Form product and ratio\n", + "c = a*b\n", + "d = a/b\n", + " \n", + "# Print out in default format\n", + "print(\"Product is : \" + str(c) + \" Ratio is : \" + str(d))\n", "\n", - " # Form pooduct and ratio\n", - " c = a*b\n", - " d = a/b\n", - " # Print out in default format\n", - " print(\"Product is : \" + str(c) + \" Ratio is : \" + str(d))\n", - "\n", - " # Get complex in polar radius / phase format. Note it returns TWO values\n", - " r,phi = cmath.polar(c)\n", - " # Print out what we get\n", - " print(\"Product in polars, r: \" + str(r) + \" phi: \" + str(phi))\n", - "\n", - "main()" + "# Get complex in polar radius / phase format. Note it returns TWO values\n", + "r,phi = cmath.polar(c)\n", + "# Print out what we get\n", + "print(\"Product in polars, r: \" + str(r) + \" phi: \" + str(phi))" ] } ], diff --git a/CodeExamples/ComplexImpedance.ipynb b/CodeExamples/ComplexImpedance.ipynb index bf05f0e24d6a367ee7335a283e0b5784a4d6de7b..0470769e120d40b3f0bcce9e26c58976f2ee00ba 100644 --- a/CodeExamples/ComplexImpedance.ipynb +++ b/CodeExamples/ComplexImpedance.ipynb @@ -16,37 +16,31 @@ "outputs": [], "source": [ "\"\"\"\n", - " Exmaple to deminstrate the use of complex maths to calcalate the impedance\n", + " Exmaple to demonstrate the use of complex maths to calculate the impedance\n", " of a LRC circuit.\n", "\n", " See fully plotting program ImpedancePlot.py in Plotting section.\n", - " You will also see the theory for this in Physics of Fields in semesterv 2\n", + " You will also see the theory for this in Physics of Fields in semester 2\n", "\n", "\"\"\"\n", "\n", "import cmath # Import complex\n", "\n", - "def main():\n", + "# Get values as floats\n", + "resistor = float(input(\"Resistor : \"))\n", + "capacitor = float(input(\"Capacitor : \"))\n", + "inductor = float(input(\"Inductor : \"))\n", + "frequency = float(input(\"Frequency : \"))\n", + "omega= 2*cmath.pi*frequency # angular frequency (note cmath.pi is a float)\n", "\n", - " # Get values as floats\n", - " resistor = float(input(\"Resistor : \"))\n", - " capacitor = float(input(\"Capicitor : \"))\n", - " inductor = float(input(\"Inductor : \"))\n", - " frequency = float(input(\"Frequency : \"))\n", - " omega= 2*cmath.pi*frequency # angular freqiency (note cmath.pi is a float)\n", + "# Form complex impedance using complex numbers\n", + "z = resistor + complex(0,-1.0/(capacitor*omega)) + complex(0,inductor*omega)\n", + "print(\"Complex impedance is : \" + str(z))\n", "\n", - " # Form complex impedance using complex numbers\n", - " z = resistor + complex(0,-1.0/(capacitor*omega)) + complex(0,inductor*omega)\n", - " print(\"Complex impedance is : \" + str(z))\n", - "\n", - " # Calculate modulus and phase of complex number\n", - " modulus = abs(z)\n", - " phase = cmath.phase(z)\n", - " print(\"Modulus is : \" + str(modulus) + \" phase is : \" + str(phase))\n", - "\n", - "\n", - " # Run the program\n", - "main()" + "# Calculate modulus and phase of complex number\n", + "modulus = abs(z)\n", + "phase = cmath.phase(z)\n", + "print(\"Modulus is : \" + str(modulus) + \" phase is : \" + str(phase))\n" ] } ], diff --git a/CodeExamples/ComplexQuadrant.ipynb b/CodeExamples/ComplexQuadrant.ipynb index 7b19ddabc638fd5c71d58bde7d136e046e1f9b41..26ec7f8c8fed50010f2410882ffc3a95a9012aba 100644 --- a/CodeExamples/ComplexQuadrant.ipynb +++ b/CodeExamples/ComplexQuadrant.ipynb @@ -20,7 +20,7 @@ " quadrant it is in using a if: elif chain inside a function\n", "\"\"\"\n", "\n", - "import cmath # Add complex maths libary\n", + "import cmath # Add complex maths library\n", "\n", "def quadrant(z):\n", " \"\"\"\n", @@ -38,15 +38,11 @@ " else:\n", " return 4\n", "\n", - " \n", - "\n", - "def main():\n", - " z = complex(input(\"Give a complex number : \"))\n", - " print(\"Typed number is : \" + str(z))\n", - " # We are able to just use the fuction inside the str()\n", - " print(\"It is in quadrant : \" + str(quadrant(z)))\n", - "\n", - "main()" + "# Test the program\n", + "z = complex(input(\"Give a complex number : \"))\n", + "print(\"Typed number is : \" + str(z))\n", + "# We are able to just use the fuction inside the str()\n", + "print(\"It is in quadrant : \" + str(quadrant(z)))\n" ] } ], diff --git a/CodeExamples/CosPlot.ipynb b/CodeExamples/CosPlot.ipynb index 072b5c696a61c7c81a6968fbdeffe2236c556ac2..3b588d1dadabdbe2c5f9fd59f13431bec610e72c 100644 --- a/CodeExamples/CosPlot.ipynb +++ b/CodeExamples/CosPlot.ipynb @@ -21,29 +21,25 @@ "import math\n", "import matplotlib.pyplot as plt # Import the plot package as plt\n", "\n", - "def main():\n", - " plot_range = 4.0*math.pi # Range of plot\n", - " plot_points = 100 # Number of point\n", - " increment = plot_range/plot_points # Increment between points\n", + "plot_range = 4.0 * math.pi # Range of plot\n", + "plot_points = 100 # Number of points\n", + "increment = plot_range / plot_points # Increment between points\n", "\n", - " theta_data = [] # lists to hold the data\n", - " cos_data = []\n", + "theta_data = [] # lists to hold the data\n", + "cos_data = []\n", "\n", - " for i in range(0,plot_points): # polulate the lists with theta, cos(theta)\n", - " theta = increment*i # value of theta\n", - " cos = math.cos(theta)\n", - " theta_data.append(theta)\n", - " cos_data.append(cos)\n", + "for i in range(0,plot_points): # populate the lists with theta, cos(theta)\n", + " theta = increment * i # value of theta\n", + " cos = math.cos(theta)\n", + " theta_data.append(theta)\n", + " cos_data.append(cos)\n", "\n", "\n", - " plt.plot(theta_data,cos_data,\"g\") # Default plot in green\n", - " plt.title(\"Plot of cosine\") # Add title/lables\n", - " plt.xlabel(\"Angle theta\")\n", - " plt.ylabel(\"Cos(theta)\")\n", - " plt.show() # show the actual plot\n", - "\n", - "\n", - "main()\n" + "plt.plot(theta_data,cos_data,\"g\") # Default plot in green\n", + "plt.title(\"Plot of cosine\") # Add title/lables\n", + "plt.xlabel(\"Angle theta\")\n", + "plt.ylabel(\"Cos(theta)\")\n", + "plt.show() # show the actual plot\n" ] } ], diff --git a/CodeExamples/CosineRule.ipynb b/CodeExamples/CosineRule.ipynb index a1809d34c6702218234df8730eac5cb2bcb6afdc..17c31b50c9979b27584c75bdc66632a4c7f28ad7 100644 --- a/CodeExamples/CosineRule.ipynb +++ b/CodeExamples/CosineRule.ipynb @@ -18,38 +18,33 @@ "\n", "\"\"\"\n", " Python programm to calculate the third side of a triangle by the Cosine Rule\n", - " and the three angle of the triangle by the Sin Rule.\n", + " and the three angles of the triangle by the Sin Rule.\n", "\"\"\"\n", "\n", "import math\n", "\n", - "def main():\n", + "# Get the lengths of two sides and angle between them\n", + "a = float(input(\"First side of triangle : \"))\n", + "b = float(input(\"Second side of triangle : \"))\n", + "theta_c = float(input(\"Angle between them : \"))\n", " \n", - " # Get the lengths of two sides and angle between them\n", - " a = float(input(\"First side of triangle : \"))\n", - " b = float(input(\"Second side of triangle : \"))\n", - " theta_c = float(input(\"Angle between them : \"))\n", + "# Convert theta_c to radians\n", + "theta_c = math.radians(theta_c)\n", " \n", - " # Convert theta_c to radians\n", - " theta_c = math.radians(theta_c)\n", - " \n", - " # Calculate length of side c by the cosine rule\n", - " # Note the order of operators and minimal use of brackets.\n", - " c = math.sqrt(a**2 + b**2 - 2*a*b*math.cos(theta_c))\n", + "# Calculate length of side c by the cosine rule\n", + "# Note the order of operators and minimal use of brackets.\n", + "c = math.sqrt(a**2 + b**2 - 2*a*b*math.cos(theta_c))\n", "\n", - " print(\"Length of third side by cosine rule is : \" + str(c))\n", + "print(\"Length of third side by cosine rule is : \" + str(c))\n", " \n", - " # Use sin rule to calcuate angle. \n", - " # Note all angle converted back to degree to make them humanly readable.\n", - " ratio = c/math.sin(theta_c)\n", - " theta_a = math.degrees(math.asin(a/ratio))\n", - " theta_b = math.degrees(math.asin(b/ratio))\n", - " theta_c = math.degrees(theta_c)\n", + "# Use sin rule to calcuate angle. \n", + "# Note all angles converted back to degree to make them humanly readable.\n", + "ratio = c/math.sin(theta_c)\n", + "theta_a = math.degrees(math.asin(a/ratio))\n", + "theta_b = math.degrees(math.asin(b/ratio))\n", + "theta_c = math.degrees(theta_c)\n", " \n", - " print(\"Angles are A: \" + str(theta_a) + \" B:\" + str(theta_b) + \" C: \" + str(theta_c))\n", - " \n", - "# Run the program\n", - "main()" + "print(\"Angles are A: \" + str(theta_a) + \" B:\" + str(theta_b) + \" C: \" + str(theta_c))" ] } ],