lab7

New topic: list comprehension (positive_places_without_loop)

derivative(f, x)

This task has two parts.

Part 1

Write a function derivative(f, x) which computes a numerical approximation of the first derivative of the function f(x) using central differences. The value that the function returns is

\[\frac{f\left(x+\frac{\epsilon}{2}\right) - f\left(x-\frac{\epsilon}{2}\right)}{\epsilon}\]

with \(\epsilon=10^{-6}\).

Example:

In [ ]: def f(x):
   ...:     return x * x

In [ ]: derivative(f, 0)
Out[ ]: 0.0

In [ ]: derivative(f, 1)
Out[ ]: 2.0000000000575113

In [ ]: derivative(f, 2)
Out[ ]: 4.000000000115023

Note that you may not get exactly the same numbers as shown above.

Part 2

Modify the derivative function such that it takes an optional third parameters eps to represent the greek letter epsilon (\(\epsilon\)) in the equation above. The parameter eps should default to 1e-6).

Examples:

In [ ]: import math

In [ ]: derivative(math.exp, 0, eps=0.1)
Out[ ]: 1.000416718753101

In [ ]: derivative(math.exp, 0)
Out[ ]: 1.0000000000287557

In [ ]: derivative(math.exp, 0, eps=1e-6)
Out[ ]: 1.0000000000287557

positive_places_without_loop(f, xs)

Implement a function positive_places_without_loop(f, xs) that takes as arguments some function f and a list of numbers xs and returns a list of those-and-only-those elements x of xs for which f(x) is strictly greater than zero.

This task was given already given before (positive_places).

In this lab, we ask that you write a function with the same behaviour but without usage of a for or while loop in te implementation (this is to practice list comprehension or the use of filter).


f1(x)

Write a function f1(x) which accepts a number x as input and computes and returns

\[f_1(x) = \cos(2\pi x)\exp(-x^2)\]

.


f2(x)

Write a function f2(x) which accepts the number x as input and computes and returns

\[f_2(x) = \log(x + 2.1)\]

where \(\log()\) refers to the natural logarithm (the Python function name is math.log).


create_plot_data(f, xmin, xmax, n)

Implement a function create_plot_data(f, xmin, xmax, n) which returns a tuple (xs, ys) where xs and ys are two sequences, each containing n numbers:

\[\mathtt{xs} = [x_0, x_1, \ldots, x_{n-1}] \qquad \mathrm{with} \qquad x_i = x_\mathrm{min} + (x_\mathrm{max} - x_\mathrm{min})\frac{i}{n - 1}\]

and

\[\mathtt{ys}=[f(x_0), f(x_1), \ldots, f(x_{n-1})\]

The function is expected to work for any \(n \ge 2\).

Examples (here the tuple of returned sequences is a tuple of lists):

In [ ]: def f(x):
...:     return x * 10
...:

In [ ]: create_plot_data(f, -1, 1, 2)
Out[ ]: ([-1.0, 1.0], [-10.0, 10.0])

In [ ]: create_plot_data(f, 0, 2, 5)
Out[ ]: ([0.0, 0.5, 1.0, 1.5, 2.0], [0.0, 5.0, 10.0, 15.0, 20.0])


In [ ]: def f(x):
...:     return 0
...:

In [ ]: create_plot_data(f, 0, 1.5, 4)
Out[ ]: ([0.0, 0.5, 1.0, 1.5], [0, 0, 0, 0])

myplot()

  • Implement a function myplot() that computes $f_1(x)$ and plots $f_1(x)$ using 1001 points for $x$ ranging from -2 to +2. The function should return None. For plotting, use matplotlib. You can re-use the functions you have implemented before for this laboratory.

  • Then extend this function myplot to also plot $f_2(x)$ in the same graph.

    To plot two or more curves on the same figure, just use the plot() command twice.

  • Label the x-axis, and provide a legend showing the function name (i.e. f1 and f2) for the two curves.

  • Extend the function myplot() so that it saves a png file of the graph (with name plot.png) and a pdf file of the graph (with name plot.pdf) through matplotlib commands.

    (Note: Generally, it is better to use pdf files rather than png files as pdf files are based on vector graphics and produce higher print quality, and can be zoomed without appearing pixelated. On the other hand, png files are good choice, for example, to include in webpages.)

  • Use the matplotlib navigation bar (at the bottom of the figure) to zoom into the image. For \(x > 0\), what is the (approximate) value \(x\) of the functions where \(f_1(x) = f_2(x)\)?

    If you cannot zoom into the plot, refer to Remark 1 and 2 below.

Remark 1 for users of IPython console and Spyder:

If you are using the the IPython console and by default your plots appear inline, i.e. in the IPYthon console or in the Plots panel in Spyder, then you cannot zoom into the figure. If so, you should use the command %matplotlib qt5 in the IPython console. After you have done this, the next plot figure should appear in its own pop-up window, and allow you to zoom in and out in the figure.

To switch back to figures showing in the IPython console, use %matplotlib inline.

Remark 2 for Jupyter Notebook users:

If you are using the Jupyter Notebook and by default your plots appear inline, i.e. in the IPYthon console, then you cannot zoom into the figure. If so, you should use the command %matplotlib notebook to get interactive plots. You may need to restart the kernel before issuing this command.

To switch back to figures appearing in your notebook, use %matplotlib inline.


Please submit your file lab7.py for this assignment.

End of lab7.