[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 MTH229
The basic ideas of a calculator easily correspond to ideas of julia:
+
, -
, *
, /
and ^
utilize the same order of operations (PEMDAS) that they do on most calculators, save for expressions like 5x
where that multiplication happens before the "P".sin()
. Buttons like "sin$^{-1}$" are not spelled that way as functions (asin
in this case). In fact the biggest gotcha is translating the shorthand of math related to powers to valid commands in julia
.=
in julia
is assignment. In math it usually implies solving an equation. This is completely different. The left hand side of the =
sign in julia is just a name or function name with arguments, not an expression.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 is done in julia
through external packages. Loading the MTH229
package loads the Plots
package which 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)
f(x) = cos(x) g(x) = 1 + f(2*x) plot([f,g], 0, 2pi)
Making a plot is easy, where to plot is the possibly difficult question. This is because there are many different things we do with plots: looking at the "large" to find horizontal or slant asymptotes, or vertical asymptotes and looking small to see, for example, zeros of a function. Don't be afraid to change the values of a
and b
to make new plots over different domains.
Finding zeros of a function is aided by some functions in the Roots
package:
f
is a polynomial function, then fzeros(f)
will return all the real zeros of f
f(x) = (x-1) * (x-2) * (x^2 + 2x + 2) fzeros(f)
2-element Array{Real,1}: 1 2
f
the command fzeros(f, a, b)
will look for all zeros within [a,b]
. This function may miss values.f
is not a polynomial,: fzero(f,a,b)
is guaranteed to find a zero in the interval $[a,b]$ provided there is obviously one there – when $f$ is continuous and $f(a)$ and $f(b)$ have different signs. This is because the bisection method – which uses the intermediate value theorem – provides a guarantee.f(x) = exp(-3x) - 1/2 plot(f, 0, 5) # zero between 0 and 1 fzero(f, [0, 1])
0.23104906018664842
Use these cells or add some more to answer the questions on the exam.