[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
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. 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)
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.
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 and roots(f)
all the zeros (which are called "roots" when speaking of polynomials.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
is not a polynomial, there is nothing general, but: fzero(f, [a,b])
is guaranteed to find a zero in the interval $[a,b]$ provided there is obviously one there, whichi is when $f$ is continuous and $f(a)$ and $f(b)$ have different signs. Further, fzeros(f, [a,b])
will try to find all simple zeros in the interval $[a,b]$. This is not guaranteed to work, but usually does.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.