\documentclass[a4,12pt]{article}
\usepackage[finnish]{babel}

%\usepackage{graphicx}
%\usepackage[dvips]{graphicx}
%\usepackage{subfigure}
%\usepackage[dvips]{epsfig}
%\usepackage[applemac]{inputenc}%mac
\usepackage[utf8]{inputenc}%unix
\usepackage{times}
\usepackage[framed,autolinebreaks,useliterate]{mcode}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{amssymb}
%\usepackage{psfrag}
\usepackage[RGB]{aaltologo}

%\usepackage{tikz}

\usepackage[exercise]{amsthm}
\theoremstyle{exercise}
\newtheorem{teht}{Tuntitehtävä}
\newtheorem{kirj}[teht]{Kotitehtävä}
\newtheorem{crossed}{Esitehtävä}
\newtheorem{calculated}[crossed]{Tehtävä}
\newtheorem{graded}[crossed]{Kotitehtävä}
\newtheorem{review}{Muut}
\newtheorem{demo}{Demo}


\newcommand{\norm}[1]{\Vert #1 \Vert}

\renewcommand{\Re}{\mbox{Re}}
\renewcommand{\Im}{\mbox{Im}}
\newcommand{\mbf}[1]{\mathbf{#1}}
\newcommand{\R}{\mathbb{R}}
\newcommand{\N}{\mathbb{N}}
\newcommand{\Z}{\mathbb{Z}}
\newcommand{\Q}{\mathbb{Q}}
\newcommand{\C}{\mathbb{C}}
\newcommand{\ds}{\displaystyle}

\newtheorem{theorem}{Theorem}[section]
\newtheorem{definition}{Definition}[section]
\newtheorem{lemma}{Lemma}[section]
\newtheorem{remark}{Remark}[section]
\newtheorem{example}{Example}[section]

\renewcommand{\vec}[1]{ \boldsymbol{#1}}
\newcommand{\range}[1]{ \mbox{range}(#1)}
%\renewcommand{\null}[1]{ \mbox{null}(#1)}
\newcommand{\Rnn}{ \mathbb{R}^{n \times n}}
\newcommand{\Rn}{ \mathbb{R}^n}
\newcommand{\Cnn}{ \mathbb{C}^{n \times n}}
\newcommand{\Cn}{ \mathbb{C}^n}
\newcommand{\Rtwo}{ \mathbb{R}^2}
\usepackage[paper=a4paper,textwidth=16.5cm,bottom=40mm]{geometry}
%\\usepackage[paper=a4paper,textwidth=16.5cm]{geometry}

\usepackage{fancyhdr}
\usepackage{lastpage}
\usepackage{datetime}
\usepackage{hyperref}

\pagestyle{fancy}
\fancyhead{}
\fancyhead[LO,RE]{MATLAB II, 2018}
\fancyhead[RO,LE]{Apiola \& Kuortti}
\fancyfoot{}
\fancyfoot[RO,LE]{\thepage/\pageref{LastPage}}
\fancyfoot[LO,LE]{Tiedostoa viimeksi muokattu: \today \mbox{ } \currenttime}

\renewcommand\headrulewidth{1pt}
\renewcommand\footrulewidth{1pt}

\mathchardef\lscr="7160
\renewcommand{\l}{\lscr}

\newcommand{\exerline}{
\vspace*{.15cm}
\noindent \rule{\textwidth}{1pt}
\vspace*{.2cm}
}

%
\setlength{\parindent}{0 pt}

\begin{document}


\begin{minipage}[t][1.5cm][b]{.2\textwidth}
\AaltoLogoRandomLarge{0.7}
\end{minipage}
\begin{minipage}[t][.88cm][b]{.75\textwidth}
\begin{flushleft}
\Large{\textbf{MATLAB II}} \\
\vspace*{.25cm}
\Large{\textbf{Exercise for Lecture 4}}
\end{flushleft}
\end{minipage}
\\
\exerline


This exercise involves the fourth lecture of the minicourse Matlab
continuation. The topic is \textbf{Parallel computing}.

Once you have solved the problems, please send \textbf{published pdf}
and your \textbf{source code} to\\ \textit{heikki.apiola@aalto.fi}.

The deadline for the return of the exercise is 15.3.2018 

Feel free to send email for questions.\\



%\begin{enumerate}
(a) Study the m-file: \href{Globalmin.m}{Globalmin.m}.
Do the ``Task-part'' with the given function
$$f(x)=x\,\sin x + x\,\cos 2x$$ on a slightly larger interval $[-2,14]$.

(b) Study the m-file: \href{minmax2d.m}{minmax2d.m}.
Function to be minimized:
$$x\exp(-(x^2+y^2))+(x^2+y^2)/20.$$
Change \mcode{for} to \mcode{parfor}, especially on Triton.
Compare timings with ``for'' and with ``parfor''.

(c) Do similar study for ``Rosenbrock's banana-function'':
$$f(x,y)=(a-x)^2+b(y-x^2)^2,$$

You can take the usual values: $a=1,b=100$

Provide explanations, observations,etc.





%\end{enumerate}
\end{document}



(a) Plot the direction field for the equation
\[
   y' = 3\,\sin y + y -2
  \]
on a rectangle $-2 \leq t \leq 10, -1 \leq y \leq 6$.

\begin{itemize}
\item
Looking at the dirfield, how can you see that the equation is autonomous?
\item
Find approximatively the constant solutions from the dirfield figure,
then plot the RHS, and finally
use \mcode{fzero} to find more accurate values.
\item
Use \mcode{ode45} to find solutions below, between and above the 
constant solutions. You are welcome to complete the picture using our 
interactive way to draw solution curves.
\item
Determine the stability/nonstability of the critical points (enough to look at the fieldplot).
\item (Voluntary) Use \mcode{dsolve} to find the symbolic solution.
\end{itemize}
(b) The same (with ``non'' at suitable places) wrt, to
\[
   y' = y^2 - t\,y
  \]
 $-2 \leq t \leq 2, -4 \leq y \leq 4$.  (Can adjust)
** See Coombes p. 60 **


\item 

[Moler p. 39, problem 7.15] \\
A classical model in mathematical ecology is the Lotka–Volterra predator-
prey model. Consider a simple ecosystem consisting of rabbits that have an
infinite supply of food and foxes that prey on the rabbits for their food. This
is modeled by a pair of nonlinear, first-order differential equations:
$$
\begin{cases}
r'=2r -\alpha\,r\,f, r(0)=r_0\\
f'=-f+\alpha\,r\,f, f(0)=f_0
\end{cases}
$$
$r(t)=$ Nr. of rabbits, $f(t)=$ Nr. of foxes, $\alpha>0$.\\
It turns out that
the solutions are always periodic, with a period $t_p$ that depends on the initial
conditions.

(a) Compute the solution with $r_0=300, f_0=150,\alpha=0.01$.
You should find that $t_p$ is close to 5. Make two plots:\\
$r(t)$ and $f(t)$ on time-axis and phase plane: axes $(r,f)$.

(b) Compute and plot the solution with $r_0=15, f_0=22,\alpha=0.01$
You should find that $t_p$ is close to 6.62.

(c) $r_0=102, f_0=198,\alpha=0.01$. Determine the period $t_p$ by trial and 
error or by event handling, or ...

(d) The point $(1/\alpha,2/\alpha)$ is a stable equilibrium point.
If the populations have these initial values, they do not change. If the initial populations are close to these values, they do not change very much ...
** linearization, forget ***

\item

[7.16]\\
Many modifications of the Lotka–Volterra predator-prey model (see previous
problem) have been proposed to more accurately reflect what happens in
nature. For example, the number of rabbits can be prevented from growing
indefinitely by changing the first equation as follows:
$$
\begin{cases}
r'=2(1-\frac{r}{R})r -\alpha\,r\,f, r(0)=r_0\\
f'=-f+\alpha\,r\,f, f(0)=f_0
\end{cases}
$$
...Consequently, the number of rabbits can never exceed $R$.

.. compare ...
\item
Apollo: from the book: Forsythe-Malcolm-Moler: Computer methods for mathematical computations, Prentice Hall 1977 
(Restricted 3-body-problem)\\
(Reminds me to the movie: ``Hidden figures''\\

The following differential equations describe the motion of a body in orbit about 2 much heavier bodies. An example: Apollo capsule in an earth-moon orbit
The 3 bodies determine a plane in space. The coordinate system: x-axis 
is the line through moon and earth. The origin is at the center of mass of the 2 heavy bodies. If $\mu=\frac{m}{M}$ is the ratio of the masses, then the 
moon and the earth are located at $(1-\mu,0)$ and $-\mu$, as we take the distance between the bodies as unity. The Apollo is assumed to habve a negligible mass
compared to the other 2.\\
Let $(x(t),y(t))$ be the position of Apollo. The Newton laws of motion are:
%^^^^^
$$
\begin{cases} 
x''=2y'+x-\frac{\lambda(x+\mu)}{r_1^3}-\frac{\mu(x-\lambda)}{r_2^3}\\
y'' = -2x' + y -\frac{\lambda y}{r_1^3}-\frac{\mu\,y}{r_2^3}
\end{cases} 
$$
$\mu=\frac{1}{82.45}$, $\lambda=1-\mu$

$r_1=((x+\mu)^2 + y^2)^{1/2}$, $r_2=((x-\lambda)^2 + y^2)^{1/2}$

The first derivatives come from the rotating coordinate system.

One type of problems is to study periodic solutions. It is known that the initial conditions
$x(0)=1.2, x'(0)=0, y(0)=0, y'(0)=-1.04935751$

lead to a solution which is periodic with period $T=6.19216933$.

These conditions mean that Apollo starts at the far side of the moon at altitude about 0.2 
 times the earth-moon distance, The resulting orbit brings Apollo in fairly 
close to the earth, out in a big loop on the opposite side of the earth 
from the moon, back in close to the earth again, and finally back to its
original position on the far side of the moon.

(a) Compute and plot this solution using \mcode{ode45} and verify 
periodicity.\\
(b) How close does Apollo come to the earth's surface\\
(c) Determine the stepsizes of the T-vector, what is the minimum and where.\\
(d) Try different values of the error tolerances.\\
(e) Try different starting values\\
(e) Animate the flight.\\
(g) Find something else, and have fun!


\begin{center}
\includegraphics[height=8cm, width=10cm]{Apollopic.png}
\end{center}



\end{enumerate}
\end{document}





