use quarto, not Pluto to render pages

This commit is contained in:
jverzani
2022-07-24 16:38:24 -04:00
parent 93c993206a
commit 7b37ca828c
879 changed files with 793311 additions and 2678 deletions

View File

@@ -77,6 +77,7 @@ on how it is being called. Within `Pluto`, a message about
this will be noted.
The basic arithmetic operations on a calculator are "+", "-", "×",
"÷", and "$xʸ$". These have parallels in `Julia` through the *binary*
operators: `+`, `-`, `*`, `/`, and `^`:
@@ -320,23 +321,21 @@ To emphasize, this is not the same as the value without the parentheses:
1 + 2 / 3 + 4
```
```julia; echo=false;
alert(L"""
!!! warning
The viniculum also indicates grouping when used with the
square root (the top bar), and complex conjugation. That usage is
often clear enough, but the usage of the viniculum in division
often leads to confusion. The example above is one where the
parentheses are often, erroneously, omitted. However, more
confusion can arise when there is more than one vinicula. An
expression such as $a/b/c$ written inline has no confusion, it is:
$(a/b) / c$ as left association is used; but when written with a
pair of vinicula there is often the typographical convention of a
slightly longer vinicula to indicate which is to be considered
first. In the absence of that, then top to bottom association is
often implied.
The viniculum also indicates grouping when used with the square root
(the top bar), and complex conjugation. That usage is often clear
enough, but the usage of the viniculum in division often leads to
confusion. The example above is one where the parentheses are often,
erroneously, omitted. However, more confusion can arise when there is
more than one vinicula. An expression such as $a/b/c$ written inline
has no confusion, it is: $(a/b) / c$ as left association is used; but
when written with a pair of vinicula there is often the typographical
convention of a slightly longer vinicula to indicate which is to
be considered first. In the absence of that, then top to bottom association is
often implied.
""")
```
### Infix, postfix, and prefix notation
@@ -356,13 +355,9 @@ unfamiliar with. These mirror the familiar notation from most math
texts.
```julia; echo=false;
note("""
!!! note
In `Julia` many infix operations can be done using a prefix manner. For example `14 + 2` can also be evaluated by `+(14,2)`. There are very few *postfix* operations, though in these notes we will overload one, the `'` operation, to indicate a derivative.
In `Julia` many infix operations can be done using a prefix manner. For example `14 + 2` can also be evaluated by `+(14,2)`. There are very few *postfix* operations, though in these notes we will overload one, the `'` operation, to indicate a derivative.
""")
```
## Constants
@@ -378,11 +373,9 @@ Whereas, `e` is is not simply the character `e`, but *rather* a [Unicode](../uni
```
```julia; echo=false
note("""
However, when the accompanying package, `CalculusWithJulia`, is loaded, the character `e` will refer to a floating point approximation to the Euler constant .
""")
```
!!! note
However, when the accompanying package, `CalculusWithJulia`, is loaded, the character `e` will refer to a floating point approximation to the Euler constant .
In the sequel, we will just use `e` for this constant (though more commonly the `exp` function), with the reminder that base `Julia` alone does not reserve this symbol.
@@ -393,10 +386,12 @@ Mathematically these are irrational values with decimal expansions that do not r
```
```julia; echo=false;
alert("""In most cases. There are occasional (basically rare) spots where using `pi` by itself causes an eror where `1*pi` will not. The reason is `1*pi` will create a floating point value from the irrational object, `pi`.
""")
```
!!! warning
In most cases. There are occasional (basically rare) spots
where using `pi` by itself causes an eror where `1*pi` will
not. The reason is `1*pi` will create a floating point value from
the irrational object, `pi`.
### Numeric literals
@@ -504,19 +499,14 @@ exp(2), log(10), sqrt(100), 10^(1/2)
```
!!! note
Parentheses have many roles. We've just seen that parentheses may be
used for grouping, and now we see they are used to indicate a function
is being called. These are familiar from their parallel usage in
traditional math notation. In `Julia`, a third usage is common, the
making of a "tuple," or a container of different objects, for example
`(1, sqrt(2), pi)`. In these notes, the output of multiple commands separated by commas is a printed tuple.
```julia; echo=false;
note("""
Parentheses have many roles. We've just seen that parentheses may be
used for grouping, and now we see they are used to indicate a function
is being called. These are familiar from their parallel usage in
traditional math notation. In `Julia`, a third usage is common, the
making of a "tuple," or a container of different objects, for example
`(1, sqrt(2), pi)`. In these notes, the output of multiple commands separated by commas is a printed tuple.
""")
```
@@ -621,21 +611,17 @@ exact, can represent much bigger values and are exact for a reasonably
wide range of integer values.)
```julia; echo=false;
alert("""
!!! warning
In a turnaround from a classic blues song, we can think of
`Julia` as built for speed, not for comfort. All of these errors
above could be worked around so that the end user doesn't see
them. However, this would require slowing things down, either
through checking of operations or allowing different types of
outputs for similar type of inputs. These are tradeoffs that are
not made for performance reasons. For the most part, the tradeoffs
don't get in the way, but learning where to be careful takes some
time. Error messages often suggest a proper alternative.
In a turnaround from a classic blues song, we can think of `Julia` as
built for speed, not for comfort. All of these errors above could be
worked around so that the end user doesn't see them. However, this
would require slowing things down, either through checking of
operations or allowing different types of outputs for similar type of
inputs. These are tradeoffs that are not made for performance
reasons. For the most part, the tradeoffs don't get in the way, but
learning where to be careful takes some time. Error messages
often suggest a proper alternative.
""")
```
##### Example
@@ -798,8 +784,8 @@ choices = [
q"(3 - 2)/ 4 - 1",
q"3 - 2 / (4 - 1)",
q"(3 - 2) / (4 - 1)"]
ans = 3
radioq(choices, ans)
answ = 3
radioq(choices, answ)
```
###### Question
@@ -818,8 +804,8 @@ choices = [
q"3 * 2 / 4",
q"(3 * 2) / 4"
]
ans = 1
radioq(choices, ans)
answ = 1
radioq(choices, answ)
```
###### Question
@@ -838,8 +824,8 @@ choices = [
q"2 ^ 4 - 2",
q"(2 ^ 4) - 2",
q"2 ^ (4 - 2)"]
ans = 3
radioq(choices, ans)
answ = 3
radioq(choices, answ)
```
###### Question
@@ -863,8 +849,8 @@ choices = [
q"1 / (2 / 3 / 4 / 5 / 6)",
q"1 / 2 * 3 / 4 * 5 / 6",
q"1 /(2 * 3 * 4 * 5 * 6)"]
ans = 3
radioq(choices, ans)
answ = 3
radioq(choices, answ)
```
###### Question
@@ -879,8 +865,8 @@ q"2 - 3 - 4",
q"(2 - 3) - 4",
q"2 - (3 - 4)"
];
ans = 3;
radioq(choices, ans)
answ = 3;
radioq(choices, answ)
```
@@ -895,8 +881,8 @@ q"2 - 3 * 4",
q"(2 - 3) * 4",
q"2 - (3 * 4)"
];
ans = 2;
radioq(choices, ans)
answ = 2;
radioq(choices, answ)
```
@@ -912,8 +898,8 @@ q"-1^2",
q"(-1)^2",
q"-(1^2)"
];
ans = 2;
radioq(choices, ans)
answ = 2;
radioq(choices, answ)
```
@@ -989,8 +975,8 @@ choices = [
raw"``e^{\pi}``",
raw"``\pi^{e}``"
];
ans = exp(pi) - pi^exp(1) > 0 ? 1 : 2;
radioq(choices, ans)
answ = exp(pi) - pi^exp(1) > 0 ? 1 : 2;
radioq(choices, answ)
```
###### Question
@@ -1002,8 +988,8 @@ What is the value of $\pi - (x - \sin(x)/\cos(x))$ when $x=3$?
```julia; hold=true; echo=false;
x = 3;
ans = x - sin(x)/cos(x);
numericq(pi - ans)
answ = x - sin(x)/cos(x);
numericq(pi - answ)
```
###### Question
@@ -1022,8 +1008,8 @@ Will `-2^2` produce `4` (which is a unary `-` evaluated *before* `^`) or `-4` (w
```julia; hold=true; echo=false;
choices = [q"4", q"-4"]
ans = 2
radioq(choices, ans)
answ = 2
radioq(choices, answ)
```
###### Question
@@ -1057,6 +1043,6 @@ choices = [
"The precedence of numeric literal coefficients used for implicit multiplication is higher than other binary operators such as multiplication (`*`), and division (`/`, `\\`, and `//`)",
"Of course it is correct."
]
ans=1
radioq(choices, ans)
answ=1
radioq(choices, answ)
```

View File

@@ -165,11 +165,9 @@ That would be an enormous growth. Don't worry: "Exponential growth
cannot continue indefinitely, however, because the medium is soon
depleted of nutrients and enriched with wastes."
```julia; echo=false
note("""
The value of `2^n` and `2.0^n` is different in `Julia`. The former remains an integer and is subject to integer overflow for `n > 62`. As used above, `2^(n/6)` will not overflow for larger `n`, as when the exponent is a floating point value, the base is promoted to a floating point value.
""")
```
!!! note
The value of `2^n` and `2.0^n` is different in `Julia`. The former remains an integer and is subject to integer overflow for `n > 62`. As used above, `2^(n/6)` will not overflow for larger `n`, as when the exponent is a floating point value, the base is promoted to a floating point value.
##### Example
@@ -366,11 +364,9 @@ If $1/10$ of the original carbon ``14`` remains, how old is the item? This amoun
-5730 * log2(1/10)
```
```julia; echo=false
note("""
(Historically) Libby and James Arnold proceeded to test the radiocarbon dating theory by analyzing samples with known ages. For example, two samples taken from the tombs of two Egyptian kings, Zoser and Sneferu, independently dated to ``2625`` BC plus or minus ``75`` years, were dated by radiocarbon measurement to an average of ``2800`` BC plus or minus ``250`` years. These results were published in Science in ``1949``. Within ``11`` years of their announcement, more than ``20`` radiocarbon dating laboratories had been set up worldwide. Source: [Wikipedia](http://tinyurl.com/p5msnh6).
""")
```
!!! note
(Historically) Libby and James Arnold proceeded to test the radiocarbon dating theory by analyzing samples with known ages. For example, two samples taken from the tombs of two Egyptian kings, Zoser and Sneferu, independently dated to ``2625`` BC plus or minus ``75`` years, were dated by radiocarbon measurement to an average of ``2800`` BC plus or minus ``250`` years. These results were published in Science in ``1949``. Within ``11`` years of their announcement, more than ``20`` radiocarbon dating laboratories had been set up worldwide. Source: [Wikipedia](http://tinyurl.com/p5msnh6).
### Properties of logarithms
@@ -503,8 +499,8 @@ Which is bigger $e^2$ or $2^e$?
```julia; hold=true; echo=false
choices = ["``e^2``", "``2^e``"]
ans = e^2 - 2^e > 0 ? 1 : 2
radioq(choices, ans)
answ = e^2 - 2^e > 0 ? 1 : 2
radioq(choices, answ)
```
@@ -514,8 +510,8 @@ Which is bigger $\log_8(9)$ or $\log_9(10)$?
```julia; hold=true; echo=false
choices = [raw"``\log_8(9)``", raw"``\log_9(10)``"]
ans = log(8,9) > log(9,10) ? 1 : 2
radioq(choices, ans)
answ = log(8,9) > log(9,10) ? 1 : 2
radioq(choices, answ)
```
###### Question
@@ -529,8 +525,8 @@ raw"``\frac{\log(2)\log(3)}{\log(5)\log(4)}``",
raw"``2/5``",
raw"``\frac{\log(5)\log(4)}{\log(3)\log(2)}``"
]
ans = 1
radioq(choices, ans)
answ = 1
radioq(choices, answ)
```
###### Question
@@ -538,8 +534,8 @@ radioq(choices, ans)
Does $12$ satisfy $\log_2(x) + \log_3(x) = \log_4(x)$?
```julia; hold=true; echo=false
ans = log(2,12) + log(3,12) == log(4, 12)
yesnoq(ans)
answ = log(2,12) + log(3,12) == log(4, 12)
yesnoq(answ)
```
@@ -563,8 +559,8 @@ for each?
```julia; hold=true; echo=false
choices = ["``1000`` times", "``100`` times", "``10`` times", "the same"]
ans = 2
radioq(choices, ans, keep_order=true)
answ = 2
radioq(choices, answ, keep_order=true)
```
###### Question
@@ -600,8 +596,8 @@ choices = [
raw"``x \geq 1 + \log(x)``",
raw"``x \leq 1 + \log(x)``"
]
ans = 1
radioq(choices, ans)
answ = 1
radioq(choices, answ)
```
@@ -623,8 +619,8 @@ choices = [
raw"``\log(1-x) \geq -x - x^2/2``",
raw"``\log(1-x) \leq -x - x^2/2``"
]
ans = 1
radioq(choices, ans)
answ = 1
radioq(choices, answ)
```
###### Question
@@ -634,8 +630,8 @@ reciprocal property of exponents, $a^{-x} = (1/a)^x$, is at play here.)
```julia; hold=true; echo=false
choices = ["``-y``", "``1/y``", "``-1/y``"]
ans = 1
radioq(choices, ans)
answ = 1
radioq(choices, answ)
```
Based on this, the graph of $\log_{1/a}(x)$ is the graph of
@@ -647,8 +643,8 @@ L"Flipped over the $x$ axis",
L"Flipped over the $y$ axis",
L"Flipped over the line $y=x$"
]
ans = 1
radioq(choices, ans)
answ = 1
radioq(choices, answ)
```
@@ -662,8 +658,8 @@ choices = [
raw"``a^{y-x}``",
raw"``a^{y-x} \cdot (a^x - 1)``"
]
ans = 1
radioq(choices, ans)
answ = 1
radioq(choices, answ)
```
Using $a > 1$ we have:
@@ -674,8 +670,8 @@ choices = [
L"as $a^x > 1$, $a^y > a^x$",
"``a^{y-x} > 0``"
]
ans=1
radioq(choices, ans)
answ=1
radioq(choices, answ)
```
If $a < 1$ then:
@@ -686,6 +682,6 @@ L"as $a^{y-x} < 1$ as $y-x > 0$, $a^y < a^x$",
L"as $a^x < 1$, $a^y < a^x$",
"``a^{y-x} < 0``"
]
ans = 1
radioq(choices, ans)
answ = 1
radioq(choices, answ)
```

View File

@@ -54,10 +54,8 @@ possible. The function $h(x)$ would have range
$[0,\infty)$. The $s(x)$ function is either $-1$ or $1$, so only has two possible values in its range. What about $g(x)$? It is a parabola that opens upward, so any $y$ values below the $y$ value of its vertex will not appear in the range. In this case, the symmetry indicates that the vertex will be at $(1/2, -1/4)$, so the range is $[-1/4, \infty)$.
```julia;echo=false;
note("""
**Thanks to Euler (1707-1783):** The formal idea of a function is a relatively modern concept in mathematics. According to [Dunham](http://www.maa.org/sites/default/files/pdf/upload_library/22/Ford/dunham1.pdf),
!!! note
**Thanks to Euler (1707-1783):** The formal idea of a function is a relatively modern concept in mathematics. According to [Dunham](http://www.maa.org/sites/default/files/pdf/upload_library/22/Ford/dunham1.pdf),
Euler defined a function as an "analytic expression composed in any way
whatsoever of the variable quantity and numbers or constant
quantities." He goes on to indicate that as Euler matured, so did
@@ -66,8 +64,6 @@ note("""
“analytic expression.” He finishes by saying: "It is fair to say
that we now study functions in analysis because of him."
""")
```
We will see that defining functions within `Julia` can be as simple a concept as Euler started with, but that the more abstract concept has a great advantage that is exploited in the design of the language.
@@ -92,28 +88,24 @@ f(pi), g(2), h(4)
For typical cases like the three above, there isn't really much new to learn.
```julia;echo=false;
note("""
!!! note
The equals sign in `Julia` always indicates either an assignment or a
mutation of the object on the left side. The definition of a function
above is an *assignment*, in that a function is added (or modified) in
a table holding the methods associated with the function's name.
The equals sign in `Julia` always indicates either an assignment or a
mutation of the object on the left side. The definition of a function
above is an *assignment*, in that a function is added (or modified) in
a table holding the methods associated with the function's name.
The equals sign restricts the expressions available on the *left*-hand
side to a) a variable name, for assignment; b) mutating an object at an index,
as in `xs[1]`; c) mutating a property of a stuct; or d) a function assignment
following this form `function_name(args...)`.
The equals sign restricts the expressions available on the *left*-hand
side to a) a variable name, for assignment; b) mutating an object at an index,
as in `xs[1]`; c) mutating a property of a stuct; or d) a function assignment
following this form `function_name(args...)`.
Whereas function
definitions and usage in `Julia` mirrors standard math notation;
equations in math are not so mirrored in `Julia`. In mathematical
equations, the left-hand of an equation is typically a complicated
algebraic expression. Not so with `Julia`, where the left hand side of
the equals sign is prescribed and quite limited.
Whereas function
definitions and usage in `Julia` mirrors standard math notation;
equations in math are not so mirrored in `Julia`. In mathematical
equations, the left-hand of an equation is typically a complicated
algebraic expression. Not so with `Julia`, where the left hand side of
the equals sign is prescribed and quite limited.
""")
```
### The domain of a function
@@ -244,17 +236,12 @@ Or similarly, a function to represent a cell phone plan where the first ``500``
cellplan(x) = x < 500 ? 20.0 : 20.0 + 0.05 * (x-500)
```
```julia;echo=false;
alert("""
Type stability. These last two definitions used `10.0` and `20.0`
instead of the integers `10` and `20` for the answer. Why the extra
typing? When `Julia` can predict the type of the output from the type
of inputs, it can be more efficient. So when possible, we help out and
ensure the output is always the same type.
""")
```
!!! warning
Type stability. These last two definitions used `10.0` and `20.0`
instead of the integers `10` and `20` for the answer. Why the extra
typing? When `Julia` can predict the type of the output from the type
of inputs, it can be more efficient. So when possible, we help out and
ensure the output is always the same type.
##### Example
@@ -303,11 +290,9 @@ end
The line `return x^2`, could have just been `x^2` as it is the last (and) only line evaluated.
```julia;echo=false;
note("""
The `return` keyword is not a function, so is not called with parentheses. An emtpy `return` statement will return a value of `nothing`.
""")
```
!!! note
The `return` keyword is not a function, so is not called with parentheses. An emtpy `return` statement will return a value of `nothing`.
##### Example
@@ -602,11 +587,9 @@ just uses the change-of-base formula for logarithms.
But not so fast, on the left side is a function with two arguments and on the right side the functions have one argument - yet they share the same name. How does `Julia` know which to use? `Julia` uses the number, order, and *type* of the positional arguments passed to a function to determine which function definition to use. This is technically known as [multiple dispatch](http://en.wikipedia.org/wiki/Multiple_dispatch) or **polymorphism**. As a feature of the language, it can be used to greatly simplify the number of functions the user must learn. The basic idea is that many functions are "generic" in that they have methods which will work differently in different scenarios.
```julia;echo=false;
alert("""
Multiple dispatch is very common in mathematics. For example, we learn different ways to add: integers (fingers, carrying), real numbers (align the decimal points), rational numbers (common denominators), complex numbers (add components), vectors (add components), polynomials (combine like monomials), ... yet we just use the same `+` notation for each operation. The concepts are related, the details different.
""")
```
!!! warning
Multiple dispatch is very common in mathematics. For example, we learn different ways to add: integers (fingers, carrying), real numbers (align the decimal points), rational numbers (common denominators), complex numbers (add components), vectors (add components), polynomials (combine like monomials), ... yet we just use the same `+` notation for each operation. The concepts are related, the details different.
`Julia` is similarly structured. `Julia` terminology would be to call the operation "`+`" a *generic function* and the different implementations *methods* of "`+`". This allows the user to just need to know a smaller collection of generic concepts yet still have the power of detail-specific implementations. To see how many different methods are defined in the base `Julia` language for the `+` operator, we can use the command `methods(+)`. As there are so many ($\approx 200$) and that number is growing, we illustrate how many different logarithm methods are implemented for "numbers:"
@@ -720,17 +703,15 @@ creates a function, this function is returned by `shift_right`.
So we could have done something more complicated like:
```julia;
p(x) = x^2 - 2x
l = shift_right(p, c=3)
f(x) = x^2 - 2x
l = shift_right(f, c=3)
```
Then `l` is a function that is derived from `p`.
Then `l` is a function that is derived from `f`.
!!! note
The value of `c` used when `l` is called is the one passed to `shift_right`. Functions like `l` that are returned by other functions also are called *closures*, as the context they are evaluated within includes the context of the function that constructs them.
```julia;echo=false
note("""
The value of `c` used when `l` is called is the one passed to `shift_right`. Functions like `l` that are returned by other functions also are called *closures*, as the context they are evaluated within includes the context of the function that constructs them.
""")
```
Anonymous functions can be created with the `function` keyword, but we will use the "arrow" notation, `arg->body` to create them, The above, could have been defined as:
@@ -741,32 +722,27 @@ shift_right_alt(f; c=0) = x -> f(x-c)
When the `->` is seen a function is being created.
```julia;echo=false;
alert("""
Generic versus anonymous functions. Julia has two types of functions,
generic ones, as defined by `f(x)=x^2` and anonymous ones, as defined
by `x -> x^2`. One gotcha is that `Julia` does not like to use the
same variable name for the two types. In general, Julia is a dynamic
language, meaning variable names can be reused with different types
of variables. But generic functions take more care, as when a new
method is defined it gets added to a method table. So repurposing the
name of a generic function for something else is not allowed (well, in `Pluto` it is.). Similarly,
repurposing an already defined variable name for a generic function is
not allowed. This comes up when we use functions that return functions
as we have different styles that can be used: When we defined `l =
shift_right(f, c=3)` the value of `l` is assigned an anonymous
function. This binding can be reused to define other variables.
However, we could have defined the function `l` through `l(x) =
shift_right(f, c=3)(x)`, being explicit about what happens to the
variable `x`. This would add a method to the generic function `l`. Meaning, we
get an error if we tried to assign a variable to `l`, such as an
expression like `l=3`. We generally employ the latter style, even though
it involves a bit more typing, as we tend to stick to methods of generic
functions for consistency.
""")
```
!!! warning
Generic versus anonymous functions. Julia has two types of functions,
generic ones, as defined by `f(x)=x^2` and anonymous ones, as defined
by `x -> x^2`. One gotcha is that `Julia` does not like to use the
same variable name for the two types. In general, Julia is a dynamic
language, meaning variable names can be reused with different types
of variables. But generic functions take more care, as when a new
method is defined it gets added to a method table. So repurposing the
name of a generic function for something else is not allowed. Similarly,
repurposing an already defined variable name for a generic function is
not allowed. This comes up when we use functions that return functions
as we have different styles that can be used: When we defined `l =
shift_right(f, c=3)` the value of `l` is assigned an anonymous
function. This binding can be reused to define other variables.
However, we could have defined the function `l` through `l(x) =
shift_right(f, c=3)(x)`, being explicit about what happens to the
variable `x`. This would add a method to the generic function `l`. Meaning, we
get an error if we tried to assign a variable to `l`, such as an
expression like `l=3`. We generally employ the latter style, even though
it involves a bit more typing, as we tend to stick to methods of generic
functions for consistency.
##### Example: the secant line
@@ -863,8 +839,8 @@ choices = [
"Domain is all non-negative numbers, range is all real numbers",
"Domain is all non-negative numbers, range is all non-negative numbers"
]
ans = 2
radioq(choices, ans)
answ = 2
radioq(choices, answ)
```
##### Question
@@ -879,8 +855,8 @@ L"Domain is all real numbers except $2$, range is all real numbers except $0$",
L"Domain is all non-negative numbers except $0$, range is all real numbers except $2$",
L"Domain is all non-negative numbers except $-2$, range is all non-negative numbers except $0$"
]
ans = 2
radioq(choices, ans)
answ = 2
radioq(choices, answ)
```
##### Question
@@ -893,8 +869,8 @@ raw"``f(x) = 2^x``",
raw"``f(x) = 1/x^2``",
raw"``f(x) = |x|``",
raw"``f(x) = \sqrt{x}``"]
ans = 1
radioq(choices, ans)
answ = 1
radioq(choices, answ)
```
@@ -908,8 +884,8 @@ q"function f(x) = sin(x + pi/3)",
q"f(x) = sin(x + pi/3)",
q"f: x -> sin(x + pi/3)",
q"f x = sin(x + pi/3)"]
ans = 3
radioq(choices, ans)
answ = 3
radioq(choices, answ)
```
###### Question
@@ -923,8 +899,8 @@ q"f(x) := (1 + x^2)^(-1)",
q"f[x] = (1 + x^2)^(-1)",
q"def f(x): (1 + x^2)^(-1)"
]
ans = 1
radioq(choices, ans)
answ = 1
radioq(choices, answ)
```
@@ -1057,8 +1033,8 @@ Will the call `C(1, mu=70)` use a value of `70` for `mu`?
```julia; hold=true;echo=false;
choices = ["Yes, this will work just as it does for keyword arguments",
"No, there will be an error that the function does not accept keyword arguments"]
ans = 2
radioq(choices, ans)
answ = 2
radioq(choices, answ)
```
###### Question
@@ -1077,8 +1053,8 @@ choices = [
"If `x` is in `[a,b]` it returns `x`, otherwise it returns `NaN`",
"`x` is the larger of the minimum of `x` and `a` and the value of `b`, aka `max(min(x,a),b)`"
]
ans = 1
radioq(choices, ans)
answ = 1
radioq(choices, answ)
```
@@ -1108,8 +1084,8 @@ L"You still get $0.649...$",
"You get a `MethodError`, as `cos(pi/4)` is evaluated as a number and `∘` is not defined for functions and numbers",
"You get a `generic` function, but this won't be callable. If tried, it will give an method error."
]
ans = 2
radioq(choices, ans, keep_order=true)
answ = 2
radioq(choices, answ, keep_order=true)
```
###### Question
@@ -1121,8 +1097,8 @@ choices = [
"It is `0.6663667453928805`, the same as `cos(sin(1))`",
"It is `0.5143952585235492`, the same as `sin(cos(1))`",
"It gives an error"]
ans = 1
radioq(choices, ans, keep_order=true)
answ = 1
radioq(choices, answ, keep_order=true)
```
###### Question
@@ -1136,8 +1112,8 @@ fn(3)
```julia; hold=true;echo=false;
choices = ["`true`","`false`"]
ans = 1
radioq(choices, ans)
answ = 1
radioq(choices, answ)
```
###### Question
@@ -1160,8 +1136,8 @@ Repeat the secant method two more times to find a better approximation for $\sqr
```julia; hold=true;echo=false;
choices = [q"4//3", q"7//5", q"58//41", q"816//577"]
ans = 3
radioq(choices, ans, keep_order=true)
answ = 3
radioq(choices, answ, keep_order=true)
```
How small is the value of $f(c)$ for this value?
@@ -1175,8 +1151,8 @@ How close is this answer to the true value of $\sqrt{2}$?
```julia; hold=true;echo=false;
choices = [L"about $8$ parts in $100$", L"about $1$ parts in $100$", L"about $4$ parts in $10,000$", L"about $2$ parts in $1,000,000$"]
ans = 3
radioq(choices, ans, keep_order=true)
answ = 3
radioq(choices, answ, keep_order=true)
```
(Finding a good approximation to $\sqrt{2}$ would be helpful to builders, for example, as it could be used to verify the trueness of a square room, say.)
@@ -1190,8 +1166,8 @@ choices = ["Just use `f = h - g`",
"Define `f(x) = h(x) - g(x)`",
"Use `x -> h(x) - g(x)` when the difference is needed"
]
ans = 1
radioq(choices, ans)
answ = 1
radioq(choices, answ)
```
###### Question

View File

@@ -120,10 +120,9 @@ From this, we have the function $g(y) = (y-32) / (9/5)$ is the inverse function
*Usually* we use the name $f^{-1}$ for the inverse function of $f$, so this would be most often [seen](http://tinyurl.com/qypbueb) as $f^{-1}(x) = (x-32)/(9/5)$ or after simplification $f^{-1}(x) = (5/9) \cdot (x-32)$.
```julia; echo=false
note(L"""The use of a negative exponent on the function name is *easily* confused for the notation for a reciprocal when it is used on a mathematical *expression*. An example might be the notation $(1/x)^{-1}$. As this is an expression this would simplify to $x$ and not the inverse of the *function* $f(x)=1/x$ (which is $f^{-1}(x) = 1/x$).
""")
```
!!! note
The use of a negative exponent on the function name is *easily* confused for the notation for a reciprocal when it is used on a mathematical *expression*. An example might be the notation $(1/x)^{-1}$. As this is an expression this would simplify to $x$ and not the inverse of the *function* $f(x)=1/x$ (which is $f^{-1}(x) = 1/x$).
##### Example
@@ -277,10 +276,9 @@ same scale, so that this type of line will look perpendicular.)
One consequence of this symmetry, is that if $f$ is strictly increasing, then so is its inverse.
```julia; echo=false
note(L"""In the above we used `cbrt(x)` and not `x^(1/3)`. The latter usage assumes that $x \geq 0$ as it isn't guaranteed that for all real exponents the answer will be a real number. The `cbrt` function knows there will always be a real answer and provides it.
""")
```
!!!note
In the above we used `cbrt(x)` and not `x^(1/3)`. The latter usage assumes that $x \geq 0$ as it isn't guaranteed that for all real exponents the answer will be a real number. The `cbrt` function knows there will always be a real answer and provides it.
### Lines
@@ -323,8 +321,8 @@ Is it possible that a function have two different inverses?
```julia; hold=true; echo=false
choices = [L"No, for all $x$ in the domain an an inverse, the value of any inverse will be the same, hence all inverse functions would be identical.",
L"Yes, the function $f(x) = x^2, x \geq 0$ will have a different inverse than the same function $f(x) = x^2, x \leq 0$"]
ans = 1
radioq(choices, ans)
answ = 1
radioq(choices, answ)
```
###### Question
@@ -335,8 +333,8 @@ A function takes a value $x$ adds $1$, divides by $2$, and then subtracts $1$. I
choices = [L"Yes, the function is the linear function $f(x)=(x+1)/2 + 1$ and so is monotonic.",
L"No, the function is $1$ then $2$ then $1$, but not \"one-to-one\""
]
ans = 1
radioq(choices, ans)
answ = 1
radioq(choices, answ)
```
###### Question
@@ -347,8 +345,8 @@ Is the function $f(x) = x^5 - x - 1$ one-to-one?
choices=[L"Yes, a graph over $(-100, 100)$ will show this.",
L"No, a graph over $(-2,2)$ will show this."
]
ans = 2
radioq(choices, ans)
answ = 2
radioq(choices, answ)
```
###### Question
@@ -396,16 +394,16 @@ What is $g(x) = (f(x))^{-1}$?
```julia; hold=true; echo=false
choices = ["``g(x) = x``", "``g(x) = x^{-1}``"]
ans = 1
radioq(choices, ans)
answ = 1
radioq(choices, answ)
```
What is $g(x) = f^{-1}(x)$?
```julia; hold=true; echo=false
choices = ["``g(x) = x``", "``g(x) = x^{-1}``"]
ans = 2
radioq(choices, ans)
answ = 2
radioq(choices, answ)
```
###### Question
@@ -457,8 +455,8 @@ L"The function that multiplies by $2$, subtracts $1$ and then squares the value.
L"The function that divides by $2$, adds $1$, and then takes the square root of the value.",
L"The function that takes square of the value, then subtracts $1$, and finally multiplies by $2$."
]
ans = 1
radioq(choices, ans)
answ = 1
radioq(choices, answ)
```
###### Question
@@ -509,8 +507,8 @@ choices = [
"``f^{-1}(x) = (5y-4)^3``",
"``f^{-1}(x) = 5/(x^3 + 4)``"
]
ans = 1
radioq(choices, ans)
answ = 1
radioq(choices, answ)
```
@@ -524,8 +522,8 @@ raw"``f^{-1}(x) = (x-e)^{1/\pi}``",
raw"``f^{-1}(x) = (x-\pi)^{e}``",
raw"``f^{-1}(x) = (x-e)^{\pi}``"
]
ans = 1
radioq(choices, ans)
answ = 1
radioq(choices, answ)
```
###### Question
@@ -536,8 +534,8 @@ choices = [
raw"``[7, \infty)``",
raw"``(-\infty, \infty)``",
raw"``[0, \infty)``"]
ans = 1
radioq(choices, ans)
answ = 1
radioq(choices, answ)
```
###### Question
@@ -550,8 +548,8 @@ choices = [
raw"``[7, \infty)``",
raw"``(-\infty, \infty)``",
raw"``[0, \infty)``"]
ans = 3
radioq(choices, ans)
answ = 3
radioq(choices, answ)
```
###### Question
@@ -620,8 +618,8 @@ choices = [
L"As $f_4(f_3(f_2(f_1(x))))=(f_1 \circ f_2 \circ f_3 \circ f_4)(x)$",
"As the latter is more complicated than the former."
]
ans=1
radioq(choices, ans)
answ=1
radioq(choices, answ)
```
@@ -631,8 +629,8 @@ What is $g_2(x)=f_2^{-1}(x)$?
```julia; hold=true; echo=false
choices = ["``g_2(x) = x^{-1}``", "``g_2(x) = x``", "``g_2(x) = x -1``"]
ans = 1
radioq(choices, ans)
answ = 1
radioq(choices, answ)
```
What is $g_3(x)=f_3^{-1}(x)$?
@@ -642,8 +640,8 @@ choices = [
raw"``c^2/(b\cdot c - a\cdot d) \cdot x``",
raw"``(b\cdot c-a\cdot d)/c^2 \cdot x``",
raw"``c^2 x``"]
ans = 1
radioq(choices, ans)
answ = 1
radioq(choices, answ)
```
Given these, what is the value of $g_4(g_3(g_2(g_1(f_4(f_3(f_2(f_1(10))))))))$?

View File

@@ -26,10 +26,6 @@ The [`Julia`](http://www.julialang.org) programming language is well suited as a
## Interacting with `Julia`
The html version of the **Calculus With Julia** notes are formatted as
Pluto notebooks. `Pluto` is one of many different means for a user to
interact with a `Julia` process.
At a basic level, `Julia` provides a means to read commands or instructions, evaluate those commands, and then print or return those commands. At a user level, there are many different ways to interact with the reading and printing. For example:
* The REPL. The `Julia` terminal is the built-in means to interact with `Julia`. A `Julia` Terminal has a command prompt, after which commands are typed and then sent to be evaluated by the `enter` key. The terminal may look something like the following where `2+2` is evaluated:
@@ -57,7 +53,7 @@ julia> 2 + 2
* A notebook. The [Project Juptyer](https://jupyter.org/) provides a notebook interface for interacting with `Julia` and a more `IDE` style `jupyterlab` interface. A jupyter notebook has cells where commands are typed and immediately following is the printed output returned by `Julia`. The output of a cell depends on the state of the kernel when the cell is computed, not the order of the cells in the notebook. Cells have a number attached, showing the execution order. The `Juypter` notebook is used by `binder` and can be used locally through the `IJulia` package. This notebook has the ability to display many different types of outputs in addition to plain text, such as images, marked up math text, etc.
* The [Pluto](https://github.com/fonsp/Pluto.jl) package provides a *reactive* notebook interface. Reactive means when one "cell" is modified and executed, the new values cascade to all other dependent cells which in turn are updated. This is very useful for exploring a parameter space, say. These html pages are formatted as `Pluto` notebooks, which makes them able to be easily run on the reader's desktop.
* The [Pluto](https://github.com/fonsp/Pluto.jl) package provides a *reactive* notebook interface. Reactive means when one "cell" is modified and executed, the new values cascade to all other dependent cells which in turn are updated. This is very useful for exploring a parameter space, say. Pluto notebooks can be exported as HTML files which make them easy to read online and -- by clever design -- embed the `.jl` file that can run through `Pluto` if it is downloaded.
The `Pluto` interface has some idiosyncracies that need explanation:
@@ -103,11 +99,9 @@ Pkg.add("CalculusWithJulia")
This command instructs `Julia` to look at its *general registry* for the `CalculusWithJulia.jl` package, download it, then install it. Once installed, a package only needs to be brought into play with the `using` or `import` commands.
```julia; echo=false;
note("""
In a terminal setting, there is a package mode, entered by typing `]` as the leading character and exited by entering `<delete>` at a blank line. This mode allows direct access to `Pkg` with a simpler syntax. The command above would be just `add CalculusWithJulia`.)
""")
```
!!! note
In a terminal setting, there is a package mode, entered by typing `]` as the leading character and exited by entering `<delete>` at a blank line. This mode allows direct access to `Pkg` with a simpler syntax. The command above would be just `add CalculusWithJulia`.)
Packages can be updated through the command `Pkg.up()`, and removed with `Pkg.rm(pkgname)`.
@@ -129,15 +123,16 @@ Pkg.add("HQuadrature") # for higher-dimensional integration
## `Julia` commands
In `Pluto`, commands are typed into a notebook cell:
In a `Jupyter` notebook or `Pluto` notebook, commands are typed into a
notebook cell:
```julia;
2 + 2 # use shift-enter to evaluate
```
Commands are executed by using `shift-enter` or the run button at the bottom right of a cell.
Commands are executed by using `shift-enter` or a run button near the cell.
Multiple commands per cell are possible if a `begin` or `let` block is used.
In `Jupyter` multiple commands per cell are allowed. In `Pluto`, a `begin` or `let` block is used to collect multiple commmands into a single call.
Commands may be separated by new lines or semicolons.
On a given line, anything **after** a `#` is a *comment* and is not processed.
@@ -146,7 +141,10 @@ The results of the last command executed will be displayed in an
output area. Separating values by commas allows more than one value to be
displayed. Plots are displayed when the plot object is returned by the last executed command.
The state of a Pluto notebook is a result of all the cells in the notebook being executed. The cell order does not impact this and can be rearranged by the user.
In `Jupyter`, the state of the notebook is a determined by the cells
executed along with their order. The state of a `Pluto` notebook is a
result of all the cells in the notebook being executed. The cell order
does not impact this and can be rearranged by the user.
## Numbers, variable types
@@ -472,11 +470,9 @@ With `Plots` loaded, we can plot a function by passing the function object by na
plot(sin, 0, 2pi) # plot a function - by name - over an interval [a,b]
```
```julia; echo=false
note("""
This is in the form of **the** basic pattern employed: `verb(function_object, arguments...)`. The verb in this example is `plot`, the object `sin`, the arguments `0, 2pi` to specify `[a,b]` domain to plot over.
""")
```
!!1 note
This is in the form of **the** basic pattern employed: `verb(function_object, arguments...)`. The verb in this example is `plot`, the object `sin`, the arguments `0, 2pi` to specify `[a,b]` domain to plot over.
Plotting more than one function over ```[a,b]``` is achieved through the `plot!` function, which modifies the existing plot (`plot` creates a new one) by adding a new layer:

View File

@@ -45,9 +45,9 @@ equal can also be represented with the Unicode `≤` (generated by
`\le[tab]`). Similarly, for greater than or equal, there is
`\ge[tab]`.
```julia; echo=false;
alert("The use of `==` is necessary, as `=` is used for assignment and mutation.")
```
!!! warning
The use of `==` is necessary, as `=` is used for assignment and mutation.")
The `!` operator takes a boolean value and negates it. It uses prefix notation:
@@ -80,9 +80,9 @@ a < b, a + c < b + c
Trying other choices will show that the two answers are either both `false` or both `true`.
```julia; echo=false;
alert(""" Well, almost... When `Inf` or `NaN` are involved, this may not hold, for example `1 + Inf < 2 + Inf` is actually `false`. As would be `1 + (typemax(1)-1) < 2 + (typemax(1)-1)`.""")
```
!!! warning
Well, almost... When `Inf` or `NaN` are involved, this may not hold, for example `1 + Inf < 2 + Inf` is actually `false`. As would be `1 + (typemax(1)-1) < 2 + (typemax(1)-1)`.
So adding or subtracting most any finite value from an inequality will preserve the inequality, just as it does for equations.
@@ -131,7 +131,7 @@ In summary we investigated numerically that the following hold:
- `a < b` if and only if `1/a > 1/b` for all finite, positive `a` and `b`.
#### Examples
### Examples
We now show some inequalities highlighted on this [Wikipedia](http://en.wikipedia.org/wiki/Inequality_%28mathematics%29) page.
@@ -337,8 +337,8 @@ choices = [
"`e^pi` is equal to `pi^e`",
"`e^pi` is less than `pi^e`"
]
ans = 1
radioq(choices, ans)
answ = 1
radioq(choices, answ)
```
###### Question
@@ -346,8 +346,8 @@ radioq(choices, ans)
Is $\sin(1000)$ positive?
```julia; hold=true; echo=false;
ans = (sin(1000) > 0)
yesnoq(ans)
answ = (sin(1000) > 0)
yesnoq(answ)
```
###### Question
@@ -359,8 +359,8 @@ choices = [
"``-1/a < -1/b``",
"``-1/a > -1/b``",
raw"``-1/a \geq -1/b``"]
ans = 3
radioq(choices, ans)
answ = 3
radioq(choices, answ)
```
@@ -372,8 +372,8 @@ Suppose you know $a < 0 < b$, is it true that $1/a > 1/b$?
choices = ["Yes, it is always true.",
"It can sometimes be true, though not always.",
L"It is never true, as $1/a$ is negative and $1/b$ is positive"]
ans = 3
radioq(choices, ans)
answ = 3
radioq(choices, answ)
```
###### Question
@@ -394,8 +394,8 @@ $x$. Which of these indicates the first negative value : `airyai(-1) <0`,
```julia; hold=true; echo=false;
choices = ["`airyai($i) < 0`" for i in -1:-1:-5]
ans = 3
radioq(choices, ans, keep_order=true)
answ = 3
radioq(choices, answ, keep_order=true)
```
###### Question
@@ -406,8 +406,8 @@ By trying three different values of $x > 0$ which of these could possibly be alw
choices = ["`x^x <= (1/e)^(1/e)`",
"`x^x == (1/e)^(1/e)`",
"`x^x >= (1/e)^(1/e)`"]
ans = 3
radioq(choices, ans)
answ = 3
radioq(choices, answ)
```
###### Question
@@ -420,8 +420,8 @@ when $x,y > 0$ and $0 < p < 1$:
choices = ["`(x+y)^p < x^p + y^p`",
"`(x+y)^p == x^p + y^p`",
"`(x+y)^p > x^p + y^p`"]
ans = 1
radioq(choices, ans)
answ = 1
radioq(choices, answ)
```
@@ -436,8 +436,8 @@ JSTOR, AMM, Vol.97, No.1, 1990). Which one?
choices = ["`a^a + b^b <= a^b + b^a`",
"`a^a + b^b >= a^b + b^a`",
"`a^b + b^a <= 1`"]
ans = 2
radioq(choices, ans)
answ = 2
radioq(choices, answ)
```
@@ -457,8 +457,8 @@ Which of the following is equivalent to $\lvert x - a\rvert > b$:
choices = [raw"``-b < x - a < b``",
raw"`` -b < x-a \text{ and } x - a < b``",
raw"``x - a < -b \text{ or } x - a > b``"]
ans = 3
radioq(choices, ans)
answ = 3
radioq(choices, answ)
```
@@ -494,8 +494,8 @@ Which of these will show DeMorgan's law holds when both values are `false`:
choices = ["`!(false && false) == (!false && !false)`",
"`!(false && false) == (false || false)`",
"`!(false && false) == (!false || !false)`"]
ans = 3
radioq(choices, ans)
answ = 3
radioq(choices, answ)
```
###### Question
@@ -505,8 +505,8 @@ For floating point numbers there are two special values `Inf` and `NaN`. For whi
```julia; hold=true; echo=false;
choices = ["`Inf < 3.0` and `3.0 <= Inf`",
"`NaN < 3.0` and `3.0 <= NaN`"]
ans = 2
radioq(choices, ans)
answ = 2
radioq(choices, answ)
```
###### Question
@@ -542,8 +542,8 @@ choices = ["""
In the manual we can read that "In the expression `a || b`, the subexpression `b` is only evaluated if `a` evaluates to false." In this case `a` is `true` and so `a` is returned.
""",
"Since the second value is \"`missing`\", only the first is used. So `false || missing` would also be `false`"]
ans = 1
radioq(choices, ans)
answ = 1
radioq(choices, answ)
```
The value for `true && missing` is `missing`, not a boolean value. What happens?
@@ -553,6 +553,6 @@ choices = ["""
In the manual we can read that "In the expression `a && b`, the subexpression `b` is only evaluated if `a` evaluates to true." In this case, `a` is `false` so `b` is evaluated and returned. As `b` is just `missing` that is the return value.
""",
"Since the second value is \"`missing`\" all such answers would be missing."]
ans = 1
radioq(choices, ans)
answ = 1
radioq(choices, answ)
```

View File

@@ -57,13 +57,11 @@ types through an easy to understand syntax:
* rationals are constructed from integers using the double division operator, `//`; and
* complex numbers are formed by including a term with the imaginary unit, `im`.
```julia; echo=false;
alert("""
Heads up, the difference between `1` and `1.0` is subtle.
Even more so, as `1.` will parse as `1.0`.
This means some expressions, such as `2.*3`, are ambigous, as the `.` might be part of the `2` (as in `2. * 3`) or the operation `*` (as in `2 .* 3`).
""")
```
!!! warngin
Heads up, the difference between `1` and `1.0` is subtle.
Even more so, as `1.` will parse as `1.0`.
This means some expressions, such as `2.*3`, are ambigous, as the `.` might be part of the `2` (as in `2. * 3`) or the operation `*` (as in `2 .* 3`).
Similarly, each type is printed slightly differently.
@@ -197,22 +195,13 @@ as $a\cdot 2^b$ where $a$ is the *significand* and $b$ is the
As per IEEE Standard 754, the `Float64` type gives 52 bits to the precision (with an additional implied one), 11 bits to the exponent and the other bit is used to represent the sign. Positive, finite, floating point numbers have a range approximately between $10^{-308}$ and $10^{308}$, as 308 is about $\log_{10}\cdot 2^{1023}$. The numbers are not evenly spread out over this range, but, rather, are much more concentrated closer to $0$.
```julia; echo=false;
alert("""
You can discover more about the range of floating point values provided by calling a few different functions.
- `typemax(0.0)` gives the largest value for the type (`Inf` in this case).
- `prevfloat(Inf)` gives the largest finite one, in general `prevfloat` is the next smallest floating point value.
- `nextfloat(-Inf)`, similarly, gives the smallest finite floating point value, and in general returns the next largest floating point value.
- `nextfloat(0.0)` gives the closest positive value to 0.
- `eps()` gives the distance to the next floating point number bigger than `1.0`. This is sometimes referred to as machine precision.
""", title="More on floating point", label="More on the range of floating point values")
```
!!! warning "More on floating point numbers"
You can discover more about the range of floating point values provided by calling a few different functions.
* `typemax(0.0)` gives the largest value for the type (`Inf` in this case).
* `prevfloat(Inf)` gives the largest finite one, in general `prevfloat` is the next smallest floating point value.
* `nextfloat(-Inf)`, similarly, gives the smallest finite floating point value, and in general returns the next largest floating point value.
* `nextfloat(0.0)` gives the closest positive value to 0.
* `eps()` gives the distance to the next floating point number bigger than `1.0`. This is sometimes referred to as machine precision.
#### Scientific notation
@@ -421,11 +410,9 @@ discr = b^2 - 4a*c
When learning calculus, the only common usage of complex numbers arises when solving polynomial equations for roots, or zeros, though they are very important for subsequent work using the concepts of calculus.
```julia;echo=false
note("""
Though complex numbers are stored as pairs of numbers, the imaginary unit, `im`, is of type `Complex{Bool}`, a type that can be promoted to more specific types when `im` is used with different number types.
""")
```
!!! note
Though complex numbers are stored as pairs of numbers, the imaginary unit, `im`, is of type `Complex{Bool}`, a type that can be promoted to more specific types when `im` is used with different number types.
## Type stability
@@ -560,8 +547,8 @@ The number created by `pi/2` is?
```julia; hold=true; echo=false;
ans = 3
radioq(choices, ans, keep_order=true)
answ = 3
radioq(choices, answ, keep_order=true)
```
###### Question
@@ -569,8 +556,8 @@ radioq(choices, ans, keep_order=true)
The number created by `2/2` is?
```julia; hold=true; echo=false;
ans = 3
radioq(choices, ans, keep_order=true)
answ = 3
radioq(choices, answ, keep_order=true)
```
###### Question
@@ -578,8 +565,8 @@ radioq(choices, ans, keep_order=true)
The number created by `2//2` is?
```julia; hold=true; echo=false;
ans = 2
radioq(choices, ans, keep_order=true)
answ = 2
radioq(choices, answ, keep_order=true)
```
###### Question
@@ -587,8 +574,8 @@ radioq(choices, ans, keep_order=true)
The number created by `1 + 1//2 + 1/3` is?
```julia; hold=true; echo=false;
ans = 3
radioq(choices, ans, keep_order=true)
answ = 3
radioq(choices, answ, keep_order=true)
```
###### Question
@@ -597,8 +584,8 @@ radioq(choices, ans, keep_order=true)
The number created by `2^3` is?
```julia; hold=true; echo=false;
ans = 1
radioq(choices, ans, keep_order=true)
answ = 1
radioq(choices, answ, keep_order=true)
```
###### Question
@@ -607,8 +594,8 @@ radioq(choices, ans, keep_order=true)
The number created by `sqrt(im)` is?
```julia; hold=true; echo=false;
ans = 4
radioq(choices, ans, keep_order=true)
answ = 4
radioq(choices, answ, keep_order=true)
```
###### Question
@@ -617,8 +604,8 @@ radioq(choices, ans, keep_order=true)
The number created by `2^(-1)` is?
```julia; hold=true; echo=false;
ans = 3
radioq(choices, ans, keep_order=true)
answ = 3
radioq(choices, answ, keep_order=true)
```
###### Question
@@ -628,8 +615,8 @@ radioq(choices, ans, keep_order=true)
The "number" created by `1/0` is?
```julia; hold=true; echo=false;
ans = 3
radioq(choices, ans, keep_order=true)
answ = 3
radioq(choices, answ, keep_order=true)
```
###### Question

View File

@@ -75,10 +75,8 @@ using Plots
```
```julia;echo=false
note("""
`Plots` is a frontend for one of several backends. `Plots` comes with a backend for web-based graphics (call `plotly()` to specify that); a backend for static graphs (call `gr()` for that). If the `PyPlot` package is installed, calling `pyplot()` will set that as a backend. For terminal usage, if the `UnicodePlots` package is installed, calling `unicodeplots()` will enable that usage. There are still other backends.""")
```
!!! note
`Plots` is a frontend for one of several backends. `Plots` comes with a backend for web-based graphics (call `plotly()` to specify that); a backend for static graphs (call `gr()` for that). If the `PyPlot` package is installed, calling `pyplot()` will set that as a backend. For terminal usage, if the `UnicodePlots` package is installed, calling `unicodeplots()` will enable that usage. There are still other backends.
The `plotly` backend is part of the `Plots` package, as is `gr`. Other backends require installation, such as `PyPlot` and `PlotlyJS`.
We use `gr` in these notes, for the most part. (The `plotly` backend is also quite nice for interactive usage, but doesn't work as well with the static HTML pages.)
@@ -112,11 +110,9 @@ Plotting a function is then this simple: `plot(f, xmin, xmax)`.
> style, where the details to execute the action are only exposed as
> needed.
```julia; echo=false
note("""
The time to first plot can feel sluggish, but subsequent plots will be speedy. See the technical note at the end of this section for an explanation.
""")
```
!!! note
The time to first plot can feel sluggish, but subsequent plots will be speedy. See the technical note at the end of this section for an explanation.
Let's see some other graphs.
@@ -165,16 +161,12 @@ plot(x -> mxplusb(x, (m=-1, b=1)), -1, 2)
```
```julia;echo=false
note("""
!!! note
The function object in the general pattern `action(function, args...)`
is commonly specified in one of three ways: by a name, as with `f`; as an
anonymous function; or as the return value of some other action
through composition.
The function object in the general pattern `action(function, args...)`
is commonly specified in one of three ways: by a name, as with `f`; as an
anonymous function; or as the return value of some other action
through composition.
""")
```
Anonymous functions are also created by `Julia's` `do` notation, which is useful when the first argument to function (like `plot`) accepts a function:
@@ -186,11 +178,9 @@ end
The `do` notation can be a bit confusing to read when unfamiliar, though its convenience makes it appealing.
```julia; echo=false
note("""
Some types we will encounter, such as the one for symbolic values or the special polynomial one, have their own `plot` recipes that allow them to be plotted similarly as above, even though they are not functions.
""")
```
!!! note
Some types we will encounter, such as the one for symbolic values or the special polynomial one, have their own `plot` recipes that allow them to be plotted similarly as above, even though they are not functions.
----
@@ -248,22 +238,6 @@ pts_needed(x -> 10x, 0, 10), pts_needed(x -> sin(10x), 0, 10)
(In fact, the `21` is the minimum number of points used for any function; a linear function only needs two.)
##### Example
This demo (which is interactive within a `Pluto` session) shows more points are needed as the function becomes more "curvy." There are the minimum of ``21`` for a straight line, ``37`` for a half period, ``45`` for a full period, etc.
```julia; echo=false
md"""
n = $(@bind 𝐧 Slider(0:20, default=1))
"""
```
```julia; hold=true;
xs,ys = unzip(x -> sin(𝐧*x*pi), 0, 1)
plot(xs, ys, title="n=$(length(xs))")
scatter!(xs, ys)
```
----
For instances where a *specific* set of ``x`` values is desired to be
@@ -495,16 +469,12 @@ The ``3`` main functions used in these notes for adding layers are:
* `annotate!((x,y, label))` to add a label at $(x,y)$
```julia;echo=false
alert("""
!!! warning
Julia has a convention to use functions named with a `!` suffix to
indicate that they mutate some object. In this case, the object is the
current graph, though it is implicit. Both `plot!`, `scatter!`, and
`annotate!` (others too) do this by adding a layer.
Julia has a convention to use functions named with a `!` suffix to
indicate that they mutate some object. In this case, the object is the
current graph, though it is implicit. Both `plot!`, `scatter!`, and
`annotate!` (others too) do this by adding a layer.
""")
```
## Additional arguments
@@ -709,8 +679,8 @@ choices = ["`(-Inf, -1)` and `(0,1)`",
"`(-Inf, -0.577)` and `(0.577, Inf)`",
"`(-1, 0)` and `(1, Inf)`"
];
ans=3;
radioq(choices, ans)
answ=3;
radioq(choices, answ)
```
@@ -737,8 +707,8 @@ choices = ["`(-Inf, -3)` and `(0, 1)`",
"`(-3, 0)` and `(1, Inf)`",
"`(-Inf, -4.1)` and `(1.455, Inf)`"
];
ans=2;
radioq(choices, ans)
answ=2;
radioq(choices, answ)
```
###### Question
@@ -787,15 +757,15 @@ choices = [
"`f(x) = x <= 4 ? 35.0 : 35.0 + 10.0 * (x-4)`",
"`f(x) = x <= 10 ? 35.0 : 35.0 + 4.0 * (x-10)`"
]
ans = 3
radioq(choices, ans)
answ = 3
radioq(choices, answ)
```
Make a plot of the model. Graphically estimate how many bags of trash will cost 55 dollars.
```julia; hold=true;echo=false
ans = 15
numericq(ans, .5)
answ = 15
numericq(answ, .5)
```
###### Question
@@ -829,8 +799,8 @@ What is seen?
choices = [L"It oscillates wildly, as the period is $T=2\pi/(500 \pi)$ so there are 250 oscillations.",
"It should oscillate evenly, but instead doesn't oscillate very much near 0 and 1",
L"Oddly, it looks exactly like the graph of $f(x) = \sin(2\pi x)$."]
ans = 3
radioq(choices, ans)
answ = 3
radioq(choices, answ)
```
The algorithm to plot a function works to avoid aliasing issues. Does the graph generated by `plot(f, 0, 1)` look the same, as the one above?
@@ -840,8 +810,8 @@ choices = ["Yes",
"No, but is still looks pretty bad, as fitting 250 periods into a too small number of pixels is a problem.",
"No, the graph shows clearly all 250 periods."
]
ans = 2
radioq(choices, ans)
answ = 2
radioq(choices, answ)
```
@@ -865,8 +835,8 @@ choices = [
"An ellipse",
"A straight line"
]
ans = 2
radioq(choices, ans)
answ = 2
radioq(choices, answ)
```
@@ -898,8 +868,8 @@ choices = [
"A straight line",
"None of the above"
]
ans = 1
radioq(choices, ans, keep_order=true)
answ = 1
radioq(choices, answ, keep_order=true)
```
@@ -917,8 +887,8 @@ choices = [
"A straight line",
"None of the above"
]
ans = 3
radioq(choices, ans,keep_order=true)
answ = 3
radioq(choices, answ,keep_order=true)
```
@@ -936,8 +906,8 @@ choices = [
"A straight line",
"None of the above"
]
ans = 2
radioq(choices, ans, keep_order=true)
answ = 2
radioq(choices, answ, keep_order=true)
```
@@ -956,8 +926,8 @@ choices = [
"A straight line",
"None of the above"
]
ans = 5
radioq(choices, ans, keep_order=true)
answ = 5
radioq(choices, answ, keep_order=true)
```
----

View File

@@ -2,6 +2,7 @@
In this section we use the following add-on packages:
```julia
using SymPy
using Plots
@@ -11,6 +12,8 @@ using Plots
using CalculusWithJulia
using CalculusWithJulia.WeaveSupport
fig_size = (800, 600) #400, 300)
const frontmatter = (
title = "Polynomials",
description = "Calculus with Julia: Polynomials",
@@ -19,6 +22,7 @@ const frontmatter = (
nothing
```
----
Polynomials are a particular class of expressions that are simple
@@ -57,7 +61,7 @@ a_n x^n + a_{n-1}x^{n-1} + \cdots a_1 x + a_0, \quad a_n \neq 0
```julia; hold=true; echo=false; cache=true
##{{{ different_poly_graph }}}
fig_size = (400, 300)
anim = @animate for m in 2:2:10
fn = x -> x^m
plot(fn, -1, 1, size = fig_size, legend=false, title="graph of x^{$m}", xlims=(-1,1), ylims=(-.1,1))
@@ -109,11 +113,9 @@ of $m$ can be found from two points through the well-known formula:
m = \frac{y_1 - y_0}{x_1 - x_0} = \frac{\text{rise}}{\text{run}}
```
```julia; hold=true, echo=false; cache=true
```julia; hold=true; echo=false; cache=true
### {{{ lines_m_graph }}}
fig_size = (400, 300)
anim = @animate for m in [-5, -2, -1, 1, 2, 5, 10, 20]
fn = x -> m * x
plot(fn, -1, 1, size = fig_size, legend=false, title="m = $m", xlims=(-1,1), ylims=(-20, 20))
@@ -177,21 +179,13 @@ Python session. That is great for `Julia` users, as the `PyCall` and
manner. This allows the `Julia` package `SymPy` to provide
functionality from SymPy within `Julia`.
```julia; echo=false
note("""
!!! note
When `SymPy` is installed through the package manger, the underlying `Python`
libraries will also be installed.
When `SymPy` is installed through the package manger, the underlying `Python`
libraries will also be installed.
""")
```
```julia; echo=false
note("""
The [`Symbolics`](../alternatives/symbolics) package is a rapidly
developing `Julia`-only packge that provides symbolic math options.
""")
```
!!! note
The [`Symbolics`](../alternatives/symbolics) package is a rapidly
developing `Julia`-only packge that provides symbolic math options.
----
@@ -212,10 +206,8 @@ that can be made. The `@syms` macro documentation lists them. The
symbols. The *macro* `@syms` does not need assignment, as the
variable(s) are created behind the scenes by the macro.
```julia;echo=false
note("""Macros in `Julia` are just transformations of the syntax into other syntax. The `@` indicates they behave differently than regular function calls.
""")
```
!!! note
Macros in `Julia` are just transformations of the syntax into other syntax. The `@` indicates they behave differently than regular function calls.
The `SymPy` package does three basic things:
@@ -440,8 +432,6 @@ larger values of $n$ have greater growth once outside of $[-1,1]$:
```julia; hold=true; echo=false; cache=true
### {{{ poly_growth_graph }}}
fig_size = (400, 300)
anim = @animate for m in 0:2:12
fn = x -> x^m
plot(fn, -1.2, 1.2, size = fig_size, legend=false, xlims=(-1.2, 1.2), ylims=(0, 1.2^12), title="x^{$m} over [-1.2, 1.2]")
@@ -482,8 +472,6 @@ of the plot window until the graph appears U-shaped.
```julia;hold=true; echo=false; cache=true
### {{{ leading_term_graph }}}
fig_size = (400, 300)
anim = @animate for n in 1:6
m = [1, .5, -1, -5, -20, -25]
M = [2, 4, 5, 10, 25, 30]
@@ -728,8 +716,8 @@ What is the leading term of $p$?
```julia; hold=true; echo=false
choices = ["``3``", "``3x^2``", "``-2x``", "``5``"]
ans = 2
radioq(choices, ans)
answ = 2
radioq(choices, answ)
```
@@ -761,8 +749,8 @@ The linear polynomial $p = 2x + 3$ is written in which form:
```julia; hold=true; echo=false
choices = ["point-slope form", "slope-intercept form", "general form"]
ans = 2
radioq(choices, ans)
answ = 2
radioq(choices, answ)
```
@@ -781,8 +769,8 @@ What command will return the value of the polynomial when $x=2$?
```julia; hold=true; echo=false
choices = [q"p*2", q"p[2]", q"p_2", q"p(x=>2)"]
ans = 4
radioq(choices, ans)
answ = 4
radioq(choices, answ)
```
@@ -796,8 +784,8 @@ L"Be $U$-shaped, opening upward",
L"Be $U$-shaped, opening downward",
L"Overall, go upwards from $-\infty$ to $+\infty$",
L"Overall, go downwards from $+\infty$ to $-\infty$"]
ans = 3
radioq(choices, ans, keep_order=true)
answ = 3
radioq(choices, answ, keep_order=true)
```
###### Question
@@ -810,8 +798,8 @@ L"Be $U$-shaped, opening upward",
L"Be $U$-shaped, opening downward",
L"Overall, go upwards from $-\infty$ to $+\infty$",
L"Overall, go downwards from $+\infty$ to $-\infty$"]
ans = 1
radioq(choices, ans, keep_order=true)
answ = 1
radioq(choices, answ, keep_order=true)
```
###### Question
@@ -824,8 +812,8 @@ L"Be $U$-shaped, opening upward",
L"Be $U$-shaped, opening downward",
L"Overall, go upwards from $-\infty$ to $+\infty$",
L"Overall, go downwards from $+\infty$ to $-\infty$"]
ans = 2
radioq(choices, ans, keep_order=true)
answ = 2
radioq(choices, answ, keep_order=true)
```
###### Question
@@ -860,7 +848,7 @@ choices = [q"x^3 - 3x^2 + 2x",
q"x^3 - x^2 - 2x",
q"x^3 + x^2 - 2x",
q"x^3 + x^2 + 2x"]
ans = 2
answ = 2
radioq(choices, 2)
```
@@ -874,6 +862,6 @@ q"-h^2 + 3hx - 3x^2",
q"h^3 + 3h^2x + 3hx^2 + x^3 -x^3/h",
q"x^3 - x^3/h",
q"0"]
ans = 1
radioq(choices, ans)
answ = 1
radioq(choices, answ)
```

View File

@@ -2,7 +2,8 @@
In this section we use the following add on packages:
```juila
```julia
using CalculusWithJulia
using Plots
using SymPy
@@ -191,11 +192,9 @@ quotient, remainder = divrem(x^4 + 2x^2 + 5, x - 2)
The answer is a tuple containing the quotient and remainder. The quotient itself could be found with `div` or `÷` and the remainder with `rem`.
```julia; echo=false
note("""
For those who have worked with SymPy within Python, `divrem` is the `div` method renamed, as `Julia`'s `div` method has the generic meaning of returning the quotient.
""")
```
!!! note
For those who have worked with SymPy within Python, `divrem` is the `div` method renamed, as `Julia`'s `div` method has the generic meaning of returning the quotient.
As well, the `apart` function could be used for this task. This function
@@ -286,12 +285,11 @@ multiplicity must be accounted for and $x^2 + 1$ to see why complex
values may be necessary.)
```julia; echo=false
alert(raw"""
The special case of the ``0`` polynomial having no degree defined
eliminates needing to exclude it, as it has infinitely many roots. Otherwise, the language would be qualified to have ``n \geq 0``.
""")
```
!!! warning
The special case of the ``0`` polynomial having no degree defined
eliminates needing to exclude it, as it has infinitely many roots.
Otherwise, the language would be qualified to have ``n \geq 0``.
## Finding roots of a polynomial
@@ -458,11 +456,9 @@ q = sympy.Poly(p, x) # identify `x` as indeterminate; alternatively p.as_poly(x
roots(q)
```
```julia; echo=false
note("""
The sympy `Poly` function must be found within the underlying `sympy` module, a Python object, hence is qualified as `sympy.Poly`. This is common when using `SymPy`, as only a small handful of the many functions available are turned into `Julia` functions, the rest are used as would be done in Python. (This is similar, but different than qualifying by a `Julia` module when there are two conflicting names. An example will be the use of the name `roots` in both `SymPy` and `Polynomials` to refer to a function that finds the roots of a polynomial. If both functions were loaded, then the last line in the above example would need to be `SymPy.roots(q)` (note the capitalization.)
""")
```
!!! note
The sympy `Poly` function must be found within the underlying `sympy` module, a Python object, hence is qualified as `sympy.Poly`. This is common when using `SymPy`, as only a small handful of the many functions available are turned into `Julia` functions, the rest are used as would be done in Python. (This is similar, but different than qualifying by a `Julia` module when there are two conflicting names. An example will be the use of the name `roots` in both `SymPy` and `Polynomials` to refer to a function that finds the roots of a polynomial. If both functions were loaded, then the last line in the above example would need to be `SymPy.roots(q)` (note the capitalization.)
### Numerically finding roots
The `solve` function can be used to get numeric approximations to the
@@ -583,11 +579,9 @@ in fact there are three, two are *very* close together:
N.(solve(h))
```
```julia; echo=false
note("""
The difference of the two roots is around `1e-10`. For the graph over the interval of ``[-5,7]`` there are about ``800`` "pixels" used, so each pixel represents a size of about `1.5e-2`. So the cluster of roots would safely be hidden under a single "pixel."
""")
```
!!! note
The difference of the two roots is around `1e-10`. For the graph over the interval of ``[-5,7]`` there are about ``800`` "pixels" used, so each pixel represents a size of about `1.5e-2`. So the cluster of roots would safely be hidden under a single "pixel."
The point of this is to say, that it is useful to know where to look
for roots, even if graphing calculators or graphing programs make
@@ -713,8 +707,8 @@ choices = [
"``6``",
"``0``"
]
ans = 3
radioq(choices, ans)
answ = 3
radioq(choices, answ)
```
@@ -728,8 +722,8 @@ choices = [
"``x^2 - 2x + 2``",
"``2``"
]
ans = 2
radioq(choices, ans)
answ = 2
radioq(choices, answ)
```
###### Question
@@ -744,8 +738,8 @@ choices = [
"``x^3 + x^2 - 1``",
"``-2x + 2``"
]
ans = 3
radioq(choices, ans)
answ = 3
radioq(choices, answ)
```
###### Question
@@ -770,8 +764,8 @@ choices = [
"``x^5 + 2x^4 + 4x^3 + 8x^2 + 15x + 31``",
"``x^4 +2x^3 + 4x^2 + 8x + 15``",
"``31``"]
ans = 1
radioq(choices, ans)
answ = 1
radioq(choices, answ)
```
@@ -784,8 +778,8 @@ choices = [
"``x^5 + 2x^4 + 4x^3 + 8x^2 + 15x + 31``",
"``x^4 +2x^3 + 4x^2 + 8x + 15``",
"``31``"]
ans = 4
radioq(choices, ans)
answ = 4
radioq(choices, answ)
```
What is $r$?
@@ -797,8 +791,8 @@ choices = [
"``x^5 + 2x^4 + 4x^3 + 8x^2 + 15x + 31``",
"``x^4 +2x^3 + 4x^2 + 8x + 15``",
"``31``"]
ans = 5
radioq(choices, ans)
answ = 5
radioq(choices, answ)
```
@@ -813,8 +807,8 @@ choices = [
L" $2$ and $3$",
L" $(x-2)$ and $(x-3)$",
L" $(x+2)$ and $(x+3)$"]
ans = 2
radioq(choices, ans)
answ = 2
radioq(choices, answ)
```
@@ -860,8 +854,8 @@ q"[-0.434235, -0.434235, 0.188049, 0.188049, 0.578696, 4.91368]",
q"[-0.434235, -0.434235, 0.188049, 0.188049]",
q"[0.578696, 4.91368]",
q"[-0.434235+0.613836im, -0.434235-0.613836im]"]
ans = 3
radioq(choices, ans)
answ = 3
radioq(choices, answ)
```
@@ -916,8 +910,8 @@ numericq(1)
Let $f(x) = x^5 - 4x^4 + x^3 - 2x^2 + x$. What does Cauchy's bound say is the largest possible magnitude of a root?
```julia; hold=true; echo=false
ans = 1 + 4 + 1 + 2 + 1
numericq(ans)
answ = 1 + 4 + 1 + 2 + 1
numericq(answ)
```
What is the largest magnitude of a real root?
@@ -925,8 +919,8 @@ What is the largest magnitude of a real root?
```julia; hold=true; echo=false
f(x) = x^5 - 4x^4 + x^3 - 2x^2 + x
rts = find_zeros(f, -5..5)
ans = maximum(abs.(rts))
numericq(ans)
answ = maximum(abs.(rts))
numericq(answ)
```
@@ -970,8 +964,8 @@ choices = [
"``2x^2``",
"``x``",
"``2x``"]
ans = 1
radioq(choices, ans)
answ = 1
radioq(choices, answ)
```
* True or false, the $degree$ of $T_n(x)$ is $n$: (Look at the defining relation and reason this out).
@@ -992,8 +986,8 @@ The Chebyshev polynomials have the property that in fact all $n$ roots are real,
@syms x
p = 16x^5 - 20x^3 + 5x
rts = N.(solve(p))
ans = maximum(norm.(rts))
numericq(ans)
answ = maximum(norm.(rts))
numericq(answ)
```
* Plotting `p` over the interval $[-2,2]$ does not help graphically identify the roots:

View File

@@ -187,11 +187,8 @@ observed from the output. The lone real root is approximately
irrational root.
```julia;echo=false
note("""
`SymPy` also has a `roots` function. If both `Polynomials` and `SymPy` are used together, calling `roots` must be qualified, as with `Polynomials.roots(...)`. Similarly, `degree` is provided in both, so it too must be qualified.
""")
```
!!! note
`SymPy` also has a `roots` function. If both `Polynomials` and `SymPy` are used together, calling `roots` must be qualified, as with `Polynomials.roots(...)`. Similarly, `degree` is provided in both, so it too must be qualified.
The `roots` function numerically identifies roots. As such, it is susceptible to floating point issues. For example, the following polynomial has one root with multiplicity ``5``, but ``5`` distinct roots are numerically identified:
@@ -352,8 +349,8 @@ Mathematically we say the ``0`` polynomial has no degree. What convention does `
```julia; hold=true; echo=false;
choices = ["`nothing`", "`-1`", "`0`", "`Inf`", "`-Inf`"]
ans = 2
radioq(choices, ans, keep_order=true)
answ = 2
radioq(choices, answ, keep_order=true)
```
###### Question
@@ -511,10 +508,7 @@ It is ``0\cdot T_1(x) + 1\cdot T_1(x) + 2\cdot T_2(x) + 3\cdot T_3(x) = -2 - 8\c
radioq(choices, 3)
```
```julia; hold=true; echo=false
note("""
The `Polynomials` package has an implementation, so you can check your answer through `convert(Polynomial, ChebyshevT([0,1,2,3]))`. Similarly, the `SpecialPolynomials` package has these and many other polynomial bases represented.
!!! note
The `Polynomials` package has an implementation, so you can check your answer through `convert(Polynomial, ChebyshevT([0,1,2,3]))`. Similarly, the `SpecialPolynomials` package has these and many other polynomial bases represented.
The `ApproxFun` package is built on top of polynomials expressed in this basis, as the Chebyshev polynomials have special properties which make them very suitable when approximating functions with polynomials. The `ApproxFun` package uses easier-to-manipulate polynomials to approximate functions very accurately, thereby being useful for investigating properties of non-linear functions leveraging properties for polynomials.
""")
```
The `ApproxFun` package is built on top of polynomials expressed in this basis, as the Chebyshev polynomials have special properties which make them very suitable when approximating functions with polynomials. The `ApproxFun` package uses easier-to-manipulate polynomials to approximate functions very accurately, thereby being useful for investigating properties of non-linear functions leveraging properties for polynomials.

View File

@@ -4,7 +4,7 @@
```julia; echo=false; results="hidden"
using CalculusWithJulia
using CalculusWithJulia.WeaveSupport
const frontmatter = (
frontmatter = (
title = "Ranges and Sets",
description = "Calculus with Julia: Ranges and Sets",
tags = ["CalculusWithJulia", "precalc", "ranges and sets"],
@@ -155,7 +155,7 @@ collected to realize the values.
The number of points is specified with keyword arguments, as in:
```julia;
xs = range(-1, 1, length=9) # or simply range(-1, 1, 9)
xs = range(-1, 1, length=9) # or simply range(-1, 1, 9) as of v"1.7"
```
and
@@ -164,11 +164,9 @@ and
collect(xs)
```
```julia;echo=false
note("""
There is also the `LinRange(a, b, n)` function which can be more performant than `range`, as it doesn't try to correct for floating point errors.
""")
```
!!! note
There is also the `LinRange(a, b, n)` function which can be more performant than `range`, as it doesn't try to correct for floating point errors.
## Modifying sequences
@@ -466,8 +464,8 @@ q"1:99",
q"1:3:99",
q"1:2:99"
]
ans = 3
radioq(choices, ans)
answ = 3
radioq(choices, answ)
```
@@ -477,8 +475,8 @@ Which of these will create the sequence $2, 9, 16, 23, \dots, 72$?
```julia; hold=true;echo=false;
choices = [q"2:7:72", q"2:9:72", q"2:72", q"72:-7:2"]
ans = 1
radioq(choices, ans)
answ = 1
radioq(choices, answ)
```
@@ -502,8 +500,8 @@ choices = [
"`1:-1:10`",
"`1:10`"
]
ans = 1
radioq(choices, ans)
answ = 1
radioq(choices, answ)
```
###### Question
@@ -538,8 +536,8 @@ choices = ["It is just random",
"Addition happens prior to the use of `:` so this is like `1:(4+2):5`",
"It gives the correct answer, a generator for the vector `[3,5,7,9]`"
]
ans = 2
radioq(choices, ans)
answ = 2
radioq(choices, answ)
```
###### Question
@@ -548,8 +546,8 @@ How is `a:b-1` interpreted:
```julia; hold=true;echo=false;
choices = ["as `a:(b-1)`", "as `(a:b) - 1`, which is `(a-1):(b-1)`"]
ans = 1
radioq(choices, ans)
answ = 1
radioq(choices, answ)
```
###### Question
@@ -558,8 +556,8 @@ Create the sequence $10, 100, 1000, \dots, 1,000,000$ using a list comprehension
```julia; hold=true;echo=false;
choices = [q"[10^i for i in 1:6]", q"[10^i for i in [10, 100, 1000]]", q"[i^10 for i in [1:6]]"]
ans = 1
radioq(choices, ans)
answ = 1
radioq(choices, answ)
```
###### Question
@@ -571,8 +569,8 @@ choices = [
q"[10^-i for i in 1:7]",
q"[(1/10)^i for i in 1:7]",
q"[i^(1/10) for i in 1:7]"]
ans = 2
radioq(choices, ans)
answ = 2
radioq(choices, answ)
```
###### Question
@@ -581,8 +579,8 @@ Evaluate the expression $x^3 - 2x + 3$ for each of the values $-5, -4, \dots, 4,
```julia; hold=true;echo=false;
choices = [q"[x^3 - 2x + 3 for i in -5:5]", q"[x^3 - 2x + 3 for x in -(5:5)]", q"[x^3 - 2x + 3 for x in -5:5]"]
ans = 3
radioq(choices, ans)
answ = 3
radioq(choices, answ)
```
###### Question

View File

@@ -260,7 +260,7 @@ saying it follows the shape of the leading term of $q(x)$, at the
expense of the work required to find $q(x)$.
##### Examples
### Examples
Consider the rational expression
@@ -853,8 +853,8 @@ The rational expression $(x^3 - 2x + 3) / (x^2 - x + 1)$ would have
choices = [L"A horizontal asymptote $y=0$",
L"A horizontal asymptote $y=1$",
L"A slant asymptote with slope $m=1$"]
ans = 3
radioq(choices, ans)
answ = 3
radioq(choices, answ)
```
###### Question
@@ -866,8 +866,8 @@ The rational expression $(x^2 - x + 1)/ (x^3 - 2x + 3)$ would have
choices = [L"A horizontal asymptote $y=0$",
L"A horizontal asymptote $y=1$",
L"A slant asymptote with slope $m=1$"]
ans = 1
radioq(choices, ans)
answ = 1
radioq(choices, answ)
```
@@ -881,8 +881,8 @@ The rational expression $(x^2 - x + 1)/ (x^2 - 3x + 3)$ would have
choices = [L"A horizontal asymptote $y=0$",
L"A horizontal asymptote $y=1$",
L"A slant asymptote with slope $m=1$"]
ans = 2
radioq(choices, ans)
answ = 2
radioq(choices, answ)
```
@@ -902,8 +902,8 @@ would have
choices = [L"A horizontal asymptote $y=0$",
L"A horizontal asymptote $y=1$",
L"A slant asymptote with slope $m=1$"]
ans = 2
radioq(choices, ans)
answ = 2
radioq(choices, answ)
```
@@ -923,8 +923,8 @@ choices = [L"A vertical asymptote $x=1$",
L"A slant asymptote with slope $m=1$",
L"A vertical asymptote $x=5$"
]
ans = 3
radioq(choices, ans)
answ = 3
radioq(choices, answ)
```
@@ -946,8 +946,8 @@ choices = [
"``y = (1/3)x``",
"``y = (1/3)x - (1/3)``"
]
ans = 3
radioq(choices, ans)
answ = 3
radioq(choices, answ)
```
@@ -971,8 +971,8 @@ Is the following common conception true: "The graph of a function never crosses
```julia; hold=true; echo=false
choices = ["No, the graph clearly crosses the drawn asymptote",
"Yes, this is true"]
ans = 1
radioq(choices, ans)
answ = 1
radioq(choices, answ)
```
(The wikipedia page indicates that the term "asymptote" was introduced
@@ -1002,8 +1002,8 @@ choices = ["The horizontal asymptote is not a straight line.",
L"The $y$-axis scale shows that indeed the $y$ values are getting close to $0$.",
L"The graph is always decreasing, hence it will eventually reach $-\infty$."
]
ans = 2
radioq(choices, ans)
answ = 2
radioq(choices, answ)
```
@@ -1040,8 +1040,8 @@ choices = ["between ``0`` and ``8`` hours",
"between ``8`` and ``16`` hours",
"between ``16`` and ``24`` hours",
"after one day"]
ans = 1
radioq(choices, ans)
answ = 1
radioq(choices, answ)
```
This graph has
@@ -1051,8 +1051,8 @@ choices = [L"a slant asymptote with slope $50$",
L"a horizontal asymptote $y=20$",
L"a horizontal asymptote $y=0$",
L"a vertical asymptote with $x = 20^{1/3}$"]
ans = 3
radioq(choices, ans)
answ = 3
radioq(choices, answ)
```
@@ -1073,6 +1073,6 @@ L"The $\sin(x)$ oscillates, but the rational function eventually follows $7/60 \
L"The $\sin(x)$ oscillates, but the rational function has a slant asymptote",
L"The $\sin(x)$ oscillates, but the rational function has a non-zero horizontal asymptote",
L"The $\sin(x)$ oscillates, but the rational function has a horizontal asymptote of $0$"]
ans = 2
radioq(choices, ans)
answ = 2
radioq(choices, answ)
```

View File

@@ -114,17 +114,13 @@ plot!(gf, label="g∘f")
```
```julia;echo=false
note("""
!!! note
Unlike how the basic arithmetic operations are treated, `Julia` defines the infix
Unicode operator `\\circ[tab]` to represent composition of functions,
mirroring mathematical notation. This infix operations takes in two functions and returns an anonymous function. It
can be useful and will mirror standard mathematical usage up to issues
with precedence rules.
Unlike how the basic arithmetic operations are treated, `Julia` defines the infix
Unicode operator `\\circ[tab]` to represent composition of functions,
mirroring mathematical notation. This infix operations takes in two functions and returns an anonymous function. It
can be useful and will mirror standard mathematical usage up to issues
with precedence rules.
""")
```
Starting with two functions and composing them requires nothing more
than a solid grasp of knowing the rules of function evaluation. If
@@ -163,11 +159,9 @@ other compositions could have been given above. For example, the last
function is also $f(x) = e^{-x/2}$ composed with $g(x) = x^2$.
```julia;echo=false
note("""
The real value of composition is to break down more complicated things into a sequence of easier steps. This is good mathematics, but also good practice more generally. For example, when we approach a problem with the computer, we generally use a smallish set of functions and piece them together (that is, compose them) to find a solution.
""")
```
!!! note
The real value of composition is to break down more complicated things into a sequence of easier steps. This is good mathematics, but also good practice more generally. For example, when we approach a problem with the computer, we generally use a smallish set of functions and piece them together (that is, compose them) to find a solution.
### Shifting and scaling graphs
@@ -508,8 +502,8 @@ If $f(x) = 1/x$ and $g(x) = x-2$, what is $g(f(x))$?
```julia; hold=true;echo=false
choices=["``1/(x-2)``", "``1/x - 2``", "``x - 2``", "``-2``"]
ans = 2
radioq(choices, ans)
answ = 2
radioq(choices, answ)
```
###### Question
@@ -519,8 +513,8 @@ If $f(x) = e^{-x}$ and $g(x) = x^2$ and $h(x) = x-3$, what is $f \circ g \circ h
```julia; hold=true;echo=false
choices=["``e^{-x^2 - 3}``", "``(e^x -3)^2``",
"``e^{-(x-3)^2}``", "``e^x+x^2+x-3``"]
ans = 3
radioq(choices, ans)
answ = 3
radioq(choices, answ)
```
###### Question
@@ -530,8 +524,8 @@ If $h(x) = (f \circ g)(x) = \sin^2(x)$ which is a possibility for $f$ and $g$:
choices = [raw"``f(x)=x^2; \quad g(x) = \sin^2(x)``",
raw"```f(x)=x^2; \quad g(x) = \sin(x)``",
raw"``f(x)=\sin(x); \quad g(x) = x^2``"]
ans = 2
radioq(choices, ans)
answ = 2
radioq(choices, answ)
```
@@ -544,7 +538,7 @@ choices = [
raw"``h(x) = 6 + \sin(x + 4)``",
raw"``h(x) = 6 + \sin(x-4)``",
raw"``h(x) = 6\sin(x-4)``"]
ans = 3
answ = 3
radioq(choices, 3)
```
@@ -555,8 +549,8 @@ Let $h(x) = 4x^2$ and $f(x) = x^2$. Which is **not** true:
choices = [L"The graph of $h(x)$ is the graph of $f(x)$ stretched by a factor of ``4``",
L"The graph of $h(x)$ is the graph of $f(x)$ scaled by a factor of ``2``",
L"The graph of $h(x)$ is the graph of $f(x) shifted up by ``4`` units"]
ans = 3
radioq(choices, ans)
answ = 3
radioq(choices, answ)
```
###### Question
@@ -567,8 +561,8 @@ The transformation $h(x) = (1/a) \cdot f((x-b)/a)$ can be viewed in one sequence
choices = [L"scaling by $1/a$, then shifting by $b$, then stretching by $1/a$",
L"shifting by $a$, then scaling by $b$, and then scaling by $1/a$",
L"shifting by $a$, then scaling by $a$, and then scaling by $b$" ]
ans=1
radioq(choices, ans)
answ=1
radioq(choices, answ)
```
###### Question
@@ -603,8 +597,8 @@ raw"``\sin(2x)``",
raw"``\sin(\pi x)``",
raw"``2 \sin(\pi x)``"
]
ans = 4
radioq(choices, ans)
answ = 4
radioq(choices, answ)
```
@@ -626,8 +620,8 @@ choices = [
q"D(S(f))(n) = f(n)",
q"S(D(f))(n) = f(n) - f(0)"
]
ans = 2
radioq(choices, ans, keep_order=true)
answ = 2
radioq(choices, answ, keep_order=true)
```
###### Question
@@ -645,6 +639,6 @@ choices = [
q"D(S(f))(n) = f(n)",
q"S(D(f))(n) = f(n) - f(0)"
]
ans = 1
radioq(choices, ans, keep_order=true)
answ = 1
radioq(choices, answ, keep_order=true)
```

View File

@@ -1,5 +1,6 @@
# Trigonometric functions
This section uses the following add-on packages:
```julia
@@ -11,6 +12,8 @@ using SymPy
```julia; echo=false; results="hidden"
using CalculusWithJulia.WeaveSupport
fig_size = (800, 600)
const frontmatter = (
title = "Trigonometric functions",
description = "Calculus with Julia: Trigonometric functions",
@@ -66,9 +69,8 @@ trigonometric functions are
\end{align*}
```
```julia; echo=false
note("""Many students remember these through [SOH-CAH-TOA](http://mathworld.wolfram.com/SOHCAHTOA.html).""")
```
!!! note
Many students remember these through [SOH-CAH-TOA](http://mathworld.wolfram.com/SOHCAHTOA.html).
Some algebra shows that $\tan(\theta) = \sin(\theta)/\cos(\theta)$. There are also ``3`` reciprocal functions, the cosecant, secant and cotangent.
@@ -78,7 +80,6 @@ These definitions in terms of sides only apply for $0 \leq \theta \leq \pi/2$. M
```julia; hold=true; echo=false; cache=true
## {{{radian_to_trig}}}
fig_size = (400, 300)
function plot_angle(m)
r = m*pi
@@ -181,15 +182,10 @@ sincos(pi/3)
```
```julia; echo=false
note(L"""
!!! note
For really large values, round off error can play a big role. For example, the *exact* value of $\sin(1000000 \pi)$ is $0$, but the returned value is not quite $0$ `sin(1_000_000 * pi) = -2.231912181360871e-10`. For exact multiples of $\pi$ with large multiples the `sinpi` and `cospi` functions are useful.
For really large values, round off error can play a big role. For example, the *exact* value of $\sin(1000000 \pi)$ is $0$, but the returned value is not quite $0$ `sin(1_000_000 * pi) = -2.231912181360871e-10`. For exact multiples of $\pi$ with large multiples the `sinpi` and `cospi` functions are useful.
(Both functions are computed by first employing periodicity to reduce the problem to a smaller angle. However, for large multiples the floating-point roundoff becomes a problem with the usual functions.)
""")
```
(Both functions are computed by first employing periodicity to reduce the problem to a smaller angle. However, for large multiples the floating-point roundoff becomes a problem with the usual functions.)
##### Example
@@ -356,7 +352,6 @@ end
# create animoation
b₁=1/3; n₁=3; b₂=1/4; n₂=4
fig_size = (400, 300)
anim = @animate for t ∈ range(0, 2.5, length=50)
makegraph(t, b₁, n₁, b₂, n₂)
end
@@ -618,45 +613,47 @@ the unit *hyperbola* ($x^2 - y^2 = 1$). We define the hyperbolic
sine ($\sinh$) and hyperbolic cosine ($\cosh$) through $(\cosh(\theta),
\sinh(\theta)) = (x,y)$.
```julia; hold=true; echo=false
## inspired by https://en.wikipedia.org/wiki/Hyperbolic_function
# y^2 = x^2 - 1
top(x) = sqrt(x^2 - 1)
```julia; echo=false
let
## inspired by https://en.wikipedia.org/wiki/Hyperbolic_function
# y^2 = x^2 - 1
top(x) = sqrt(x^2 - 1)
p = plot(; legend=false, aspect_ratio=:equal)
p = plot(; legend=false, aspect_ratio=:equal)
x₀ = 2
xs = range(1, x₀, length=100)
ys = top.(xs)
plot!(p, xs, ys, color=:red)
plot!(p, xs, -ys, color=:red)
x₀ = 2
xs = range(1, x₀, length=100)
ys = top.(xs)
plot!(p, xs, ys, color=:red)
plot!(p, xs, -ys, color=:red)
xs = -reverse(xs)
ys = top.(xs)
plot!(p, xs, ys, color=:red)
plot!(p, xs, -ys, color=:red)
xs = -reverse(xs)
ys = top.(xs)
plot!(p, xs, ys, color=:red)
plot!(p, xs, -ys, color=:red)
xs = range(-x₀, x₀, length=3)
plot!(p, xs, xs, linestyle=:dash, color=:blue)
plot!(p, xs, -xs, linestyle=:dash, color=:blue)
xs = range(-x₀, x₀, length=3)
plot!(p, xs, xs, linestyle=:dash, color=:blue)
plot!(p, xs, -xs, linestyle=:dash, color=:blue)
a = 1.2
plot!(p, [0,cosh(a)], [sinh(a), sinh(a)])
annotate!(p, sinh(a)/2, sinh(a)+0.25,"cosh(a)")
plot!(p, [cosh(a),cosh(a)], [sinh(a), 0])
annotate!(p, sinh(a) + 1, cosh(a)/2,"sinh(a)")
scatter!(p, [cosh(a)], [sinh(a)], markersize=5)
a = 1.2
plot!(p, [0,cosh(a)], [sinh(a), sinh(a)])
annotate!(p, [(sinh(a)/2, sinh(a)+0.25,"cosh(a)")])
plot!(p, [cosh(a),cosh(a)], [sinh(a), 0])
annotate!(p, [(sinh(a) + 1, cosh(a)/2,"sinh(a)")])
scatter!(p, [cosh(a)], [sinh(a)], markersize=5)
ts = range(0, a, length=100)
xs = cosh.(ts)
ys = sinh.(ts)
ts = range(0, a, length=100)
xs = cosh.(ts)
ys = sinh.(ts)
xs = [0, 1, xs..., 0]
ys = [0, 0, ys..., 0]
plot!(p, xs, ys, fillcolor=:red, fill=true, alpha=.3)
xs = [0, 1, xs..., 0]
ys = [0, 0, ys..., 0]
plot!(p, xs, ys, fillcolor=:red, fill=true, alpha=.3)
p
p
end
```
These values are more commonly expressed using the exponential function as:
@@ -682,8 +679,8 @@ What is bigger $\sin(1.23456)$ or $\cos(6.54321)$?
```julia; hold=true; echo=false
a = sin(1.23456) > cos(6.54321)
choices = [raw"``\sin(1.23456)``", raw"``\cos(6.54321)``"]
ans = a ? 1 : 2
radioq(choices, ans, keep_order=true)
answ = a ? 1 : 2
radioq(choices, answ, keep_order=true)
```
###### Question
@@ -694,8 +691,8 @@ Let $x=\pi/4$. What is bigger $\cos(x)$ or $x$?
x = pi/4
a = cos(x) > x
choices = [raw"``\cos(x)``", "``x``"]
ans = a ? 1 : 2
radioq(choices, ans, keep_order=true)
answ = a ? 1 : 2
radioq(choices, answ, keep_order=true)
```
###### Question
@@ -707,8 +704,8 @@ choices = [
raw"``\cos(x) = \sin(x - \pi/2)``",
raw"``\cos(x) = \sin(x + \pi/2)``",
raw"``\cos(x) = \pi/2 \cdot \sin(x)``"]
ans = 2
radioq(choices, ans)
answ = 2
radioq(choices, answ)
```
###### Question
@@ -720,8 +717,8 @@ choices = [
L"The values $k\pi$ for $k$ in $\dots, -2, -1, 0, 1, 2, \dots$",
L"The values $\pi/2 + k\pi$ for $k$ in $\dots, -2, -1, 0, 1, 2, \dots$",
L"The values $2k\pi$ for $k$ in $\dots, -2, -1, 0, 1, 2, \dots$"]
ans = 2
radioq(choices, ans, keep_order=true)
answ = 2
radioq(choices, answ, keep_order=true)
```
###### Question
@@ -768,24 +765,24 @@ The sine function is an *odd* function.
```julia; hold=true; echo=false
choices = ["odd", "even", "neither"]
ans = 1
radioq(choices, ans, keep_order=true)
answ = 1
radioq(choices, answ, keep_order=true)
```
* The hyperbolic cosine is:
```julia; hold=true; echo=false
choices = ["odd", "even", "neither"]
ans = 2
radioq(choices, ans, keep_order=true)
answ = 2
radioq(choices, answ, keep_order=true)
```
* The hyperbolic tangent is:
```julia; hold=true; echo=false
choices = ["odd", "even", "neither"]
ans = 1
radioq(choices, ans, keep_order=true)
answ = 1
radioq(choices, answ, keep_order=true)
```
###### Question

View File

@@ -64,11 +64,9 @@ Variable names can be reused, as here, where we redefine `x`:
x = 2
```
```julia; echo=false
note("""
The `Pluto` interface for `Julia` is idiosyncratic, as variables are *reactive*. This interface allows changes to a variable `x` to propogate to all other cells referring to `x`. Consequently, the variable name can only be assigned *once* per notebook **unless** the name is in some other namespace, which can be arranged by including the assignment inside a function or a `let` block.
""")
```
!!! note
The `Pluto` interface for `Julia` is idiosyncratic, as variables are *reactive*. This interface allows changes to a variable `x` to propogate to all other cells referring to `x`. Consequently, the variable name can only be assigned *once* per notebook **unless** the name is in some other namespace, which can be arranged by including the assignment inside a function or a `let` block.
`Julia` is referred to as a "dynamic language" which means (in most
@@ -95,7 +93,7 @@ bottom = 5 - 6/7
top/bottom
```
#### Examples
### Examples
##### Example
@@ -197,27 +195,22 @@ the `Main` module. `Julia` looks for variables in this module when it
encounters an expression and the value is substituted. Other uses, such as when variables are defined within a function, involve different contexts which may not be
visible within the `Main` module.
```julia; echo=false;
note("""
The `varinfo` function will list the variables currently defined in the
main workspace. There is no mechanism to delete a single variable.
""")
```
!!! note
The `varinfo` function will list the variables currently defined in the
main workspace. There is no mechanism to delete a single variable.
```julia; echo=false;
alert("""
**Shooting oneselves in the foot.** `Julia` allows us to locally
redefine variables that are built in, such as the value for `pi` or
the function object assigned to `sin`. For example, this is a
perfectly valid command `sin=3`. However, it will overwrite the
typical value of `sin` so that `sin(3)` will be an error. At the terminal, the
binding to `sin` occurs in the `Main` module. This shadows that
value of `sin` bound in the `Base` module. Even if redefined in
`Main`, the value in base can be used by fully qualifying the name,
as in `Base.sin(pi)`. This uses the notation
`module_name.variable_name` to look up a binding in a module.
""")
```
!!! warning
**Shooting oneselves in the foot.** `Julia` allows us to
locally redefine variables that are built in, such as the value
for `pi` or the function object assigned to `sin`. For example,
this is a perfectly valid command `sin=3`. However, it will
overwrite the typical value of `sin` so that `sin(3)` will be an
error. At the terminal, the binding to `sin` occurs in the `Main`
module. This shadows that value of `sin` bound in the `Base`
module. Even if redefined in `Main`, the value in base can be used
by fully qualifying the name, as in `Base.sin(pi)`. This uses the
notation `module_name.variable_name` to look up a binding in a
module.
## Variable names
@@ -268,17 +261,12 @@ For example, we could have defined `theta` (`\theta[tab]`) and `v0` (`v\_0[tab]`
θ = 45; v₀ = 200
```
These notes often use Unicode alternatives to avoid the `Pluto` requirement of a single use of assigning to a variable name in a notebook without placing the assignment in a `let` block or a function body.
!!! note "Unicode"
These notes can be presented as HTML files *or* as `Pluto` notebooks. They often use Unicode alternatives to avoid the `Pluto` requirement of a single use of assigning to a variable name in a notebook without placing the assignment in a `let` block or a function body.
```julia; echo=false;
alert("""
There is even support for tab-completion of
[emojis](https://github.com/JuliaLang/julia/blob/master/stdlib/REPL/src/emoji_symbols.jl)
such as `\\:snowman:[tab]` or `\\:koala:[tab]`
""")
```
!!! note "Emojis"
There is even support for tab-completion of [emojis](https://github.com/JuliaLang/julia/blob/master/stdlib/REPL/src/emoji_symbols.jl) such as `\\:snowman:[tab]` or `\\:koala:[tab]`
##### Example
@@ -404,8 +392,8 @@ What is the result of the above?
p, q = 0.25, 0.2;
top = p - q;
bottom = sqrt(p*(1-p));
ans = top/bottom;
numericq(ans)
answ = top/bottom;
numericq(answ)
```
###### Question
@@ -433,8 +421,8 @@ q"some_really_long_name_that_is_no_fun_to_type",
q"aMiXeDcAsEnAmE",
q"fahrenheit451"
]
ans = 1
radioq(choices, ans)
answ = 1
radioq(choices, answ)
```
@@ -445,8 +433,8 @@ Which of these symbols is one of `Julia`'s built-in math constants?
```julia; hold=true; echo=false;
choices = [q"pi", q"oo", q"E", q"I"]
ans = 1
radioq(choices, ans)
answ = 1
radioq(choices, answ)
```
@@ -464,8 +452,8 @@ choices=[
q"\delta[tab] = 1/10",
q"delta[tab] = 1/10",
q"$\\delta$ = 1/10"]
ans = 1
radioq(choices, ans)
answ = 1
radioq(choices, answ)
```
@@ -478,8 +466,8 @@ choices = [
q"a=1, b=2, c=3",
q"a,b,c = 1,2,3",
q"a=1; b=2; c=3"]
ans = 1
radioq(choices, ans)
answ = 1
radioq(choices, answ)
```
###### Question
@@ -497,6 +485,6 @@ choices = ["Assign all three variables at once to a value of `3`",
"Create ``3`` linked values that will stay synced when any value changes",
"Throw an error"
]
ans = 1
radioq(choices, ans)
answ = 1
radioq(choices, answ)
```

View File

@@ -8,7 +8,8 @@ using Plots
using Measures
using LaTeXStrings
fig_size = (400, 300)
#fig_size = (400, 300)
fig_size = (800, 600)
const frontmatter = (
title = "Vectors",
@@ -463,23 +464,13 @@ component. However, the special keyword `end` will do so as well, when
put into the context of indexing. So `v[end]` is more idiomatic. (Similarly, there is a `begin` keyword that is useful when the vector is not ``1``-based, as is typical but not mandatory.)
```julia; echo=false;
note("""
There is [much more](http://julia.readthedocs.org/en/latest/manual/arrays/#indexing)
to indexing than just indexing by a single integer value. For example, the following can be used for indexing:
* a scalar integer (as seen)
* a range
* a vector of integers
* a boolean vector
Some add-on packages extend this further.
""",
title="More on indexing", label="More on indexing")
```
!!! note "More on indexing"
There is [much more](http://julia.readthedocs.org/en/latest/manual/arrays/#indexing) to indexing than just indexing by a single integer value. For example, the following can be used for indexing:
* a scalar integer (as seen)
* a range
* a vector of integers
* a boolean vector
Some add-on packages extend this further.
### Assignment and indexing
@@ -668,10 +659,9 @@ performant `abs2` function which, in general, efficiently finds $|x|^2$
for various number types. The `.-` uses broadcasting to subtract a scalar (`mean(xs)`) from a vector (`xs`). Without the `.`, this would error.
```julia; echo=false;
note("""The `map` function is very much related to broadcasting and similarly named functions are found in many different programming languages. (The "dot" broadcast is mostly limited to `Julia` and mirrors on a similar usage of a dot in `MATLAB`.) For those familiar with other programming languages, using `map` may seem more natural. Its syntax is `map(f, xs)`.
""")
```
!!! note
The `map` function is very much related to broadcasting and similarly named functions are found in many different programming languages. (The "dot" broadcast is mostly limited to `Julia` and mirrors on a similar usage of a dot in `MATLAB`.) For those familiar with other programming languages, using `map` may seem more natural. Its syntax is `map(f, xs)`.
### Comprehensions
@@ -763,15 +753,9 @@ In the sequel, we will typically use broadcasting for this task using two
steps: one to define a function the second to broadcast it.
```julia; echo=false;
note(L"""
!!! note
The style generally employed here is to use plural variable names for a collection of values, such as the vector of $y$ values and singular names when a single value is being referred to, leading to expressions like "`x in xs`".
The style generally employed here is to use plural variable names for a collection
of values, such as the vector of $y$ values and singular names when a
single value is being referred to, leading to expressions like "`x in xs`".
""")
```
## Other container types
@@ -805,8 +789,8 @@ q"v = {4, 3}",
q"v = '4, 3'",
q"v = (4,3)",
q"v = <4,3>"]
ans = 1
radioq(choices, ans)
answ = 1
radioq(choices, answ)
```
###### Question
@@ -815,8 +799,8 @@ Which command will create the vector with components "4,3,2,1"?
```julia; hold=true; echo=false;
choices = [q"v = [4,3,2,1]", q"v = (4,3,2,1)", q"v = {4,3,2,1}", q"v = '4, 3, 2, 1'", q"v = <4,3,2,1>"]
ans = 1
radioq(choices, ans)
answ = 1
radioq(choices, answ)
```
@@ -840,8 +824,8 @@ Which of the following is the unit vector in the direction of $\vec{v} = \langle
```julia; hold=true; echo=false;
choices = [q"[3, 4]", q"[0.6, 0.8]", q"[1.0, 1.33333]", q"[1, 1]"]
ans = 2
radioq(choices, ans)
answ = 2
radioq(choices, answ)
```
@@ -851,8 +835,8 @@ What vector is in the same direction as $\vec{v} = \langle 3,~ 4 \rangle$ but is
```julia; hold=true; echo=false;
choices = [q"[3, 4]", q"[30, 40]", q"[9.48683, 12.6491 ]", q"[10, 10]"]
ans = 2
radioq(choices, ans)
answ = 2
radioq(choices, answ)
```
###### Question
@@ -861,8 +845,8 @@ If $\vec{v} = \langle 3,~ 4 \rangle$ and $\vec{w} = \langle 1,~ 2 \rangle$ find
```julia; hold=true; echo=false;
choices = [q"[4, 6]", q"[6, 8]", q"[11, 18]", q"[5, 10]"]
ans = 3
radioq(choices, ans)
answ = 3
radioq(choices, answ)
```
###### Question
@@ -939,24 +923,24 @@ Express vector **c** in terms of **a** and **b**:
```julia; hold=true; echo=false;
choices = ["3a", "3b", "a + b", "a - b", "b-a"]
ans = 1
radioq(choices, ans)
answ = 1
radioq(choices, answ)
```
Express vector **d** in terms of **a** and **b**:
```julia; hold=true; echo=false;
choices = ["3a", "3b", "a + b", "a - b", "b-a"]
ans = 3
radioq(choices, ans)
answ = 3
radioq(choices, answ)
```
Express vector **e** in terms of **a** and **b**:
```julia; hold=true; echo=false;
choices = ["3a", "3b", "a + b", "a - b", "b-a"]
ans = 4
radioq(choices, ans)
answ = 4
radioq(choices, answ)
```
@@ -966,8 +950,8 @@ If `xs=[1, 2, 3, 4]` and `f(x) = x^2` which of these will not produce the vector
```julia; hold=true; echo=false;
choices = [q"f.(xs)", q"map(f, xs)", q"[f(x) for x in xs]", "All three of them work"]
ans = 4
radioq(choices, ans, keep_order=true)
answ = 4
radioq(choices, answ, keep_order=true)
```
###### Question
@@ -982,8 +966,8 @@ What construct will give the function values of $f$ at the zeros of $g$?
```julia;hold=true; echo=false;
choices = [q"sin(zs)", q"sin.(zs)", q"sin(.zs)", q".sin(zs)"]
ans = 2
radioq(choices, ans, keep_order=true)
answ = 2
radioq(choices, answ, keep_order=true)
```
###### Question
@@ -997,6 +981,6 @@ q"sqrt.(zs)",
q"zs^(1/2)",
q"zs^(1./2)"
]
ans = 2
radioq(choices, ans, keep_order=true)
answ = 2
radioq(choices, answ, keep_order=true)
```