Excercise sheet 5

Revision Excercises (before attacking differential equations)

One

1.	Construct a table of series of the cosine function, of different orders, at least a dozen of them. Plot them in the same picture, with different colours. Figure out how you can set the colours as a table.
2.	Experiment with the Select function. Construct a table of random numbers from the interval [1, 6], and pick out all the sixes therefrom.
3.	Get familiar with the Cases function. Construct the set {x, (x^2), (e^y), x + y, 5  x, (x^((x^x))), sin x, 
            log x + cos x}or make your own, with a lot of different kind of expressions. Then pick out all expressions which are in the form of (x^y). Also try out the Levelspec option.

Solutions

The Plot function might appear a bit problematic sometimes. It is due to the fact that it evaluates its arguments in a non-standard way. If everything should be OK, but Plot still complains, try using the function Evaluate.

A = 
        Table[Series[Cos[x], {x, 0, i}], {i, 2, 30, 2}] // 
          Normal;

    Plot[Evaluate[Prepend[A, Cos[x]]], {x, -5 Pi, 5  Pi}, 
      PlotStyle -> Table[Hue[i/30], {i, 2, 30, 2}]]

The criterion for the Select function is conveniently presented in the form of a pure function. The #-symbol stands for the argument of the pure function, and the &-sign barely tells Mathematica that we're dealing with a pure function. The selection of all sixes from a random table is of course something bloody stupid.

Table[
      Random[Integer, {1, 6}], {i, 10}]

    Select[%, # == 6 &]

    Length[%]

See the help file for Cases for more information.

thelist = {x, x^2, Exp[y], x + y, 
        5  x, x^(x^x), Sin[x], Log[x] + Cos[x]}

    Cases[thelist, a_^b_]

    Cases[thelist, a_^b_, {1, 2}]

Two

1.	Experiment with the options of Plot. Find out how you can label the axes, change the font and font size. Then change the background color.
2.	Plot the trajectory (x(t), y(t)) = (cos t, sin t) in (x, y)-plane. Then plot the trajectory (x(t), y(t), z(t)) = (t, cos t, 
          sin t) in (x, y, z)-plane. Find out 
how you can change the viewpoint.

Solutions

Options[Plot]

    Plot[{x^2, Cos[x]}, {x, -Pi, Pi}, 
      PlotStyle -> {{Hue[1/3], Thickness[0.003]}, {Hue[2/3], 
            Thickness[0.005], Dashing[{0.01}]}}, AxesLabel -> {x, y}, 
      AxesStyle -> {Hue[0]}, Background -> GrayLevel[0.9], 
      TextStyle -> {FontSize -> 15}, 
      ImageSize -> 500]

ParametricPlot[{Cos[t], 
        Sin[t]}, {t, 0, 2  Pi}]

    ParametricPlot3D[{t, 5  Cos[t], 5  Sin[t]}, {t, 0, 4  Pi}, 
      ViewPoint -> {1, 1, 1}]

Three!

This excercise should be made with a computer with speakers.
1.	Check the help file for the function Play.

Solutions

Play[
      Sin[1000/t], {t, -1, 1}]

Differential Equations

First

Solve the following linear first order differential equations
3.1.	y' (x) + 3  y(x) = 2,  y(0) = 1
3.2.	x' (t) + sin(t) x(t) = 0,  
      x(0) = -1. Also plot the solution x(t) for 0 < t < 3
3.3.	f' (x) + p(x) f(x) = q(x), find the general solution.

Solutions

DSolve[{y'[x] + 
            3  y[x] == 2, y[0] == 1}, y[x], x]

DSolve[{x'[t] + 
            Sin[t] x[t] == 0, x[0] == 1}, x[t], 
      t]

    Plot[x[t] /. %, {t, -Pi, Pi}]

In the next one, the symbols K$xxxx stand for variables.

DSolve[f'[x] + 
          p[x] f[x] == q[x], f[x], x]

Second

Solve the following second-order linear differential equations
3.4.	y'' (t) + 3  y' (t) - 2  y(t) = 1,  
      y' (0) = 1,  y(0) = 0, plot the real part of the solution.
3.5.	f'' (x) + x f(x) = 0,  f' (0) = 1,  
      f(0) = 2, solve both numerically and analytically. 
3.6.	x'' (t) + (a - 2  q cos(2  t)) x(
              t) = 0.

Solutions

In 3.4., the solution will be real. The only source of complexity are the boundary conditions.

DSolve[{y''[t] + 
            3  y'[t] - 2  y[t] == 1, y'[0] == 1, 
        y[0] == 0}, y[t], t]

    Plot[y[t] /. %, {t, -1, 2}]

DSolve[{f''[x] + 
            x f[x] == 0, f'[0] == 1, f[0] == 2}, f[x], 
      x]

    NDSolve[{f''[x] + x f[x] == 0, f'[0] == 1, 
        f[0] == 2}, f[x], {x, -3, 6}]

    Plot[f[x] /. %, {x, -3, 6}]

DSolve[x''[
            t] + (a - 2  q Cos[2  t]) x[t] == 0, x[t], 
      t]

Third

Solve the following nonlinear differential equations. If DSolve does not give you an analytic solution, find the solution numerically.
3.7.	y' (t) + ((y(t))^2) = 0,  y(0) = 2, plot the solutions for 0 < t < 10
3.8.	θ'' (t) + (1/10) θ' (t) + 
          2  sin(θ(t)) = 3  cos(2  t), plot the solution for 0 < t < 10  π

Solutions

Nonlinearity is a typical sign of unsolvability, but here DSolve prevails.

DSolve[{y'[t] + 
            y[t]^2 == 0, y[0] == 2}, y[t], t]

    Plot[y[t] /. %, {t, 0, 10}]

In the next one, DSolve fails. We will solve it using NDSolve, but to do that, we must supply the problem with some initial conditions. Remember to give the interval to NDSolve!

DSolve[{θ''[
              t] + (1/10) θ'[t] + 2  Sin[θ[t]] == 
          3  Cos[2  t]}, θ[t], t]

    NDSolve[{θ''[t] + (1/10) θ'[t] + 
            2  Sin[θ[t]] == 3  Cos[2  t], θ[0] == 
          1, θ'[0] == 0}, θ[t], {t, 0, 
        10  Pi}]

    Plot[θ[t] /. %, {t, 0, 10  Pi}]

Fourth

3.9. Additional extra-fun excercise! Solve the following nonlinear set of differential equations (known as the Lorenz equations):
	x' (t) = σ x(t) (y(t) - x(t))
y' (t) = ρ x(t) - 
          y(t) - x(t) z(t)
z' (t) = x(t) y(t) - β z(
              t)		σ = 10.0
ρ = 28.0
β = 2.6667
You can try to plot the solution as a three dimensional curve using ParametricPlot3D.

Solutions

Solving a set of differential equations with DSolve is just as easy as solving a set of ordinary equations with Solve. Here, again, DSolve fails, and NDSolve is needed. Note that the number of required initial conditions is 3, which is the order of this set of equations. Note also that {x(0) = 0,  y(0) = 0, z(0) = 0} is not a good set of initial conditions, because these force the derivatives to zero and the graph reduces to a point.

σ = 10.0;  ρ = 
      28.0;  β = 2.6667;

    DSolve[{x'[t] == σ x[t] (y[t] - x[t]), y'[
            t] == ρ x[t] - y[t] - x[t] z[t], z'[t] == 
          x[t] y[t] - β z[t]}, {x[t], y[t], z[t]}, 
      t]

    NDSolve[{x'[t] == σ x[t] (y[t] - x[t]), y'[
            t] == ρ x[t] - y[t] - x[t] z[t], z'[t] == 
          x[t] y[t] - β z[t], x[0] == 1, y[0] == 1, 
        z[0] == 1}, {x[t], y[t], z[t]}, {t, 0, 10}]

    ParametricPlot3D[{x[t], y[t], z[t]} /. %[[1]], {t, 0, 10}, 
      PlotPoints -> 300]