Mathematica — Euler’s method for systems, revisited

An earlier post discusses Mathematica code for Euler’s method. Here is some updated code.

A couple preliminary notes:

  • I like to clear all variables at the beginning.
  • For systems it is fun to superimpose the plot on top of the stream plot of the corresponding vector field

Example 1
For the IVP \frac{dy}{dt} = t-y, y(0) = 2 with \Delta t = 0.1 we can use the code

Clear["Global`*"]
f[t_, y_] := t - y
deltat = 0.1;
eulerValues = 
RecurrenceTable[{t[k + 1] == t[k] + deltat,
   y[k + 1] == y[k] + deltat*f[t[k],y[k]], 
   t[0] == 0., 
   y[0] == 2.}, 
   {t, y}, {k, 0, 10}];
Grid[eulerValues]
eulerPlot=ListLinePlot[eulerValues, PlotMarkers -> Automatic]
realPlot = Plot[3 Exp[-t] + t - 1, 
	{t, 0, 1}, PlotStyle -> Red];
Show[eulerPlot, realPlot]

Example 2
Consider \frac{dy}{dt} = e^y, y(0)=1, \Delta t = .25
We use

Clear["Global`*"]
deltat = .05;
f[y_] := Exp[y];
values = RecurrenceTable[{
    t[k + 1] == t[k] + deltat,
    y[k + 1] == y[k] + deltat*f[y[k]],
    t[0] == 0, y[0] == 1
    }, {t, y}, {k, 0, 10}];
Grid[values]
ListLinePlot[values, PlotMarkers -> Automatic]

Example 3
Here we consider the system
\frac{dx}{dt} = x(1-x) - xy
\frac{dy}{dt} = -y + 2 xy
with \Delta t = 0.1 and initial conditions x(0) = 1.5, y(0) = 1. The following code processes the first 100 time steps.

Clear["Global`*"]

f[x_, y_] := x (1 - x) - x*y
g[x_, y_] := -y + 2 x*y
streamPlot = StreamPlot[
	{f[x, y], g[x, y]}, 
	{x, -1, 2}, 
	{y, -1, 2}]
	
deltat = 0.1;
steps = 100;

eulerValues = RecurrenceTable[{
    t[0] == 0, x[0] == 1.5, y[0] == 1,
    t[k + 1] == t[k] + deltat, 
    x[k + 1] == x[k] + deltat*f[x[k], y[k]], 
    y[k + 1] == y[k] + deltat*g[x[k], y[k]]},
   {t, x, y},
   {k, 0, steps}];
eulerPlotValues = Table[{
    eulerValues[[k, 2]],
    eulerValues[[k, 3]]},
   {k, 1, steps + 1}];
eulerPlot = ListLinePlot[
  eulerPlotValues,
  PlotMarkers -> Automatic,
  PlotStyle -> Red]
  
Show[streamPlot, eulerPlot]
Posted in Differential equations, Mathematica | Leave a comment

Mathematica for linear algebra

Here is some mathematica code to be used by my linear algebra students. The post will be periodically updated as the course progresses.

Entering matrices

Mathematica views matrices as lists of columns. Thus to enter the matrix
\left(  \begin{array}{cccc}   1 & 1 & -1 & -1 \\   1 & 2 & 3 & 4 \\   1 & 3 & 5 & 7 \\  \end{array}  \right)
we type in

mat = {{1, 1, -1, -1}, {1, 2, 3, 4}, {1, 3, 5, 7}};

and hit [enter]. Here we have given the matrix the name mat and suppressed the output.

To view the matrix, type in the code

MatrixForm[mat]

and hit [enter].

Row reducing a matrix

To row reduce the matrix, type in the code

RowReduce[mat]

and hit [enter]. Notice that the output is just given in list format. To display the result as a matrix, you can use either

MatrixForm[RowReduce[mat]]

or

RowReduce[mat] // MatrixForm
Posted in Linear Algebra, Mathematica | Leave a comment

Idea for building intuition for Fourier series and transform

One of the (many) challenging aspects of my Math 305 course (which is roughly a math methods course for scientists) is to help students build intuition for the Fourier transform. Roughly speaking, my goal is for students to not only know how to use Fourier transforms to address problems, but also to gain some intuition for the interplay between “physical space” and “transform space” (also called “k-space” in some physics circles).

(In some sense, what I’m trying to do is to communicate the ideas underlying microlocal analysis without buiding up any of the machinery. Now that I’ve written that, it occurs to me that it could be useful to check out a couple of introductory microlocal analysis books…)

At a first pass, one can look at what it means to be “concentrated” in one space or another. By playing around with functions of the form e^{-\alpha x^2} or characteristic functions, one can pretty quickly develop intuition for the idea that begin “concentrated” in one space roughly corresponds to being “spread out” in another.

Today it occurs to me that it might be very helpful to introduce this same idea when studying cosine/sine Fourier series. A really good exercise would be to plot the magnitude of the coefficients (as a function of frequency) for various functions… and then have students construct functions with various types of frequency distributions. For example, what does a function having equal amounts of the first five frequencies, and no others, look like on physical space? What happens if we toss in equal amounts of frequencies 25-30? Etc.

If this is done at the leve of the Fourier series (and perhaps again at the level of other Fourier-type series, such as Hermite, Legendre, Bessel), then perhaps it will be more natural when we come to Fourier transforms. One can even assign essentially the same problem: Build a function with compact support [0,1] in frequency space. What does it look like in physical space? What happens if we add a function with frequency support in [25,30]? Etc.

Posted in Math 305 | Leave a comment

Oberwolfach report: Weakly asymptotically hyperbolic metrics and the constraint equations

Earlier this month I gave a lecture at Mathematisches Forschungsinstitut Oberwolfach as part of participating in the workshop “Mathematical Aspects of General Relativity.”

My Oberwolfach Report contains a short description of the contents of my lecture.

Posted in Asymptotically hyperbolic, Constraint Equations, Differential equations, Elliptic PDE | Leave a comment

New papers: Weakly asymptotically hyperbolic manifolds, constraint equations, etc.

Two new papers, joint with James Isenberg, John M. Lee, and Iva Stavrov Allen have been posted to the arXiv.

Weakly asymptotically hyperbolic manifolds.

Abstract:

We introduce a class of “weakly asymptotically hyperbolic” geometries whose sectional curvatures tend to -1 and are C^0, but are not necessarily C^1 conformally compact. We subsequently investigate the rate at which curvature invariants decay at infinity, identifying a conformally invariant tensor which serves as an obstruction to “higher order decay” of the Riemann curvature operator. Finally, we establish Fredholm results for geometric elliptic operators, extending the work of Rafe Mazzeo and John M. Lee to this setting. As an application, we show that any weakly asymptotically hyperbolic metric is conformally related to a weakly asymptotically hyperbolic metric of constant negative curvature.

The shear-free condition and constant-mean-curvature hyperboloidal initial data.

Abstract:

We consider the Einstein-Maxwell-fluid constraint equations, and make use of the conformal method to construct and parametrize constant-mean-curvature hyperboloidal initial data sets that satisfy the shear-free condition. This condition is known to be necessary in order that a spacetime development admit a regular conformal boundary at future null infinity. We work with initial data sets in a variety of regularity classes, primarily considering those data sets whose geometries are weakly asymptotically hyperbolic, as defined in [arXiv:1506.03399]. These metrics are C^{1,1} conformally compact, but not necessarily C^2 conformally compact. In order to ensure that the data sets we construct are indeed shear-free, we make use of the conformally covariant traceless Hessian introduced in [arXiv:1506.03399]. We furthermore construct a class of initial data sets with weakly asymptotically hyerbolic metrics that may be only C^{0,1} conformally compact; these data sets are insufficiently regular to make sense of the shear-free condition.

A third paper, joint with Iva Stavrov Allen, has also been posted.

Smoothly compactifiable shear-free hyperboloidal data is dense in the physical topology.

We show that any polyhomogeneous asymptotically hyperbolic constant-mean-curvature solution to the vacuum Einstein constraint equations can be approximated, arbitrarily closely in Hölder norms determined by the physical metric, by shear-free smoothly conformally compact vacuum initial data.

Posted in Asymptotically hyperbolic, Constraint Equations, Elliptic PDE | Leave a comment

Writing and math

I’m collecting links (in no particular order) to resources for students concerning writing math.

I’ll add to this list when I find more…

Posted in Writing | Leave a comment

Math 235, Spring 2015: Comments on Report 2

After grading (most of) the second round of reports, I have the following comments:

  • While it is necessary to state the methods being used to analyze a system (such as linearizing about an equilibrium), it is not necessary to explain the theory behind the method, or to show all the details. Rather, it is better to tell the reader what method is being used and then to report the result. If the method is long or complicated, you might show the result of an intermediate step.

    For example, if you linearize about an equilibrium and find that it is a saddle point, you might report what the eigenvalues are.
    One consequence is that most of the math work you do won’t actually get typed up.

  • In general, the reports needed to include more interpretation and less computational details.
  • Further, it is important to remember that it is the model that is being interpreted. In this case, the model is an “idealized predator-prey model.” While the introduction can motivate such a model by mentioning specific predatory-prey situations, you are not analyzing a specific situation. Rather, you are analyzing the model. This has two important consequences:
    • The mechanisms which cause/explain behavior are mathematical, not biological. That is, the reason variable x tends to zero is not because some species isn’t getting enough food, but because the differential equation governing x is pushing the value down.
    • If a mathematical statement is made (such as x decreases to zero) this needs to be justified mathematically. A biological interpretation or rationale is neither sufficient nor appropriate.
  • It is important to make good choices of windows / ranges when making plots!
  • Do not make value judgements. Statements like “this value of \alpha is too large” should be avoided. What is more appropriate is something like “if \alpha is large, then x tends to zero very quickly.”
Posted in Differential equations | Leave a comment

Quick tips for using Manipulate in Mathematica

We’ve been using the Manipulate function in class a lot. Here are a couple of quick tips and examples.

The basic idea is to take something you Mathematica to do and add a parameter. For example, suppose you want to plot \cos{(\omega t)} for various values of \omega. The first thing I would do is plot the function with some sample value, such as \omega = 2.

Plot[Cos[2*t], {t, 0, 2 Pi}]

Next we wrap the whole thing in a Manipulate function, with some parameter w.

Manipulate[Plot[Cos[2*t], {t, 0, 2 Pi}], {w, 2, 4}]

Notice that nothing happens, because we haven’t made anything dependent on w.

Now we replace the 2*t with w*t:

Manipulate[Plot[Cos[w*t], {t, 0, 2 Pi}], {w, 2, 4}]

Notice that Mathematica often does a lot of automatic formatting, which can lead to deceiving results. Here is a poorly designed Manipulation:

Manipulate[Plot[w*Cos[w*t], {t, 0, 2 Pi}], {w, 2, 4}]

Here is a better version of that same Manipulation. (Why is it better?)

Manipulate[Plot[w*Cos[w*t], {t, 0, 2 Pi}, PlotRange -> 5], {w, 2, 4}]

Here is another poorly designed example:

Manipulate[Plot[Cos[2 t] Cos[(2 - a) t], {t, 0, 10 Pi}], {a, 0, .5}]

What should you do to make it better?

Finally, here is another fun example:

Manipulate[
 StreamPlot[{x - x^2 - a*x*y, -y + a*x*y}, {x, -.5, 1.5}, {y, -.5, 
   1}], {a, 0, 4}]
Posted in Mathematica, Uncategorized | Leave a comment

Math 235, Spring 2015: Comments on Report 1

Here are some comments on Report 1:

Comments about content

  • In general, it is better to state the assumptions leading to a model right away, even before giving the equation for the model.
  • The general formula for Euler’s method needs to be stated.
  • There seemed to be a lot of confusion about who the target audience is. Write as if writing to another faculty member in our department. Thus you can expect that the audience has reasonable math skills, but may not know the specifics of what’s happening in this course. This means that you need to describe technical terms that are specific to this course, but you don’t need to show details of an algebraic computation.
  • Clear communication is always a goal for technical writing. You don’t need to add lots of fluffy words where simple ones will do, but you do need to include enough words to communicate the ideas at hand. You should strive to strike a balance between words and symbols.
  • The purpose of an abstract, if you include one, is to communicate what the content of the paper is.
  • Captions should include a substantial amount of detail. If someone flipped open your paper and looked only at a picture, they should get a good idea of what’s happening just from reading the caption of the figure.

Comments about LaTeX and Mathematica

  • All math must be between dollar signs, or in a displayed equation. Thus if you have a sentence which references a variable name, that letter should be between dollar signs.
  • In you want large parentheses, use the commands \left( and \right).

    For example:
    \displaystyle \left( 1-\frac{P}{K}\right)
    is written with the command \left( 1-\frac{P}{K}\right)

  • Do give your graphics files names which have spaces in them. This will make LaTex do weird things.

Comments about writing

  • Try to write in the present tense.
  • All content must be part of a complete sentence that is properly punctuated. This includes math content.
  • Avoid starting sentences with math symbols, and avoid having too many commas around math symbols. Here is a a great example sentence:

    The population P is increasing so long as it is less than 7.

    Notice that I added the words “The population” in front of the P, and that I did not put commas around the letter P.

Posted in Math 235 -- Spring 2015 | Leave a comment

Math 235, Spring 2015: Comments on Exam 1, Problem 1

In this problem, students were first asked to verify that y(t) = \frac{1}{5-t} was a solution to the differential equation
\displaystyle \frac{dy}{dt} = y^2
and to plot that function.

  • Remember when verifying that a function is a solution to a differential equation that one cannot assume that the equation is satisfied. The easiest way to proceed is to consider the left side \frac{dy}{dt} independently from the right side y^2, and then to compare the two.
  • When sketching a plot of a function, it is only necessary to plot a few “key” or “relevant” points. For the function y(t) = \frac{1}{5-t}, it is important to note the vertical intercept at y=1/5 and the asymptote at y=5. No other points need to be indicated.

    It is also important to label axes.

  • Remember not to abuse notation! Math symbols have specific meaning; be sure that you intend the meaning they impart!

For the second part of the problem, students were asked to consider another solution \tilde y(t) satisfying the same differential equation, but with initial value \tilde y(0)=2.

I was hoping that students would invoke the fundamental theorem of ODEs. This requires considering the function f(y) = y^2 and stating that both f and f^\prime(y) = 2y are continuous for all values of $y$.

  • Note: These properties of f are independent of the differential equation; they are only properties about the squaring function.

Once we know that FTODE can be applied, then we know that the solution \tilde y and y cannot cross. Therefore, since \tilde y(0)> y(0), we know that \tilde y(t) > y(t) so long as the solution exists.
In particular, it must be the case that \tilde y(t) blows up at some time, no later than t=5.

  • It is important to justify that FTODE is applicable in this setting.
  • It is not enough to describe how \tilde y behaves; one must explain the reasoning that leads to this description (by invoking FTODE).
  • There were several instances of the phrase “grows exponentially” being used improperly. We discussed in class what these words mean in a math setting.

Several students found the exact formula for \tilde y, which was not too hard. Even if one has the exact formula, one still needs to precisely describe the behavior.

Posted in Differential equations, Uncategorized | Leave a comment