
Laboratory 11: Various SciPy applications
Prerequisites: +scipy: quad, bisect, fsolve, fmin, +closures
Create a file training11.py and populate with the following functions:
A function f(x) which computes:
Examples
In [ ]: f(0)
Out[ ]: 1.1176470588235294
In [ ]: f(2)
Out[ ]: 0.07293440360502447
While you do not need to do the following, it is possible to
implement the function f so that it can compute the function
for array input arguments. If you do this, the examples above
should work as shown, and in addition you should be able to do:
In [ ]: import numpy
In [ ]: x = numpy.array([0, 2])
In [ ]: f(x)
Out[ ]: array([ 1.11764706, 0.0729344 ])
For the following tasks, it is not necessary to support numpy array
arguments x as shown here but you may find this useful to plot
f(x) more conveniently.
Write a function make_multiplier(factor) that returns a
function which takes an argument x and which should return
factor * x.
Examples
In [ ]: g = make_multiplier(10)
In [ ]: g(1)
Out[ ]: 10
In [ ]: g(2)
Out[ ]: 20
In [ ]: g = make_multiplier(0.5)
In [ ]: g(10)
Out[ ]: 5.0
In [ ]: g(100)
Out[ ]: 50.0
Submit your file training11.py with subject line training11 to
get your code tested and obtain some feedback.
Create a file lab11.py. You may want to save training11.py
under the new name lab11.py as you will need to use the function
f from training11.py in this file.
Populate lab11.py with the following additional functions and features:
A function integrate_f_from0(b) which computes (an approximation of)
where the function f(x) is as defined in the training part of this
laboratory. The function integrate_f_from0(b) should return a
floating point number.
A function find_max_f() which returns (an approximation of) the
x for which f(x) takes the maximum value. Your function
find_max_f should return a floating point number.
A function find_f_equals_1() which returns a float which is (an
approximation of) the x for which f(x)=1. We are only
interested in the solution where x is negative.
A function lin_int(xs, ys) which accepts sequences xs and ys of data as input,
and returns a callable object f(x) which returns y(x) using linear interpolation
between the points provided by the data xs and ys.
You can write your own interpolation function or use a library function for this.
Example
In [ ]: xs = [1, 2, 3]
In [ ]: ys = [10, 20, 15]
In [ ]: f = lin_int(xs, ys)
In [ ]: f(1)
Out[ ]: array(10.0)
In [ ]: f(2)
Out[ ]: array(20.0)
In [ ]: f(1.5)
Out[ ]: array(15.0)
In [ ]: f(2.5)
Out[ ]: array(17.5)
Write a function make_oscillator(frequency) that returns a
function which takes a floating point value t
to represent time, and returns sin(t * frequency).
Examples
In [ ]: osc1 = make_oscillator(1)
In [ ]: osc1(0 * math.pi)
Out[ ]: 0.0
In [ ]: osc1(0.25 * math.pi)
Out[ ]: 0.7071067811865475
In [ ]: osc1(0.5 * math.pi)
Out[ ]: 1.0
In [ ]: osc2 = make_oscillator(2)
In [ ]: osc2(0 * math.pi)
Out[ ]: 0.0
In [ ]: osc2(0.25 * math.pi)
Out[ ]: 1.0
This submission is assessed: the first submission (with subject line lab11 and file lab11.py attached) will be used for this assessment.
