Review for test 1 in MTH 229

[Get this a an ipynb file.]

Test 1 will cover the first 4 projects. This review workbook serves two purposes:

To begin, you should "run all" so that these packages are loaded into your Julia session.

using Plots
using Roots

Calculator skills:

The basic ideas of a calculator easily correspond to ideas of julia:

Functions

Basic functions in julia and in math look almost identical: $f(x) = \sin(x) + \cos(2x)$ becomes

f(x) = sin(x) + cos(2x)
f (generic function with 1 method)

Julia has some alternative forms though, for example when parameters are involved these can be added as additional arguments to the function with default values.

Functions in math that involve cases are often easily handled with the ternary operator: predicate ? iftrue : iffalse. An example was a model for cost when the first 100 cost 5 dollars per unit and afterwards 4 dollars per unit:

cost(x) = (x <= 100) ? 5*x : 5*100 + 4*(x - 100)
cost (generic function with 1 method)

Here are some examples:

f(x) = sin(0) + (sin(pi/2) - sin(0)) / (pi/2 - 0) * (x - 0)
f (generic function with 1 method)
f(x) = 1000 * exp(0.05 * x)
f(2)
1105.1709180756477

Plotting

Plotting is done in julia through external packages. We discussed the Plots package that makes it easy to plot functions by name: plot(f, a, b). We can also plot two or more functions over $[a,b]$ – just place the functions into a [] container:

f(x) = x^2 - 2x + 3
plot(f, -3, 4)
-14 -12 -10 -8 -6 -4 -2 0 2 4 6 8 10 12 14 -12.0 -11.5 -11.0 -10.5 -10.0 -9.5 -9.0 -8.5 -8.0 -7.5 -7.0 -6.5 -6.0 -5.5 -5.0 -4.5 -4.0 -3.5 -3.0 -2.5 -2.0 -1.5 -1.0 -0.5 0.0 0.5 1.0 1.5 2.0 2.5 3.0 3.5 4.0 4.5 5.0 5.5 6.0 6.5 7.0 7.5 8.0 8.5 9.0 9.5 10.0 10.5 11.0 11.5 12.0 -20 -10 0 10 20 -12.0 -11.5 -11.0 -10.5 -10.0 -9.5 -9.0 -8.5 -8.0 -7.5 -7.0 -6.5 -6.0 -5.5 -5.0 -4.5 -4.0 -3.5 -3.0 -2.5 -2.0 -1.5 -1.0 -0.5 0.0 0.5 1.0 1.5 2.0 2.5 3.0 3.5 4.0 4.5 5.0 5.5 6.0 6.5 7.0 7.5 8.0 8.5 9.0 9.5 10.0 10.5 11.0 11.5 12.0 y1 -25 -20 -15 -10 -5 0 5 10 15 20 25 30 35 40 45 -20 -19 -18 -17 -16 -15 -14 -13 -12 -11 -10 -9 -8 -7 -6 -5 -4 -3 -2 -1 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 -20 0 20 40 -20 -18 -16 -14 -12 -10 -8 -6 -4 -2 0 2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40
f(x) = cos(x)
g(x) = 1 + f(2*x)
plot([f,g], 0, 2pi)
-10 -8 -6 -4 -2 0 2 4 6 8 10 12 14 16 18 -8.0 -7.5 -7.0 -6.5 -6.0 -5.5 -5.0 -4.5 -4.0 -3.5 -3.0 -2.5 -2.0 -1.5 -1.0 -0.5 0.0 0.5 1.0 1.5 2.0 2.5 3.0 3.5 4.0 4.5 5.0 5.5 6.0 6.5 7.0 7.5 8.0 8.5 9.0 9.5 10.0 10.5 11.0 11.5 12.0 12.5 13.0 13.5 14.0 14.5 15.0 15.5 16.0 -10 0 10 20 -8.0 -7.5 -7.0 -6.5 -6.0 -5.5 -5.0 -4.5 -4.0 -3.5 -3.0 -2.5 -2.0 -1.5 -1.0 -0.5 0.0 0.5 1.0 1.5 2.0 2.5 3.0 3.5 4.0 4.5 5.0 5.5 6.0 6.5 7.0 7.5 8.0 8.5 9.0 9.5 10.0 10.5 11.0 11.5 12.0 12.5 13.0 13.5 14.0 14.5 15.0 15.5 16.0 y1 y2 -5 -4 -3 -2 -1 0 1 2 3 4 5 6 -4.0 -3.9 -3.8 -3.7 -3.6 -3.5 -3.4 -3.3 -3.2 -3.1 -3.0 -2.9 -2.8 -2.7 -2.6 -2.5 -2.4 -2.3 -2.2 -2.1 -2.0 -1.9 -1.8 -1.7 -1.6 -1.5 -1.4 -1.3 -1.2 -1.1 -1.0 -0.9 -0.8 -0.7 -0.6 -0.5 -0.4 -0.3 -0.2 -0.1 0.0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1.0 1.1 1.2 1.3 1.4 1.5 1.6 1.7 1.8 1.9 2.0 2.1 2.2 2.3 2.4 2.5 2.6 2.7 2.8 2.9 3.0 3.1 3.2 3.3 3.4 3.5 3.6 3.7 3.8 3.9 4.0 4.1 4.2 4.3 4.4 4.5 4.6 4.7 4.8 4.9 5.0 -5 0 5 -4.0 -3.8 -3.6 -3.4 -3.2 -3.0 -2.8 -2.6 -2.4 -2.2 -2.0 -1.8 -1.6 -1.4 -1.2 -1.0 -0.8 -0.6 -0.4 -0.2 0.0 0.2 0.4 0.6 0.8 1.0 1.2 1.4 1.6 1.8 2.0 2.2 2.4 2.6 2.8 3.0 3.2 3.4 3.6 3.8 4.0 4.2 4.4 4.6 4.8 5.0

Making a plot is easy, where to plot is the possibly difficult question.

Zeros

Finding zeros of a function is aided by some functions in the Roots package:

f(x) = (x-1) * (x-2) * (x^2 + 2x + 2)
roots(f)
4-element Array{Complex{Float64},1}:
 -1.0+1.0im
 -1.0-1.0im
  1.0+0.0im
  2.0+0.0im

(Note, this uses complex values as some of the values are complex.) Whereas this finds the real roots:

fzeros(f)
2-element Array{Real,1}:
 1
 2
f(x) = exp(-3x) - 1/2
plot(f, 0, 5)			# zero between 0 and 1
fzero(f, [0, 1])
0.23104906018664842

Okay, start your engines

Use these cells or add some more to answer the questions on the exam.