ππ = 3.1415926535897...
Lecture 1: Introduction to Math 5486
I encourage you to play around with the juptyer notebook for this lecture - you can copy the code with the this notebook button on the side of this page.
These notes are mainly a record of what we discussed and are not a substitute for attending the lectures and reading books! If anything is unclear/wrong, let me know and I will update the notes.
Follow the steps in /InstallingJulia.html and A1 to learn about notebooks and Julia.
Notebooks are made up from “cells”.
A adds a cell above the currently selected cell,B adds a cell below,M changes the cell from Julia to markdown,enter to see the syntax and edit cells,ctrl+enter to exit edit mode,Example 1.1 Add a new markdown cell below this one and write “Hello world!”
Hello, world!
Text in this cell can be bold, italic, bold italic, quotes
“The only source of knowledge is experience”
– Einstein
Preformatted text,
useful for
pseudocode
Markdown cheat sheet: link
\LaTeX allows you to display math: e.g. \pi \approx \frac{22}{7} and f(x) = f(a) + f'(a) (x - a) + \frac{1}{2} f''(a) (x-a)^2 + R(x).
Multiline equations are also possible:
\begin{align} \int_a^b f(x) \mathrm{d}x % &\approx \int_a^b p_n(x) \mathrm{d}x \nonumber\\ % &= \sum_{j=0}^n w_j f(x_j) \end{align}
LaTex cheat sheet: link
Draw symbol to get latex syntax: link
You can use greek symbols in Julia code: type \pi and tab to get the symbol.
You can show the result of a calculation by not ending a line with a semi-colon:
ππ = 3.1415926535897...
You can add comments with a #:
# Define p
p = 22/7;You can also use @show to print variables (which also prints the calculation):
@show abs(π-p)/π;abs(π - p) / π = 0.0004024994347707008
You can use println() to output variables (and you can also add text):
println( "The relative error when approximating π by 22/7 is ", abs(π-p)/π)The relative error when approximating π by 22/7 is 0.0004024994347707008
Arrays are indexed at 1:
a = [1 2 3 4 5];
a[1]1
b = [2, 4, 6, 8]4-element Vector{Int64}:
2
4
6
8
Vector of objects:
c = [ a , b , "hello world" ]3-element Vector{Any}:
[1 2 … 4 5]
[2, 4, 6, 8]
"hello world"
c[3]"hello world"
You can either specify the columns:
X = [ [1, 4] [2, 5] ]2×2 Matrix{Int64}:
1 2
4 5
Or the rows:
Y = [ 1 2 ; 4 5 ]2×2 Matrix{Int64}:
1 2
4 5
In order to use a certain package, you need to use the using keyword:
using LinearAlgebraRecall that
X 2×2 Matrix{Int64}:
1 2
4 5
det(X)-3.0
tr( X )6
(-1/3) * [ 5 -2 ; -4 1 ]2×2 Matrix{Float64}:
-1.66667 0.666667
1.33333 -0.333333
inv( X )2×2 Matrix{Float64}:
-1.66667 0.666667
1.33333 -0.333333
ϵ = eigvals( X )2-element Vector{Float64}:
-0.4641016151377544
6.464101615137754
v = eigvecs( X )2×2 Matrix{Float64}:
-0.806898 -0.343724
0.59069 -0.939071
v1 = v[1:2,1]; # gets the first column of v
norm( ( X - ϵ[1] * Diagonal([1, 1]) ) * v1 )2.220446049250313e-16
f1 = x -> cos( x );
f1( π )-1.0
f2(x) = cos(x)
f2(π)-1.0
In order to do entry-wise evaluation, you can use a dot (this is called vectorisation):
f1.( [-π, 0, π] )3-element Vector{Float64}:
-1.0
1.0
-1.0
You can also specify the vectorisation at the start with @.:
@. f1( [-π, 0, π] )3-element Vector{Float64}:
-1.0
1.0
-1.0
In order to plot functions we need the Plots package and, in order to write \LaTeX in the titles and labels, we need LaTeXStrings.
using Plots
using LaTeXStringsg(x) = x/exp(x)
plot( g, -5, 1, label="hello" )X = -5:.01:1;
Y = X .* exp.(X) ;
plot( X, Y, label = L"f(w) = w e^w",
xlabel = L"w",
ylabel = "not latex",
title = "L\"\" is a LaTeX string and \"\" a normal string") Element-wise operations can also be written using the following:
X = range(0, 10, length=100)
Y1 = @. sin(X) / X
Y2 = cos.(X)
plot( X, [Y1 Y2],
xlabel=L"x",
label=[L"\frac{\sin x}{x}" L"\cos x"],
linewidth=[4 2],
lc=[:green :blue]) In Figure 1.1, you can see two graphs!
If you are looking at the code, don’t worry about the @fig-1 or the ::: {.callout-note} syntax - this adds the html/css elements so that the webpage looks nicer.
# ! is like "hold on" in Matlab (if you are familar with that!)
plot( X, Y1, xlabel=L"x", label=L"\frac{\sin x}{x}", linewidth=4, lc=:green)
plot!( X, Y2, label=L"\cos x", linewidth=2, lc=:blue) f3(x) = 1 / (1 + x^2);
f4(x) = 1 / (1 + sin(x)^2);
plot(f3, -π, π,
lw=3,
label = L"f_1(x) = \frac{1}{1+x^2}");
plot!(f4, -π, π,
lw = 3,
label = L"f_2(x) = \frac{1}{1+\sin^2(x)}",
size = (600, 250), legend = :outertopright )Exercise 1.1 What is the relative error in approximating \pi by 3? (write down a formula, no need to simplify)
Exercise 1.2 Let x_n = \frac{\sin n}{n^2}. For what p do we have x_n = \mathcal O( n^p ) as n \to \infty?
Exercise 1.3 Write down the 3rd order Taylor polynomial of f(x) = x - \sin x about x = 0.
Exercise 1.4 Let y_n = \frac1n - \sin \frac1n. For what p do we have y_n = \mathcal O( n^p ) as n \to \infty?
Exercise 1.5 Let F(x) := \frac{e^x - 1}{x} and notice that F(x) \approx G(x) := 1 + \frac{x}{2} + \frac{x^2}{6}
F(x) = ( exp(x) - 1 )/x
G(x) = 1 + x/2 + x^2/6
ϵ = 1e-16
F(ϵ), G(ϵ)(0.0, 1.0)
Which result do you trust more and why?
Exercise 1.6 What does the relative condition number \kappa_f(x) of a function f measure?
Exercise 1.7 Consider the following plot
Is the decay algebraic x^{-p} or exponential e^{-a x}?
Exercise 1.8 Write down the Intermediate Value Theorem.
Exercise 1.9 Consider some function f and a polynomial interpolation p. How could one use p to approximate
\begin{align} \int_a^b f(x) \mathrm{d}x \approx \sum_{j=0}^n w_j f(x_j) ? \end{align}
What are x_j and w_j?
Exercise 1.10 Suppose u : [0,T] \to \mathbb R solves the initial value problem
\begin{align} u'(t) &= f\big( t, u(t) \big) \qquad \text{on } (0,T) \nonumber \\ u(0) &= u_0. \end{align}
Fix mesh points t_j = j \frac{T}{n} for j=0,\dots,n and show that
\begin{align} u(t_{j+1}) - u( t_j ) = \int_{t_j}^{t_{j+1}} f\big( s, u(s) \big) \mathrm{d}s \end{align}
Exercise 1.11 Write down what you get when you apply the rectangular rule to (4).
Exercise 1.12 A simple numerical method for solving (3) is to approximate the integral in (4) with the rectangular rule (this is known as Euler’s method)
\begin{align} u(t_{j+1}) \approx u(t_j) + \frac{T}{n} f\big( t_j, u(t_j) \big). \end{align}
We implement this here:
function Euler( u0, f, T, n )
h = T/n
t = 0:h:T
u = zeros(n+1)
u[1] = u0
for j = 1:n
u[j+1] = u[j] + h * f( t[j], u[j] )
end
return u
end Euler (generic function with 1 method)
We now use this method to numerically solve the initial value problem for u_0 = 0 and f(t, u) := -t \cos(u):
f(t,u) = -t * cos(u)
u0 = 0
T = 10
n = 100
u = Euler( 0, f, T, n)
plot( 0:T/n:T , u, label = "Approximate solution with n = $n" )We observe u(t) \to \alpha as t\to\infty. Can you compute the value of \alpha?