We have been using Julia
to solve problems in calculus. So far we have looked at
Julia
Julia
To do the plotting, you need to load an external package. This is the command you need to run:
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)
Once defined, the function is evaluated at a value (called) with the usual notation:
f(2)
0.25565380596206977
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. For this class, this point is more important for being able to read things, not for writing things.
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]$. To do so, we add to a plot with plot!
:
f(x) = x^2 - 2x + 3 plot(f, -3, 4)
f(x) = cos(x) plot(f, 0, 2pi) g(x) = 1 + f(2*x) plot!(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 or where a function is increasing or decreasing. Don't be afraid to change the values of a
and b
to make new plots over different domains.
For example, to find where $f(x) = \tan(x) = 0$ don't do this:
f(x) = tan(x) plot(f, -pi/2, pi/2) plot!(zero, -pi/2, pi/2)
But rather, back off from the vertical asymptotes:
plot(f, -pi/4, pi/4) plot!(zero, -pi/4, pi/4)
Whereas to see horizontal asymptotes of $f(x) = \tan^{-1}(x)$, don't do this:
f(x) = atan(x) plot(f, -.1, .1)
Rather, back away:
plot(f, -25, 25)