Open Educational Resources

Python Lab Tutorials: Lab 2

  1. Consider the relationship

        \[y=\sin{\frac{\pi x}{6}}\]


    Using Sympy, find y' and y''. Evaluate y, y', and y'' at x_0=1. Then, using Matplotlib, plot the relationships y, y', and y'' for the domain x\in[-5,5] by first finding the y-values using a “for” loop and a list. Label your plot and plot legend appropriately.
  2. Consider the sequence whose terms are a_n=\frac{1}{3^n}. Use Sympy to evaluate the sum of the first twenty terms. Then, evaluate the infinite sum:

        \[\sum_{n=1}^\infty a_n\]


    Use Matplotlib to plot the first ten terms of the ordered pairs (i,\sum_{n=1}^ia_n). Label the plot appropriately.
  3. Consider the matrix

        \[M=\left(\begin{matrix}1 & x\\ x & 1\end{matrix}\right)\]


    Use Matplotlib and Sympy to plot the relationship between the determinant of M and x for x\in[-5,5]. Label the plot appropriately. From the plot, can you identify the values of x for which the determinant is equal to zero?
  4. The \cos(x) function where x is in radian can be represented as the infinite series:

        \[\cos(x)=1-\frac{x^2}{2!}+\frac{x^4}{4!}-\cdots=\sum_{n=0}^{\infty}\frac{(-1)^n}{(2n)!}x^{2n}\]


    Let i+1 be the number of terms used to approximate the function \cos(x). Using Numpy, create a function in Python whose inputs are x and i and whose output is the approximation:

        \[Approxcos(x,i)=\sum_{n=0}^{i}\frac{(-1)^n}{(2n)!}x^{2n}\]


    Show using numerical examples that your function gives proper approximations by comparing its output with the built-in \cos function in Numpy.

Extra Practice Problems

  1. The \sin(x) function where x is in radian can be represented as the infinite series:

        \[\sin(x)=x-\frac{x^3}{3!}+\frac{x^5}{5!}-\frac{x^7}{7!}+\cdots = \sum_{n=0}^\infty\frac{(-1)^n}{(2n+1)!}x^{2n+1}\]


    Let i+1 be the number of terms used to approximate the function \sin(x). Using Numpy, create a function in Python whose inputs are x and i and whose output is the approximation:

        \[Approxsin(x,i)=\sum_{n=0}^i\frac{(-1)^n}{(2n+1)!}x^{2n+1}\]


    Show using numerical examples that your function gives proper approximations by comparing its output with the built-in \sin function in Numpy.
  2. The discharge velocity through an orifice at the bottom of a water tank open to the atmosphere is given by the relationship:

        \[v=\sqrt{2gh}\]


    where h is the height of the water left in the tank. If A_{orifice} is the “effective” area of the orifice and A_{tank} is the cross sectional area of the tank, then the following differential equation describes the rate of change of the height of the water left in the tank

        \[\frac{\mathrm{d}h}{\mathrm{d}t}=-\frac{A_{orifice}}{A_{tank}}v\]


    Assuming that at t=0 the height of the water in the tank is given by h_0, use the Sympy’s dsolve function to solve the above differential equation using Python to find the height h as a function of A_{orifice}, A_{tank}, h_0, g, and t. Then, use the “Solve” function to show that the time t_e to empty the tank (i.e., the time at which h=0) is given by the expression:

        \[t_e=\sqrt{\frac{2h_0}{g}}\frac{A_{tank}}{A_{orifice}}\]

  3. In Sympy or Numpy, write two functions in Python whose arguments are a matrix and a vector. The first function should output the matrix after replacing the last row with the given vector. The second function should output the matrix after replacing the last column with the given vector.

Video Tutorials

Numpy

Scipy

Matplotlib

Leave a Reply

Your email address will not be published. Required fields are marked *