diff --git a/0.3 Some Exercises.ipynb b/0.3 Some Exercises.ipynb index bcd7808..3120da4 100644 --- a/0.3 Some Exercises.ipynb +++ b/0.3 Some Exercises.ipynb @@ -1,420 +1,420 @@ { "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Some exercises\n", "\n", "**Course**: [_Analyse Numérique pour SV_](https://moodle.epfl.ch/course/info.php?id=) (MATH-2xx)\n", "\n", "Original by prof. Fabio Nobile\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- Before starting to work on this exercise session, you should review the material in the short `Python tutorial` available on Moodle." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Exercise 0 -- Learning Python using Python." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Type the following commands and look at the results. Begin by importing numpy and matplotlib.pyplot as " ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import numpy as np\n", "import matplotlib.pyplot as plt" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "These are the libraries that python relies upon for scientific computing and plotting. " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "| No. | Code | Instruction |\n", "| --- | --- | --- |\n", "| 1 | `a=7` | Define $a$ as a scalar variable which has type `int` |\n", "| 2 | `a=7.1` | Define $a$ as a scalar variable which has type `float` |\n", "| 3 | `b=np.array([1,2,3])` | Define $b\\in\\mathbb{R}^{1 \\times 3}$. What happens if you replace `,` by `''` or `;`? |\n", "| 4 | `g=b[2]` | Return the **third** element of $b$. Note: Indexation starts at 0. |\n", "| 5 | `E=np.array([[1, 2, 3],[4, 5, 6]])` | Create matrix $E\\in\\mathbb{R}^{2\\times3}$. Note: Python is case sensitive. |\n", - "| 6 | `E[0,:]` | Return first column of $E;$ $(1,4)^T$ |\n", + "| 6 | `E[0,:]` | Return first row of $E;$ $(1,4)^T$ |\n", "| 7 | `h=E[1,2]` | Return the element $E_{2,3}$ of the matrix $E$ ($E_{2,3}=6$). |\n", "| 8 | `%whos` | Fetch information about the used variables. |\n", "| 9 | `help(np.sin)` | Fetch help for the command ` np.sin`. Same as `? np.sin` in iPython. |\n", "| 10 | `A=np.eye(3)` | Return the identity matrix $3\\times 3$ |\n", "| 11 | `I=np.ones((4,4))` | Return matrix $4\\times 4$ for which each value is 1 |\n", "| 12 | `F=np.zeros((2,3))` | Return $F\\in\\mathbb{R}^{2\\times 3}$ for which each value is 0 |\n", "| 13 | `F.T` | Perform transpose of $F$ |\n", "| 14 | `x=np.linspace(0,1,3)` | Return a vector of length $3$ whose values are equally distributed between $0$ and $1$ |\n", "| 15 | `D=np.diag(b)` | Return diagonal matrix with diagonal given by vector b. |\n", "| 16 | `np.shape(F)` | Return the number of lines and columns of $F$ in a line vector. |\n", "| 17 | `A@D` | Return product of two matrices |\n", "| 18 | `E@x` | Return matrix vector product |\n", "| 19 | `A[-1,1]` | Return element of $A$ which is on the last line, second column |\n", "| 20 | `H=np.array([[1, 3],[9, 11]]);np.linalg.solve(H,E)` | Compute $H^{-1}E$. Note: Never use `np.linalg.inv(H)@E` |\n", "| 21 | `np.linalg.det(H)` | Compute determinant of $H$ |" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "## type here and execute the 21 commands above\"\n", "#1\n", "a=7\n", "print('#1',a),\n", "#2 continue by yourself" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Exercise 1\n", "\n", "Fibonacci's series is defined as follows:\n", "$$\\begin{align*}\n", "F_0&=1\\\\\n", "F_1&=1\\\\\n", "F_{n}&=F_{n-1}+F_{n-2}, \\quad \\forall n \\geq 2\n", "\\end{align*}$$\n", "* Complete the `Python` function below which computes the $30^{\\text{th}}$ Fibonacci's number.\n", "\n", "* Write a function which takes as an argument a natural number $n$ and returns the $n^{\\text{th}}$ Fibonacci's number.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "jupyter": { "source_hidden": true } }, "outputs": [], "source": [ "#COMPLETE THIS CODE\n", "def Fibonacci(n):\n", " # n should be a positive integer\n", " # Complete this code\n", " Fn=None #Change this value\n", " # ...\n", " return Fn\n", "\n", "F30=Fibonacci(30)\n", "print(F30)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Exercise 2\n", "Let us consider the matrices \n", "\\begin{align*}\n", "A=\n", "\\begin{bmatrix}\n", "5 & 3 & 0\\\\\n", "1 & 1 & -4\\\\\n", "3 & 0 & 0\n", "\\end{bmatrix}, \\quad\n", "B=\n", "\\begin{bmatrix}\n", "4 & 3 & 2\\\\\n", "0 & 1 & 0\\\\\n", "5 & 0 & 1/2\n", "\\end{bmatrix}.\n", "\\end{align*}\n", "Compute (without using any loops), the matrix $C=AB$ (matrix product) and the matrix $D$ which\n", "has as elements $D_{ij}=A_{ij}B_{ij}$ (element-wise product)." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Solution:\n", "\n", "We can use the Python commands `@` for matrix multiplication and `*` for element-wise multiplication, as shown below:\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "A=np.array([ [5,3,0],\n", " [1,1,-4],\n", " [3,0,0]])\n", "B=np.array([ [4,3,2],\n", " [0,1,0],\n", " [5,0,1/2]])\n", "\n", "\n", "# Complete this code\n", "C=None #Change this value\n", "D=None #Change this value\n", "\n", "\n", "print('C matrix')\n", "print(C)\n", "\n", "print('D matrix')\n", "print(D)\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Exercise 3\n", "\n", "Define (without using any loops) the\n", "bidiagonal matrix of size $n=5$ whose main diagonal is a vector\n", "of equally distributed points between $3$ and $6$, i.e. $$D=(3, 3.75, 4.5, 5.25, 6)^T,$$\n", "\n", "and the sub-diagonal is the vector of equally distributed points between $2$ and $3.5$, i.e. $$S=(2, 2.5, 3, 3.5)^T.$$\n", "\n", "\n", "Tip: See ``https://numpy.org/doc/stable/reference/generated/numpy.diag.html``\n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Solution\n", "\n", "Following the reference for the`diag` method of numpy we obtain:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Complete this code\n", "D=None #Change this value\n", "S=None #Change this value\n", "M=None #Change this value\n", "\n", "print('The matrix M is')\n", "print(M)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Remember that you can also obtain help from a python function using the command \n", "`help(NAME OF FUNCTION)`. In this case: `help(np.diag)`" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "help(np.diag)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Exercise 4\n", "Let us consider the vectors $x=(1,4,7,2,1,2)$ and $y=(0,9,1,4,3,0)$. \n", "Compute (without using any loops for points a and b):\n", "1. the product, component by component, between two vectors $x$ and $y$ (tip: use the operator `*`)\n", "2. the scalar product between the same vectors $x$ and $y$ (tip: use the operator `@`)\n", "3. a vector whose elements are defined by:\n", "$v_1=x_1\\,y_n, \\quad v_2=x_2\\, y_{n-1},\\quad \\dots, \\quad v_{n-1}=x_{n-1}\\, y_2,\\quad v_n=x_n\\,y_1$.\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Solution" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "x=np.array([1,4,7,2,1,2])\n", "y=np.array([0,9,1,4,3,0])\n", "\n", "# Complete this code\n", "v1=None #Change this value\n", "v2=None #Change this value\n", "v3=None #Change this value\n", "\n", "print('v1 = '+str(v1))\n", "print('v2 = '+str(v2))\n", "print('v3 = '+str(v3))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Exercise 5\n", "\n", "Plot the functions\n", "\\begin{align*}\n", "f(x)=x^3 , \\quad x \\in [1,10]\n", "\\end{align*}\n", "\\begin{align*}\n", "g(x)=\\exp(4x) , \\quad x \\in [1,10]\n", "\\end{align*}\n", "\n", "in linear scale (i.e. using the function `plt.plot()` and logarithmic scale (i.e. using the function `plt.semilogy()` and `plt.loglog()`. We will use the definition interval to plot these graphs considering 200 equally distributed points.\n", "\n", "\n", "\n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Solution \n", "\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Complete this code\n", "f=None #Change this value to define inline function f\n", "g=None #Change this value to define inline function g \n", "\n", "x=np.linspace(1,10,200)\n", "\n", "\n", "#plot ins plot plot \n", "plt.title(' Normal scale')\n", "plt.plot(x,f(x),label=r'$f(x)$')\n", "# Complete this code\n", "# add a plot here for g\n", "plt.legend()\n", "plt.show()\n", "\n", "#plot in semilogy \n", "\n", "\n", "# Complete this code\n", "\n", "\n", "\n", "\n", "#plot in log-log\n", "\n", "# Complete this code\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Exercise 6\n", "\n", "1. Given \n", "\\begin{align*}\n", "f(x)=\\frac{x^2}{2}\\sin(x) , \\quad x \\in [1,20]\n", "\\end{align*}\n", "plot the function $f$ using 10, 20 and 100 equidistant points in the given interval.\n", "Plot the three graphs on the same figure with three\n", "different colors. Which one gives the best representation of $f$?\n", "\n", "2. Do the same for the functions:\n", "\\begin{align*}\n", "g(x) &= \\frac{x^3}{6}\\cos(\\sin(x))\\exp(-x)+\\left(\\frac{1}{1+x}\\right)^2 , \\quad x \\in [1,20] \\\\\n", "h(x) &= x(1-x)+\\frac{\\sin(x)\\cos(x)}{x^3}, \\quad x \\in [1,20].\n", "\\end{align*}\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### solution" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# complete the code here for question 1" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# complete the code here for question 2\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.7.7" } }, "nbformat": 4, "nbformat_minor": 4 }