Here is an equation without label using backslash-bracket environment: $$ a = b + c $$ or with number and label, as in (12), using the equation environment: $$ \begin{equation} {\partial u\over\partial t} = \nabla^2 u \tag{12} \end{equation} $$ We can refer to this equation by (12).
Here is a system without equation numbers, using the align-asterisk environment: $$ \begin{align*} \pmb{a} &= \pmb{q}\times\pmb{n} \\ b &= \nabla^2 u + \nabla^4 v \end{align*} $$
And here is a system of equations with labels in an align environment:
$$
\begin{align}
a &= q + 4 + 5+ 6 \tag{13} \\
b &= \nabla^2 u + \nabla^4 x \tag{14}
\end{align}
$$
We can refer to (13)-(14). They are a bit simpler than
the Navier–Stokes equations. And test LaTeX hyphen in CG-2
.
Also test \( a_{i-j} \) as well as \( kx-wt \).
Testing alignat
environment:
$$
\begin{alignat}{2}
a &= q + 4 + 5+ 6\qquad & \mbox{for } q\geq 0 \tag{15} \\
b &= \nabla^2 u + \nabla^4 x & x\in\Omega \tag{16}
\end{alignat}
$$
More mathematical typesetting is demonstrated in the coming exercises.
Below, we have Problem 2: Flip a Coin and Project 4: Compute a Probability, as well as Project 5: Explore Distributions of Random Circles and Project 11: References in a headings do not work well in html, and in between there we have Exercise 10: Make references to projects and problems.
a) Make a program that simulates flipping a coin \( N \) times. Print out "tail" or "head" for each flip and let the program count the number of heads.
Use r = random.random()
and define head as r <= 0.5
.
Draw an integer among \( \{1,2\} \) with
r = random.randint(1,2)
and define head when r
is 1.
If the random.random()
function returns a number \( < 1/2 \), let it be
head, otherwise tail. Repeat this \( N \) number of times.
import sys, random
N = int(sys.argv[1])
heads = 0
for i in range(N):
r = random.random()
if r <= 0.5:
heads += 1
print 'Flipping a coin %d times gave %d heads' % (N, heads)
b) Vectorize the code in a) using boolean indexing.
Vectorized code can be written in many ways. Sometimes the code is less intuitive, sometimes not. At least there is not much to find in the section Section 1.
c)
Vectorize the code in a) using numpy.sum
.
np.sum(np.where(r <= 0.5, 1, 0))
or np.sum(r <= 0.5)
.
In this latter subexercise, we have an example where the code is easy to read.
Remarks with such a subsubsection is treated as more text after the last subexercise. Test a list too:
flip_coin.py
, flip_coin.pdf
.
These are the exercise remarks, appearing at the very end.
Should be possible to stick a normal section in the middle of many exercises.
Very short exercise. What is the capital
of Norway?
Filename: myexer1
.
What is the probability of getting a number between 0.5 and 0.6 when drawing uniformly distributed random numbers from the interval \( [0,1) \)?
At the end we have a list because that caused problems in LaTeX in previous DocOnce versions:
To answer this question empirically, let a program
draw \( N \) such random numbers using Python's standard random
module,
count how many of them, \( M \), that fall in the interval \( (0.5,0.6) \), and
compute the probability as \( M/N \).
The formula for a circle is given by
$$
\begin{align}
x &= x_0 + R\cos 2\pi t,
\tag{17}\\
y &= y_0 + R\sin 2\pi t,
\tag{18}
\end{align}
$$
where \( R \) is the radius of the circle, \( (x_0,y_0) \) is the
center point, and \( t \) is a parameter in the unit interval \( [0,1] \).
For any \( t \), \( (x,y) \) computed from (17)-(18)
is a point on the circle.
The formula can be used to generate n
points on a circle:
import numpy as np
def circle(R, x0, y0, n=501):
t = np.linspace(0, 1, n)
x = x0 + R*np.cos(2*np.pi*t)
y = y0 + R*np.sin(2*np.pi*t)
return x, y
x, y = circle(2.0, 0, 0)
The goal of this project is to draw \( N \) circles with random
center and radius. Plot each circle using the circle
function
above.
a) Let \( R \) be normally distributed and \( (x_0,y_0) \) uniformly distributed.
Use the numpy.random
module to draw the
\( x_0 \), \( y_0 \), and \( R \) quantities.
Here goes the short answer to part a).
Here goes a full solution to part a).
b)
Let \( R \) be uniformly distributed and \( (x_0,y_0) \) normally distributed.
Filename: norm
.
c) Let \( R \) and \( (x_0,y_0) \) be normally distributed.
Filename: circles
.
At the very end of the exercise it may be appropriate to summarize and give some perspectives.
Intro to this exercise. Questions are in subexercises below.
a) Subexercises are numbered a), b), etc.
First hint to subexercise a). With math \( a=b \) in hint: $$ a=b. $$ And with code (in plain verbatim) returning \( x+1 \) in hint:
def func(x):
return x + 1 # with code in hint
Second hint to subexercise a).
Test list in hint:
Filename: subexer_a.pdf
.
Short answer to subexercise a). With math in answer: \( a=b \).
b) Here goes the text for subexercise b).
Some math \( \cos^2 x + \sin^2 x = 1 \) written one a single line: $$ \cos^2 x + \sin^2 x = 1 \thinspace .$$
A hint for this subexercise.
Filename: subexer_b.pdf
.
Here goes the solution of this subexercise.
The text here belongs to the main (intro) part of the exercise. Need closing remarks to have text after subexercises.
Test list in exercise:
Here goes a full solution of the whole exercise.
With some math \( a=b \) in this solution:
$$ \hbox{math in solution: } a = b $$
And code a=b
in this solution:
a = b # code in solution
End of solution is here.
Some final closing remarks, e.g., summarizing the main findings and their implications in other problems can be made. These remarks will appear at the end of the typeset exercise.
Just some text. And some math saying that \( e^0=1 \) on a single line, to test that math block insertion is correct: $$ \exp{(0)} = 1 $$
And a test that the code lambda x: x+2
is correctly placed here:
lambda x: x+2
Given $$ \frac{dy}{dx} = -y(x),\quad y(0)=1 $$ What is the solution of this equation?
from math import exp
def f(x):
return exp(x)
exp(-x)
, otherwise this Python code
must be considered as a good answer. It is more natural,
though, to write the solution to the problem
in mathematical notation:
$$ y(x) = e^{-y}.$$
Choice D: The solution cannot be found because there is a derivative in the equation.
Choice E: The equation is meaningless: an equation must be an equation for \( x \) or \( y \), not a function \( y(x) \).
a) What is the capital of Norway?
Answer. Oslo.
With some text, before we continue with exercises.
Pick a statement from Project 5: Explore Distributions of Random Circles or Problem 2: Flip a Coin and verify it.
Test list at the end of an exercise without other elements (like subexercise, hint, etc.):
verify_formula.py
.
Refer to the previous exercise as Exercise 10: Make references to projects and problems,
the two before that as Project 4: Compute a Probability and Project 5: Explore Distributions of Random Circles,
and this one as Project 11: References in a headings do not work well in html.
Filename: selc_composed.pdf
.
This is the first appendix.
Some text.
This is more stuff for an appendix.
Without label.
With label.
What about inserting a quiz?