Description
π(π,π₯) = π0 + π1π₯ + β― + πππ₯π.
1. Write a function poly_eval(a, x) to evaluate π(π,π₯) in the naive way by just summing the terms as written. This should accept the coefficients π0,β¦,ππ as a vector.
2. The Horner method is an alternative evaluation algorithm in which no powers of π₯ are (explicitly) calculated.
For example, for a quadratic π(π₯) = π2π₯2+π1π₯+π0 we have π(π₯) = π0 + π₯(π1 + π2π₯)
Generalise this to polynomials of degree 3 and then degree π
3. Implement this as a function horner(a, x).
4. Test horner to make sure it gives the same result as poly_eval.
5. Benchmark the two methods to evaluate the polynomial π(π,π₯) = 1 + 2π₯ + 3π₯2 + β― + 10π₯10. Which algorithm is more efficient and by how much?
6. Count (approximately) the number of operations required by each of the two algorithms. Does this agree with your benchmarks? (Feel free to perform more benchmarks to check this, e.g. using polynomials with random coefficients.)
β
Exercise 2: Calculatingβ π¦ The Babylonian algorithm for calculating π₯ = π¦ is the following iteration:
1 π¦
π₯π+1 = 2 (π₯π + π₯ π).
1. Suppose that π₯π converges to some limiting valueβ π₯β as π β β. Show that then we must have π₯β = π¦.
2. To show that it does, in fact, converge, suppose (without loss of generality)β that π₯0 < π¦.
Show that we then have π₯π < π₯π+1 < π¦π+1 < π¦π for all π. Hence the π₯π form an increasing sequence that is bounded above, and hence they converge. (This is a theorem that we will just assume for this course.) 3. Write a function babylon that implements this algorithm. It takes a tolerance π and iterates until the residual is < π. It should return the sequence
(π₯π).
4. Test your function to make sure it gives the correct result.
5. Use rational initial values to convince yourself that calculating with rationals is a bad idea and that floating point is a good compromise.
β
6. Let πΏπ βΆ= π₯π β π₯β be the distance of π₯π from the limiting value π¦.
7. Plot the (absolute value of) πΏπ as a function of π on a suitable combination of linear and log scales. How fast does it seem to be converging?
8. Compare this on the same plot to the bisection algorithm from class. Which is better?
9. With πΏπ defined as in [6.], show that πΏπ+1 β πΏπ2 (β means βis approximately equal toβ).
Exercise 3: Collision of two discs [In this question we will finally find an actual use case for solving a quadratic equation!]
Suppose we have two discs located at positions x1 and x2, with velocities v1 and v2, respectively. The discs each have radius π.
3. Write a function collision that takes all the data and calculates the collision time by using the quadratic formula. Make sure you take account of the answer to [2.]
4. Check that your code works by setting up some combinations of discs where you can do the calculation by hand (e.g. both discs moving at a 45-degree angle).
Exercise 4: Evaluating elementary functions
1. Implement the degree-π Taylor polynomial approximation expπ(π₯) for exp(π₯) around π₯ = 0. Make sure that you do not explicitly calculate factorials in your code.
2. Make an interactive visualization using the Interact.jl package, showing expπ and exp as π varies.
3. Make another visualization showing the truncation error, i.e. exp(π₯) β expπ(π₯). How does it behave?
4. Calculate a bound for the truncation error using the Lagrange remainder. Use Stirlingβs approximation to estimate how many terms you need to take for the error to be of a certain size π.
5. Implement range reduction for the exponential function: instead of blindly applying a Taylor expansion (which will require many terms for large π₯), calculate exp(π₯) by reducing to the calculation of exp(π) for π β [β0.5,0.5] using the relation
exp(2π₯) = exp(π₯)2.
6. Make an interactive visualization of the Taylor polynomial approximation to log(1+π₯), showing visually that it fails outside a certain interval. Which interval is that, and why?
Exercise 5: Exactly representing irrationals using symbolic computing
Here we will represent certain real numbers in the computer exactly. Effectively we will use a type ofβ symbolic computation, in which we explicitly keep the symbol 2, rather than approximating it by a floating-point number.
(Although this is not really the subject of the course, itβs useful to remember that there is a whole world of symbolic computation that can be applied to certain problems, and that with a bit of work you often do not need an expensive commercial tool to do this!)
β
Consider the subset of the real numbers given byβ π βΆ= {π+π 2 βΆ π,π β β},
i.e. the set of numbers of the form π + π 2 where π and π are rational.
β
1. Write down formulae for the sum, difference and product ofβ π1 + π1 2
and π2 + π2 2, showing that the results are in π.
β β
2. Show that 1/(π+ π) is also in π by supposing that it equals π +π 2 and finding explicit equations for π and π. What type of equations are they? Solve them to find explicit values for π and π in terms of π and π. [This shows that we can also do division (by non-zero elements) and remain within the set, i.e. that π is a field, namely an extension field of β.
]
3. Define a type FieldExtension to represent these number pairs, and the corresponding operations. Also define a show method to print them nicely using a β symbol (typed as sqrt<TAB>).
β β
4. Find formulae to represent 1/(1 + 2 + 3) as elements of the corresponding set.
Some Julia tips
Package installation
using Pkg
Pkg.add(“BenchmarkTools”)
Load the package in each Julia session with
using BenchmarkTools
Benchmarking
“`julia
@time f(x)
“`
However, if the time taken is too short then this is not accurate. Instead, use the BenchmarkTools.jl package, with the syntax
“`julia using BenchmarkTools
@btime f(1, $x)
“`
Note that any variables you pass in must be given $ signs like this.
Plotting
The Plots.jl package is used as follows after loading it with using Plots:
β’ plot(x, y): plots the data with given π₯ coordinates and π¦ coordinates, joining those points with lines. These should be vectors.
β’ plot(y): specifying only one argument plots the data against the numbers
1,2,β¦
β’ plot!: adding ! adds a new plot to an existing one.
β’ scatter: plots points instead of lines
β’ Add yscale=:log10 inside the plotting command to use a logarithmic scale on the π¦ axis.
Note that there are small delays when first loading the package and for the first plot. Later plots will be quick.
Interactivity
The Interact.jl package enables us to generate simple interactive visualizations using a slider (and some other βwidgetsβ).
To generate a single slider, wrap a for loop in the @manipulate macro, e.g.
@manipulate for i in 1:10
i^2 end
To manipulate a plot, put a plot as the result at the end of the for loop:
@manipulate for i in 1:10
plot(-5:0.01:5, x -> sin(i * x))
end
You can generate multiple sliders by using a joint for loop:
@manipulate for i in 1:10, j in 0.1:0.1:0.9
i + j
end
Reviews
There are no reviews yet.