Code

Click here to return to my homepage.

TeX

Below are some things I've drawn or coded in TeX.

The code below draws the slope field given by a function \(f(x,y)\) of two variables, once you've specified a few parameters. This example produces the slope field for \(f(x,y) = x^2 + y^3\). Be aware that this is unlikely to work with poorly-behaved functions, for instance when the slope is undefined.

You can download the TeX file here.

\documentclass{amsart}
\usepackage{tikz}
\begin{document}
\begin{center}
\begin{tikzpicture}
%% Set the window for x and y values.
\pgfmathsetmacro\xmin{-2};
\pgfmathsetmacro\ymin{-2};
\pgfmathsetmacro\xmax{2};
\pgfmathsetmacro\ymax{2};
%% Set the density of the slope field; a lower spacing gives a denser slope field.
\pgfmathsetmacro\xspacing{0.2};
\pgfmathsetmacro\yspacing{0.2};
%% Set the length of the tangent lines: all lines of the slope field are drawn with this length. This should be less than both spacing numbers above for a neat slope field.
\pgfmathsetmacro\length{0.1};
%% Define the slope at (\x,\y) as a function f of \x and \y. Here, #1 is \x and #2 is \y. The function should be defined within \pgfmathparse{ }.
\pgfmathdeclarefunction{f}{2}{\pgfmathparse{(#1)^2 + (#2)^3}};

%% This block generates the slope field.
%% Restrict drawing to the confines of the specified rectangle.
\clip (\xmin,\ymin) rectangle (\xmax,\ymax);
%% Draw the axes.
\draw (\xmin,0) -- (\xmax,0);
\draw (0,\ymin) -- (0,\ymax);
%% Some calculations (since we cannot do arithmetic inside a list).
\pgfmathsetmacro\xnext{\xmin+\xspacing};
\pgfmathsetmacro\ynext{\ymin+\yspacing};
%% Loop to draw the tangent lines.
\foreach \x in {\xmin,\xnext, ..., \xmax}{ \foreach \y in {\ymin, \ynext, ..., \ymax}{
%% Calculate slope at (\x,\y).
\pgfmathsetmacro\slope{f(\x,\y)};
\begin{scope}
%% Cut the drawing down to the circle of radius 0.5*\length centered at (\x,\y).
\clip (\x,\y) circle ({0.5*\length});
%% Draw tangent lines of length at least \length, with midpoint (\x,\y).
\draw[very thin] ({\x - 0.5*\length}, {\y - 0.5*\slope*\length}) -- ({\x + 0.5*\length}, {\y + 0.5*\slope*\length});
\end{scope}
}
}

\end{tikzpicture}
\end{center}
\end{document}

Using Euler's method, it is relatively easy to add a solution curve. The following amended code draws the slope field above along with a solution curve passing through the point \((0, 0.3)\).

You can download the TeX file here.

\documentclass{amsart}
\usepackage{tikz}
\begin{document}
\begin{center}
\begin{tikzpicture}
%% Set the window for x and y values.
\pgfmathsetmacro\xmin{-2};
\pgfmathsetmacro\ymin{-2};
\pgfmathsetmacro\xmax{2};
\pgfmathsetmacro\ymax{2};
%% Set the density of the slope field; a lower spacing gives a denser slope field.
\pgfmathsetmacro\xspacing{0.2};
\pgfmathsetmacro\yspacing{0.2};
%% Set the length of the tangent lines: all lines of the slope field are drawn with this length. This should be less than both spacing numbers above for a neat slope field.
\pgfmathsetmacro\length{0.1};
%% Define the slope at (\x,\y) as a function f of \x and \y. Here, #1 is \x and #2 is \y. The function should be defined within \pgfmathparse{ }.
\pgfmathdeclarefunction{f}{2}{\pgfmathparse{(#1)^2 + (#2)^3}};

%% Specify the step size to use in Euler's method.
\pgfmathsetmacro\stepsize{0.005};
%% Specify the bounds of the interval on which to draw the solution curve, and the x co-ordinate of the initial point. Make sure that the pairwise differences \initialx - lower and \upper - \initialx are divisible by \stepsize.
\pgfmathsetmacro\lower{-2};
\pgfmathsetmacro\initialx{0};
\pgfmathsetmacro\upper{1.4};
%% Specify the desired y-value at x=\initialx of the solution to be graphed.
\pgfmathsetmacro\initialy{0.3};

%% This block generates the slope field.
%% Restrict drawing to the confines of the specified rectangle.
\clip (\xmin,\ymin) rectangle (\xmax,\ymax);
%% Draw the axes.
\draw (\xmin,0) -- (\xmax,0);
\draw (0,\ymin) -- (0,\ymax);
%% Some calculations (since we cannot do arithmetic inside a list).
\pgfmathsetmacro\xnext{\xmin+\xspacing};
\pgfmathsetmacro\ynext{\ymin+\yspacing};
%% Loop to draw the tangent lines.
\foreach \x in {\xmin,\xnext, ..., \xmax}{ \foreach \y in {\ymin, \ynext, ..., \ymax}{
%% Calculate slope at (\x,\y).
\pgfmathsetmacro\slope{f(\x,\y)};
\begin{scope}
%% Cut the drawing down to the circle of radius 0.5*\length centered at (\x,\y).
\clip (\x,\y) circle ({0.5*\length});
%% Draw tangent lines of length at least \length, with midpoint (\x,\y).
\draw[very thin] ({\x - 0.5*\length}, {\y - 0.5*\slope*\length}) -- ({\x + 0.5*\length}, {\y + 0.5*\slope*\length});
\end{scope}
}
}

%% This block generates the solution curve with the specificed parameters.
%% Some calculations again.
\pgfmathsetmacro\xnext{\initialx+\stepsize};
\pgfmathsetmacro\xprev{\initialx-\stepsize};
%% Iterate to draw the desired solution curve with Euler's method.
\foreach[remember=\lasty as \y (initially \initialy)] \x in {\initialx,\xnext, ..., \upper}{
\draw (\x,\y) -- ({\x + \stepsize}, {\y + f(\x,\y)*\stepsize});
\pgfmathsetmacro\lasty{\y + f(\x,\y)*\stepsize};
}
\foreach[remember=\lasty as \y (initially \initialy)] \x in {\initialx,\xprev, ..., \lower}{
\draw (\x,\y) -- ({\x - \stepsize}, {\y - f(\x,\y)*\stepsize});
\pgfmathsetmacro\lasty{\y - f(\x,\y)*\stepsize};
}
\end{tikzpicture}
\end{center}
\end{document}

Here is a torus. You can download the TeX file here.

\documentclass{amsart}
\usepackage{fullpage}
\usepackage{tikz}
\begin{document}
\begin{center}
\begin{tikzpicture}
\draw[smooth] (0,0) to[out=90,in=90] (3,0) to[out=-90,in=-90] (0,0);
\draw[smooth] (0.9,0) .. controls (1.3,-0.15) and (1.7,-0.15) .. (2.1,0);
\draw[smooth] (1,-0.03) .. controls (1.2,0.2) and (1.8,0.2) .. (2,-0.03);
\end{tikzpicture}
\end{center}
\end{document}

Here is a surface of genus 2. You can download the TeX file here.

\documentclass{amsart}
\usepackage{fullpage}
\usepackage{tikz}
\begin{document}
\begin{center}
\begin{tikzpicture}
\draw[smooth] (0,1) to[out=30,in=150] (2,1) to[out=-30,in=210] (3,1) to[out=30,in=150] (5,1) to[out=-30,in=30] (5,-1) to[out=210,in=-30] (3,-1) to[out=150,in=30] (2,-1) to[out=210,in=-30] (0,-1) to[out=150,in=-150] (0,1);
\draw[smooth] (0.4,0.1) .. controls (0.8,-0.25) and (1.2,-0.25) .. (1.6,0.1);
\draw[smooth] (0.5,0) .. controls (0.8,0.2) and (1.2,0.2) .. (1.5,0);
\draw[smooth] (3.4,0.1) .. controls (3.8,-0.25) and (4.2,-0.25) .. (4.6,0.1);
\draw[smooth] (3.5,0) .. controls (3.8,0.2) and (4.2,0.2) .. (4.5,0);
\end{tikzpicture}
\end{center}
\end{document}

Here is a pants decompositions for this surface. You can download the TeX file here.

\documentclass{amsart}
\usepackage{fullpage}
\usepackage{tikz}
\begin{document}
\thispagestyle{empty}
\begin{center}
\begin{tikzpicture}
\draw[smooth] (0,1) to[out=30,in=150] (2,1) to[out=-30,in=210] (3,1) to[out=30,in=150] (5,1) to[out=-30,in=30] (5,-1) to[out=210,in=-30] (3,-1) to[out=150,in=30] (2,-1) to[out=210,in=-30] (0,-1) to[out=150,in=-150] (0,1);
\draw[smooth] (0.4,0.1) .. controls (0.8,-0.25) and (1.2,-0.25) .. (1.6,0.1);
\draw[smooth] (0.5,0) .. controls (0.8,0.2) and (1.2,0.2) .. (1.5,0);
\draw[smooth] (3.4,0.1) .. controls (3.8,-0.25) and (4.2,-0.25) .. (4.6,0.1);
\draw[smooth] (3.5,0) .. controls (3.8,0.2) and (4.2,0.2) .. (4.5,0);
\draw (-0.5,0) arc(180:0:0.51 and 0.2);
\draw[dashed] (-0.5,0) arc(180:0:0.51 and -0.2);
\draw (2.5,-0.85) arc(270:90:0.3 and 0.85);
\draw[dashed] (2.5,-0.85) arc(270:450:0.3 and 0.85);
\draw (5.5,0) arc(180:0:-0.51 and 0.2);
\draw[dashed] (5.5,0) arc(180:0:-0.51 and -0.2);
\end{tikzpicture}
\end{center}
\end{document}

Here is a surface of genus 3. You can download the TeX file here.

\documentclass{amsart}
\usepackage{fullpage}
\usepackage{tikz}
\begin{document}
\begin{center}
\begin{tikzpicture}
\draw[smooth] (0,1) to[out=30,in=150] (2,1) to[out=-30,in=210] (3,1) to[out=30,in=150] (5,1) to[out=-30,in=210] (6,1) to[out=30,in=150] (8,1) to[out=-30,in=30] (8,-1) to[out=210,in=-30] (6,-1) to[out=150,in=30] (5,-1) to[out=210,in=-30] (3,-1) to[out=150,in=30] (2,-1) to[out=210,in=-30] (0,-1) to[out=150,in=-150] (0,1);
\draw[smooth] (0.4,0.1) .. controls (0.8,-0.25) and (1.2,-0.25) .. (1.6,0.1);
\draw[smooth] (0.5,0) .. controls (0.8,0.2) and (1.2,0.2) .. (1.5,0);
\draw[smooth] (3.4,0.1) .. controls (3.8,-0.25) and (4.2,-0.25) .. (4.6,0.1);
\draw[smooth] (3.5,0) .. controls (3.8,0.2) and (4.2,0.2) .. (4.5,0);
\draw[smooth] (6.4,0.1) .. controls (6.8,-0.25) and (7.2,-0.25) .. (7.6,0.1);
\draw[smooth] (6.5,0) .. controls (6.8,0.2) and (7.2,0.2) .. (7.5,0);
\end{tikzpicture}
\end{center}
\end{document}

Here is a pants decomposition for this surface. You can download the TeX file here.

\documentclass{amsart}
\usepackage{fullpage}
\usepackage{tikz}
\begin{document}
\begin{center}
\begin{tikzpicture}
\draw[smooth] (0,1) to[out=30,in=150] (2,1) to[out=-30,in=210] (3,1) to[out=30,in=150] (5,1) to[out=-30,in=210] (6,1) to[out=30,in=150] (8,1) to[out=-30,in=30] (8,-1) to[out=210,in=-30] (6,-1) to[out=150,in=30] (5,-1) to[out=210,in=-30] (3,-1) to[out=150,in=30] (2,-1) to[out=210,in=-30] (0,-1) to[out=150,in=-150] (0,1);
\draw[smooth] (0.4,0.1) .. controls (0.8,-0.25) and (1.2,-0.25) .. (1.6,0.1);
\draw[smooth] (0.5,0) .. controls (0.8,0.2) and (1.2,0.2) .. (1.5,0);
\draw[smooth] (3.4,0.1) .. controls (3.8,-0.25) and (4.2,-0.25) .. (4.6,0.1);
\draw[smooth] (3.5,0) .. controls (3.8,0.2) and (4.2,0.2) .. (4.5,0);
\draw[smooth] (6.4,0.1) .. controls (6.8,-0.25) and (7.2,-0.25) .. (7.6,0.1);
\draw[smooth] (6.5,0) .. controls (6.8,0.2) and (7.2,0.2) .. (7.5,0);
\draw (-0.5,0) arc(180:0:0.51 and 0.2);
\draw[dashed] (-0.5,0) arc(180:0:0.51 and -0.2);
\draw (2.5,-0.85) arc(270:90:0.3 and 0.85);
\draw[dashed] (2.5,-0.85) arc(270:450:0.3 and 0.85);
\draw (4.0,0.15) arc(270:90:0.3 and 1.14/2);
\draw[dashed] (4.0,0.15) arc(270:450:0.3 and 1.14/2);
\draw (4.0,-0.15) arc(270:90:0.3 and -1.14/2);
\draw[dashed] (4.0,-0.15) arc(270:450:0.3 and -1.14/2);
\draw (5.5,-0.85) arc(270:90:0.3 and 0.85);
\draw[dashed] (5.5,-0.85) arc(270:450:0.3 and 0.85);
\draw (8.5,0) arc(180:0:-0.51 and 0.2);
\draw[dashed] (8.5,0) arc(180:0:-0.51 and -0.2);
\end{tikzpicture}
\end{center}
\end{document}

Here is a surface of genus n. You can download the TeX file here.

\documentclass{amsart}
\usepackage{fullpage}
\usepackage{tikz}
\begin{document}
\thispagestyle{empty}
\begin{center}
\begin{tikzpicture}
\draw[smooth] (5.5,-0.85) to[out=180,in=30] (5,-1) to[out=210,in=-30] (3,-1) to[out=150,in=30] (2,-1) to[out=210,in=-30] (0,-1) to[out=150,in=-150] (0,1) to[out=30,in=150] (2,1) to[out=-30,in=210] (3,1) to[out=30,in=150] (5,1) to[out=-30,in=180] (5.5,0.85);
\draw[smooth] (0.4,0.1) .. controls (0.8,-0.25) and (1.2,-0.25) .. (1.6,0.1);
\draw[smooth] (0.5,0) .. controls (0.8,0.2) and (1.2,0.2) .. (1.5,0);
\draw[smooth] (3.4,0.1) .. controls (3.8,-0.25) and (4.2,-0.25) .. (4.6,0.1);
\draw[smooth] (3.5,0) .. controls (3.8,0.2) and (4.2,0.2) .. (4.5,0);
\node at (6.5,0) {$. \; \; . \; \; .$};
\draw[smooth] (7.5,0.85) to[out=0,in=210] (8,1) to[out=30,in=150] (10,1) to[out=-30,in=210] (11,1) to[out=30,in=150] (13,1) to[out=-30,in=30] (13,-1) to[out=210,in=-30] (11,-1) to[out=150,in=30] (10,-1) to[out=210,in=-30] (8,-1) to[out=150,in=0] (7.5,-0.85);
\draw[smooth] (8.4,0.1) .. controls (8.8,-0.25) and (9.2,-0.25) .. (9.6,0.1);
\draw[smooth] (8.5,0) .. controls (8.8,0.2) and (9.2,0.2) .. (9.5,0);
\draw[smooth] (11.4,0.1) .. controls (11.8,-0.25) and (12.2,-0.25) .. (12.6,0.1);
\draw[smooth] (11.5,0) .. controls (11.8,0.2) and (12.2,0.2) .. (12.5,0);
\draw (5.5,-0.85) arc(270:90:0.3 and 0.85);
\draw (5.5,-0.85) arc(270:450:0.3 and 0.85);
\draw (7.5,-0.85) arc(270:90:0.3 and 0.85);
\draw[dashed] (7.5,-0.85) arc(270:450:0.3 and 0.85);
\end{tikzpicture}
\end{center}
\end{document}

Here is a pants decomposition for this surface. You can download the TeX file here.

\documentclass{amsart}
\usepackage{fullpage}
\usepackage{tikz}
\begin{document}
\begin{center}
\begin{tikzpicture}
\draw[smooth] (5.5,-0.85) to[out=180,in=30] (5,-1) to[out=210,in=-30] (3,-1) to[out=150,in=30] (2,-1) to[out=210,in=-30] (0,-1) to[out=150,in=-150] (0,1) to[out=30,in=150] (2,1) to[out=-30,in=210] (3,1) to[out=30,in=150] (5,1) to[out=-30,in=180] (5.5,0.85);
\draw[smooth] (0.4,0.1) .. controls (0.8,-0.25) and (1.2,-0.25) .. (1.6,0.1);
\draw[smooth] (0.5,0) .. controls (0.8,0.2) and (1.2,0.2) .. (1.5,0);
\draw[smooth] (3.4,0.1) .. controls (3.8,-0.25) and (4.2,-0.25) .. (4.6,0.1);
\draw[smooth] (3.5,0) .. controls (3.8,0.2) and (4.2,0.2) .. (4.5,0);
\node at (6.5,0) {$. \; \; . \; \; .$};
\draw[smooth] (7.5,0.85) to[out=0,in=210] (8,1) to[out=30,in=150] (10,1) to[out=-30,in=210] (11,1) to[out=30,in=150] (13,1) to[out=-30,in=30] (13,-1) to[out=210,in=-30] (11,-1) to[out=150,in=30] (10,-1) to[out=210,in=-30] (8,-1) to[out=150,in=0] (7.5,-0.85);
\draw[smooth] (8.4,0.1) .. controls (8.8,-0.25) and (9.2,-0.25) .. (9.6,0.1);
\draw[smooth] (8.5,0) .. controls (8.8,0.2) and (9.2,0.2) .. (9.5,0);
\draw[smooth] (11.4,0.1) .. controls (11.8,-0.25) and (12.2,-0.25) .. (12.6,0.1);
\draw[smooth] (11.5,0) .. controls (11.8,0.2) and (12.2,0.2) .. (12.5,0);
\draw (-0.5,0) arc(180:0:0.51 and 0.2);
\draw[dashed] (-0.5,0) arc(180:0:0.51 and -0.2);
\draw (2.5,-0.85) arc(270:90:0.3 and 0.85);
\draw[dashed] (2.5,-0.85) arc(270:450:0.3 and 0.85);
\draw (4.0,0.15) arc(270:90:0.3 and 1.14/2);
\draw[dashed] (4.0,0.15) arc(270:450:0.3 and 1.14/2);
\draw (4.0,-0.15) arc(270:90:0.3 and -1.14/2);
\draw[dashed] (4.0,-0.15) arc(270:450:0.3 and -1.14/2);
\draw (5.5,-0.85) arc(270:90:0.3 and 0.85);
\draw (5.5,-0.85) arc(270:450:0.3 and 0.85);
\draw (7.5,-0.85) arc(270:90:0.3 and 0.85);
\draw[dashed] (7.5,-0.85) arc(270:450:0.3 and 0.85);
\draw (9.0,0.15) arc(270:90:0.3 and 1.14/2);
\draw[dashed] (9.0,0.15) arc(270:450:0.3 and 1.14/2);
\draw (9.0,-0.15) arc(270:90:0.3 and -1.14/2);
\draw[dashed] (9.0,-0.15) arc(270:450:0.3 and -1.14/2);
\draw (10.5,-0.85) arc(270:90:0.3 and 0.85);
\draw[dashed] (10.5,-0.85) arc(270:450:0.3 and 0.85);
\draw (13.5,0) arc(180:0:-0.51 and 0.2);
\draw[dashed] (13.5,0) arc(180:0:-0.51 and -0.2);
\end{tikzpicture}
\end{center}
\end{document}