From 1b1b5f64b330ce24dba5f6b647961659b205111c Mon Sep 17 00:00:00 2001
From: cprutean <cip.pruteanu@ed.ac.uk>
Date: Fri, 8 Sep 2023 12:53:25 +0100
Subject: [PATCH] Update 5 files

- /CodeExamples/QuadraticFunction.ipynb
- /CodeExamples/PointsInCircle.ipynb
- /CodeExamples/ListofFunctions.ipynb
- /CodeExamples/FunctionFromCommand.ipynb
- /CodeExamples/CodeExamples.ipynb
---
 CodeExamples/CodeExamples.ipynb        | 102 ++++++++++++++++++++-----
 CodeExamples/FunctionFromCommand.ipynb |  80 +++++++++++++++++++
 CodeExamples/ListofFunctions.ipynb     |  79 +++++++++++++++++++
 CodeExamples/PointsInCircle.ipynb      |  98 ++++++++++++++++++++++++
 CodeExamples/QuadraticFunction.ipynb   |  32 +++++---
 5 files changed, 364 insertions(+), 27 deletions(-)
 create mode 100644 CodeExamples/FunctionFromCommand.ipynb
 create mode 100644 CodeExamples/ListofFunctions.ipynb
 create mode 100644 CodeExamples/PointsInCircle.ipynb

diff --git a/CodeExamples/CodeExamples.ipynb b/CodeExamples/CodeExamples.ipynb
index 9627edf..299162d 100644
--- a/CodeExamples/CodeExamples.ipynb
+++ b/CodeExamples/CodeExamples.ipynb
@@ -28,39 +28,103 @@
     "\n",
     "Set of examples for reading and writing to simple text file.\n",
     "\n",
-    "> [ReadTextFile](ReadTextFile.ipynb): Read a text file and print out again (discussed \"\\n\" problem with print()).\n",
-    "\n",
-    "> [WriteFile](WriteFile.ipynb): Writes a bit of text (and culture) to a file. (this write / overwrites \"burns.txt\").\n",
-    "\n",
-    "> [RangeLooptoFile](RangeLooptoFile.ipynb): Write data to a file from a loop.\n",
-    "\n",
-    "> [LineCount](LineCount.ipynb): Open a file and countv the number of lines  (with error trap for wrong filename).\n",
-    "\n",
-    "> [ImageShow](ImageShow.ipynb): Read and display an image file using Matplotlib.\n",
+    "> [ReadTextFile](ReadTextFile.ipynb): Read a text file and print out again (discussed \"\\n\" problem with print()).  \n",
+    "> [WriteFile](WriteFile.ipynb): Writes a bit of text (and culture) to a file. (this write / overwrites \"burns.txt\").  \n",
+    "> [RangeLooptoFile](RangeLooptoFile.ipynb): Write data to a file from a loop.  \n",
+    "> [LineCount](LineCount.ipynb): Open a file and countv the number of lines  (with error trap for wrong filename).  \n",
+    "> [ImageShow](ImageShow.ipynb): Read and display an image file using Matplotlib.  \n",
     "\n",
     "Data supplied\n",
     "\n",
     "> [burns.txt](burns.txt): A bit of culture !!!  \n",
     "> [cosdata.txt](cosdata.txt): Output from RangeLooptoFile.py  \n",
-    "> [sample.txt](sample.txt): Data for [Checkpoint 4](../Checkpoints/Checkpoint4.ipynb). \n",
+    "> [sample.txt](sample.txt): Data for [Checkpoint 4](../Checkpoints/Checkpoint4.ipynb).  \n",
     "> [stones.jpg](stones.jpg):\tStones on Scottish beach.  \n",
     "\n",
     "- ## Format\n",
     "\n",
     "Example Python programs to show formatted output.\n",
     "\n",
-    "> [FloatFormat](FloatFormat.ipynb): Example of formatting floats with .format()\n",
-    "\n",
-    "> [IntegerFormat](IntegerFormat.ipynb): Example of specifying and printing int in various bases.\n",
+    "> [FloatFormat](FloatFormat.ipynb): Example of formatting floats with .format()  \n",
+    "> [IntegerFormat](IntegerFormat.ipynb): Example of specifying and printing int in various bases.  \n",
     "\n",
     "- ## Functions\n",
     "\n",
+    "Example Python programs to illustrate the simple use of functions.\n",
+    "\n",
+    "> [QuadraticFunction](QuadraticFunction.ipynb): Function for quadratic and how to use it.  \n",
+    "> [TemperatureConverter](TemperatureConverter.ipynb): Fahrenheit to / from Celsius functions (see in mini-lectures).  \n",
+    "> [PlanckPlot](PlanckPlot.ipynb): Plot the Planck radiation cuurve (details of plotting in ***Plotting*** example section).  \n",
+    "\n",
+    "- ## Further Functions\n",
+    "\n",
+    "This set contains some more advanced use of functions.  \n",
+    "\n",
+    "> [PointsInCircle](PointsInCircle.ipynb): Use of function to return two floats as a list, and also some more advanced Matplotlib to display scatter plots and annonate graphs.  \n",
+    "> [FunctionPlot](FunctionPlot.ipynb): Passing a function as the argument to another function, also returning lists from functions.  \n",
+    "> [ListofFunctions](ListofFunctions.ipynb): Show that functions can be held in a list and the list elements can be called as functions. Shows the flexibility of lists, but this is not good programming style.  \n",
+    "> [FunctionFromCommand](FunctionFromCommand.ipynb): Show functions names being entered from command line and then called.\n",
+    "\n",
+    "- ## Getting Started\n",
+    "\n",
+    "This theme contains a series of simple starter Python programs, these being:  \n",
+    "\n",
+    "> [Hello](Hello.ipynb): Minimal Hello World program with single print.  \n",
+    "> [ReadingandPrintingStrings](ReadingandPrintingStrings.ipynb): Read a string by input() and print it.  \n",
+    "> [ReadingandPrintingStringsMain](ReadingandPrintingStringsMain.ipynb): Read a string by input() and print it using a main() structure.  \n",
+    "> [ReadingandPrintingFloats](ReadingandPrintingFloats.ipynb): Read a single float and print it out as a string.  \n",
+    "> [ReadingandFormatFloatsMain](ReadingandFormatFloatsMain.ipynb): Read a single float and print it out as a string using a main() structure. \n",
+    "> [ReadingandFormatFloats](ReadingandFormatFloats.ipynb): Read float, form *10^6 and ^2, and print out.  \n",
+    "> [ReadingandFormatFloatsMain](ReadingandFormatFloatsMain.ipynb): Read float, form *10^6 and ^2, and print out using a main() structure.  \n",
+    "> [ReadingIntandFloat](ReadingIntandFloat.ipynb): Read in int and float, and perform some basic arithmetic and print.  \n",
+    "> [ReadingIntandFloatMain](ReadingIntandFloatMain.ipynb): Read in int and float, and perform some basic arithmetic and print using a main() structure.  \n",
+    "\n",
+    "- ## Lists\n",
+    "\n",
+    "Set of simple list example, there are more extensive examples on the use of lists in the section on [Loops](../CourseNotes/loops.ipynb).\n",
+    "\n",
+    "> [MakeandAppendList](MakeandAppendList.ipynb): Simple example to make a list of integers and append an extra element.  \n",
+    "> [AppendFunction](AppendFunction.ipynb): A general simple append function that works for any object that implements the \"+\" operator.  \n",
+    "> [ExtractSublist](ExtractSublist.ipynb): Example of extracting a sublist and adding lists together.  \n",
+    "> [SortFunction](SortFunction.ipynb): A conceptually more complex use of a function operating on a list, rather more complex operation (not really needed in this course).  \n",
+    "\n",
+    "- ## Loops\n",
+    "\n",
+    "This theme contains example programs with simple loops.\n",
+    "\n",
+    "> [RangeLoop.ipynb](RangeLoop.ipynb): Loop to print out theta, cos(theta).  \n",
+    "> [RangeLooptoList](RangeLooptoList.ipynb): For loop to list of theta, cos(theta) and print.  \n",
+    "> [WhileLooptoList](WhileLooptoList.ipynb): Alternative version of above using while loop.  \n",
+    "> [GuessGame](GuessGame.ipynb): Simple interactive guess game to guess an int with while loop.  \n",
+    "\n",
+    "- ## Maths\n",
+    "\n",
+    "Set of Python example programs illustrating basic mathematics and how to call functions from the math module.  \n",
+    "\n",
+    "> EvaluateQuadratic.py      evaluates a a*x**2 + b*c + c quadtartic\n",
+    "\n",
+    "> EvaluateSinCos.py\t  converts angle from degrees to radians and evaluates\n",
+    "\t\t\t  sin / cos functions.\n",
+    "\n",
+    "> Cosine Rule.py\t\t  Calculate side lengths and angles of a triangle\n",
+    "       \t\t\t  using cosine and sine rules.\n",
+    "\n",
+    "> BasicComplex.py\t\t  Basic use of complex, read complex, basic calcualtion\n",
+    "\t\t\t  and display\n",
+    "\n",
+    "> MultiplyByLogs.py\t  demonestrates using log / exp to perform\n",
+    "\t\t\t  multiplication and division.\n",
+    "\n",
+    "> MultiplyByLogsNegativeFixed.by  same as program above but with fix to\n",
+    "\t\t\t\tdeal with negative numbers\n",
+    "\t\t\t\t(a bit more compicated)\n",
+    "\n",
+    "> EvaluateInput.py\t\texample of using eval() to evalate a string to float() and complex().\n",
+    "\t\t\t\tThis replicates the old Phyton 2 behaviour.\n",
+    "\n",
+    "> ComplexImpedance.py\t  Application of complex to calculate the complex\n",
+    "\t\t\t  impedance of LRC circuit.\n",
     "\n",
-    "- Further Functions\n",
-    "- Getting Started\n",
-    "- Lists\n",
-    "- Loops\n",
-    "- Maths\n",
     "- MoreComplex\n",
     "- Objects\n",
     "- Plotting\n",
@@ -77,6 +141,7 @@
     "- [FlightPlot](FlightPlot.ipynb)\n",
     "- [FloatFormat](FloatFormat.ipynb)\n",
     "- [FunctionCall](FunctionCall.ipynb)\n",
+    "- [FunctionFromCommand](FunctionFromCommand.ipynb)\n",
     "- [FunctionPlot](FunctionPlot.ipynb)\n",
     "- [GaussianNoisePlot](GaussianNoisePlot.ipynb)\n",
     "- [GuessGame](GuessGame.ipynb)\n",
@@ -89,6 +154,7 @@
     "- [OneWayConditional](OneWayConditional.ipynb)\n",
     "- [PiExample](PiExample.ipynb)\n",
     "- [PlanckPlot](PlanckPlot.ipynb)\n",
+    "- [PointsInCircle](PointsInCircle.ipynb)\n",
     "- [PrintList](PrintList.ipynb)\n",
     "- [QuadraticFunction](QuadraticFunction.ipynb)\n",
     "- [QuadraticPlot](QuadraticPlot.ipynb)\n",
diff --git a/CodeExamples/FunctionFromCommand.ipynb b/CodeExamples/FunctionFromCommand.ipynb
new file mode 100644
index 0000000..c3d36e9
--- /dev/null
+++ b/CodeExamples/FunctionFromCommand.ipynb
@@ -0,0 +1,80 @@
+{
+ "cells": [
+  {
+   "cell_type": "markdown",
+   "id": "6838b2de",
+   "metadata": {},
+   "source": [
+    "# Hello"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": null,
+   "id": "4200d243",
+   "metadata": {},
+   "outputs": [],
+   "source": [
+    "\"\"\"\n",
+    "    Example program to show that you read in a function name from the\n",
+    "    command line and then ececute it\n",
+    "\n",
+    "    Try with fnOne, fnTwo, it it also works for math.cos, math.sin, math.log etc\n",
+    "\"\"\"\n",
+    "\n",
+    "import math\n",
+    "\n",
+    "\n",
+    "def fnOne(y):\n",
+    "    \"\"\"\n",
+    "        Create a function\n",
+    "    \"\"\"\n",
+    "    print(\"Funcion one called with y = \" + str(y))\n",
+    "    return y     # Just return the given vaue\n",
+    "\n",
+    "\n",
+    "def fnTwo(y):\n",
+    "    \"\"\"\n",
+    "        Create a second function\n",
+    "    \"\"\"\n",
+    "    print(\"Funcion two called with y = \" + str(y))\n",
+    "    return 2*y   # Just return double the value\n",
+    "\n",
+    "\n",
+    "def main():\n",
+    "\n",
+    "    while True:     # Keep in a loop\n",
+    "        fn = eval(input(\"function : \"))    # Read in functioon name NOTE: eval()\n",
+    "        y = float(input(\"y value : \"))     # Get a parameter value\n",
+    "\n",
+    "        z = fn(y)                          # Call function read in\n",
+    "        print(\"Value returned is : \" + str(z))\n",
+    "\n",
+    "\n",
+    "if __name__ == \"__main__\":\n",
+    "    main()"
+   ]
+  }
+ ],
+ "metadata": {
+  "kernelspec": {
+   "display_name": "Python 3 (ipykernel)",
+   "language": "python",
+   "name": "python3"
+  },
+  "language_info": {
+   "codemirror_mode": {
+    "name": "ipython",
+    "version": 3
+   },
+   "file_extension": ".py",
+   "mimetype": "text/x-python",
+   "name": "python",
+   "nbconvert_exporter": "python",
+   "pygments_lexer": "ipython3",
+   "version": "3.9.7"
+  }
+ },
+ "nbformat": 4,
+ "nbformat_minor": 5
+}
diff --git a/CodeExamples/ListofFunctions.ipynb b/CodeExamples/ListofFunctions.ipynb
new file mode 100644
index 0000000..fae21e9
--- /dev/null
+++ b/CodeExamples/ListofFunctions.ipynb
@@ -0,0 +1,79 @@
+{
+ "cells": [
+  {
+   "cell_type": "markdown",
+   "id": "6838b2de",
+   "metadata": {},
+   "source": [
+    "# Hello"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": null,
+   "id": "4200d243",
+   "metadata": {},
+   "outputs": [],
+   "source": [
+    "\"\"\"\n",
+    "    Example program to create a list of fucntions and then call them from\n",
+    "    a loop.\n",
+    "\n",
+    "    This program illustrated the flexibility of functions, also that you can have\n",
+    "    list of anything, in this case you have a list of functions !!\n",
+    "\n",
+    "    This is a rather dangerous language feature and while it shows the flexibility of lists and\n",
+    "    functions, it is not an advised progarmming method since the scope for obscure code is\n",
+    "    unlimited.\n",
+    "\n",
+    "\"\"\"\n",
+    "\n",
+    "\n",
+    "import math\n",
+    "\n",
+    "def myFn(y):\n",
+    "    #       Create own local function that returns a value\n",
+    "    return 1 - math.erf(y)\n",
+    "\n",
+    "def main():\n",
+    "\n",
+    "    #      Make a list of, both from math library and lovcal\n",
+    "    functions = [math.cos,math.sin,math.tan,myFn]\n",
+    "\n",
+    "\n",
+    "    x = float(input(\"X value : \"))    # Read in float from command line\n",
+    "\n",
+    "    for fn in functions:               # Access the list of functions\n",
+    "        y = fn(x)                      # Use each elemenst of the list as a function.\n",
+    "\n",
+    "        print(\"Value from \" + str(fn) + \" is : \" + str(y))\n",
+    "        #     Note str(fn) gives the correct information but in rather messeg way.\n",
+    "\n",
+    "\n",
+    "\n",
+    "main()\n"
+   ]
+  }
+ ],
+ "metadata": {
+  "kernelspec": {
+   "display_name": "Python 3 (ipykernel)",
+   "language": "python",
+   "name": "python3"
+  },
+  "language_info": {
+   "codemirror_mode": {
+    "name": "ipython",
+    "version": 3
+   },
+   "file_extension": ".py",
+   "mimetype": "text/x-python",
+   "name": "python",
+   "nbconvert_exporter": "python",
+   "pygments_lexer": "ipython3",
+   "version": "3.9.7"
+  }
+ },
+ "nbformat": 4,
+ "nbformat_minor": 5
+}
diff --git a/CodeExamples/PointsInCircle.ipynb b/CodeExamples/PointsInCircle.ipynb
new file mode 100644
index 0000000..3ac32de
--- /dev/null
+++ b/CodeExamples/PointsInCircle.ipynb
@@ -0,0 +1,98 @@
+{
+ "cells": [
+  {
+   "cell_type": "markdown",
+   "id": "6838b2de",
+   "metadata": {},
+   "source": [
+    "# Hello"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": null,
+   "id": "4200d243",
+   "metadata": {},
+   "outputs": [],
+   "source": [
+    "\n",
+    "\"\"\"\n",
+    "    Use a function to get x,y position of random points in a circle\n",
+    "    and plot out the restuls with a few more new MatPlotLib features\n",
+    "\"\"\"\n",
+    "\n",
+    "import random\n",
+    "import matplotlib.pyplot as plt       # Standard Matplotlib import\n",
+    "from matplotlib.patches import Circle # Add circle from the patches module\n",
+    "\n",
+    "def getpoint(radius = 1.0):\n",
+    "    \"\"\"\n",
+    "    Function to get a random point in a circle of specified radius and return\n",
+    "    [x,y] as a list of floats\n",
+    "\n",
+    "    :param radius: the circle radius (Default = 1.0)\n",
+    "    :type radius: float\n",
+    "    :return: [x,y]\n",
+    "    \"\"\"\n",
+    "    while True:       # Go round loop forming x,y point\n",
+    "        x = random.uniform(-radius,radius)\n",
+    "        y = random.uniform(-radius,radius)\n",
+    "        if x*x + y*y <= radius*radius:  # In in circle, sucess break out of loop\n",
+    "            break\n",
+    "\n",
+    "    return x,y         # Return the x,y point as a list\n",
+    "\n",
+    "\n",
+    "def main():\n",
+    "    #        Get number of points and radius of circlde\n",
+    "    npoint = int(input(\"Number of points : \"))\n",
+    "    radius = float(input(\"Radius : \"))\n",
+    "\n",
+    "    xData = []        # Lists to hold x,y coordinates\n",
+    "    yData = []\n",
+    "\n",
+    "    #      Go round while loop getting point\n",
+    "    while npoint > 0:\n",
+    "        xpt,ypt = getpoint(radius)    # use function to get point\n",
+    "        xData.append(xpt)             # Add points to lists\n",
+    "        yData.append(ypt)\n",
+    "        npoint -= 1\n",
+    "\n",
+    "\n",
+    "    #      Some more complex Matplot lib plotting (more flexible)\n",
+    "    fig,ax = plt.subplots()           # Get figure and axis of default plot\n",
+    "    ax.scatter(xData,yData)           # Plot points in axis as scatter plot\n",
+    "    #      Add a unfilled circle at (0,0) of colour \"r\"\n",
+    "    ax.add_artist(Circle((0,0),radius,color = \"r\",fill = False))\n",
+    "    ax.axhline(y=0, color='r')        # Add horizonal line though y = 0\n",
+    "    ax.axvline(x=0, color='r')        # Add vertical line though x = 0\n",
+    "    ax.axis(\"equal\")                  # Set scale of axis to be equal in x,y\n",
+    "    plt.title(\"Scatter Plot\")         # Add a ittle to the main plot\n",
+    "    plt.show()                        # Show the plot\n",
+    "\n",
+    "main()"
+   ]
+  }
+ ],
+ "metadata": {
+  "kernelspec": {
+   "display_name": "Python 3 (ipykernel)",
+   "language": "python",
+   "name": "python3"
+  },
+  "language_info": {
+   "codemirror_mode": {
+    "name": "ipython",
+    "version": 3
+   },
+   "file_extension": ".py",
+   "mimetype": "text/x-python",
+   "name": "python",
+   "nbconvert_exporter": "python",
+   "pygments_lexer": "ipython3",
+   "version": "3.9.7"
+  }
+ },
+ "nbformat": 4,
+ "nbformat_minor": 5
+}
diff --git a/CodeExamples/QuadraticFunction.ipynb b/CodeExamples/QuadraticFunction.ipynb
index 837c3cc..efe4d29 100644
--- a/CodeExamples/QuadraticFunction.ipynb
+++ b/CodeExamples/QuadraticFunction.ipynb
@@ -16,24 +16,38 @@
    "outputs": [],
    "source": [
     "\"\"\"\n",
-    "   Example of a function to calculate the value of a quadratic\n",
+    "   Example of a function to calculate the value of a quadratic.\n",
+    "   \n",
+    "\n",
     "\"\"\"\n",
     "\n",
     "def quadratic(a , b, c, x):\n",
     "    \"\"\"\n",
-    "    Calcuate a quadratic of for a*x**2 + b*x + c and return a float value\n",
+    "    Function to calculate a quadratic of for a*x**2 + b*x + c and return a float value.\n",
+    "    Note contents of function are indented.\n",
     "    \"\"\"\n",
+    "    #     Do the calcualtion \n",
     "    y = a*x**2 + b*x + c\n",
-    "    return float(y)      # Return value and make sure it is a float\n",
+    "    #     Return the value of y at a float\n",
+    "    return float(y)\n",
     "\n",
     "\n",
     "def main():\n",
-    "    a = 4.0\n",
-    "    b = 6.0\n",
-    "    c = -10.0\n",
-    "    x = float(input(\"Give x value : \"))\n",
-    "    y = quadratic(a,b,c,x)\n",
-    "    print(\"Value of quadratic is : \" + str(y))\n",
+    "    \n",
+    "    #     Create the three coefficients as floats of specified values\n",
+    "    aval = 4.0\n",
+    "    bval = 6.0\n",
+    "    cval = -10.0\n",
+    "    \n",
+    "    #      get a x value from the keyboad\n",
+    "    xval = float(input(\"Give x value : \"))\n",
+    "    \n",
+    "    #     call the quadratic function with the specified values\n",
+    "    #     Note: the varable names are diffent; its the value that is passed.\n",
+    "    yval = quadratic(aval,bval,cval,xval)\n",
+    "    \n",
+    "    # print the returned value.\n",
+    "    print(\"Value of quadratic is : \" + str(yval))\n",
     "\n",
     "main()"
    ]
-- 
GitLab