% -*- latex -*-
\documentclass[letter,12pt]{article}
%---------------------------------------------------------------
%\usepackage{listings}
\usepackage[framed]{matlab-prettifier}
\usepackage{color}
\usepackage{fullpage}
\usepackage{enumitem} % control over itemize, list environment
%---------------------------------------------------------------


\begin{document}
\newcommand{\hwtitle}[1]{%
	\begin{center}
		\Large  \bf Homework #1%
	\end{center}%
}

\newcommand{\mat}[1]{% matlab code
%{\color{blue}\texttt{#1}}%
	% this one does not work inside of captions
	{\lstinline[style = Matlab-editor, basicstyle = \mlttfamily, columns=fixed]!#1!}%
	% this one work but style is different
	%{\lstinline[columns=fixed]!#1!}%
}
%---------------------------------------------------------------
\newcommand{\problem}[1]{% homework problem
	%{\item  \bf #1%}
\item {\bf #1{}:}% the next blank line is important

}

\newcommand{\Matlab}[0]{%
	MATLAB%
}

\newenvironment{homework}[2]%
{%
	\hwtitle{#1}
	#2% Prerequisites

	\begin{enumerate}[
		label={\bf Problem\ \arabic*}, 
		leftmargin=0pt,
		start,
		labelindent=0pt,
		widest=20, align=left, itemindent=!
		]
}%
{%
	\end{enumerate}
}%

%---------------------------------------------------------------
\begin{homework}{09}{
%Prerequisites:
%
%General comments: 
%\begin{itemize}
	%\item Do not forget to run some test cases. 
%\end{itemize}
}

\problem{(5 points)}
%---------------------------------------------------------------
The model for a more realistic pendulum. 
Solve  numerically (using the built-in \mat{ode45} solver) the following
physical problem of a pendulum's motions
\begin{eqnarray*}
	\theta''(t) = - \frac{g}{L} \sin(\theta)
\end{eqnarray*}
where $g$ is acceleration due to gravity ($g$=9.8 m/s$^2$), $L=1$ is the
length of the pendulum, and $\theta$ is the  angular deviation of the pendulum
from the vertical.

Assuming that the initial angular velocity ($\beta$) is zero, i.e., 
$\beta(0)=\theta'(0)=0$,
solve this problem (i.e., plot  $\theta(t)$ and $\beta(t)$) for two values of the
initial deflection $\theta(0)=\pi/10$ and $\theta(0)=\pi/3$. Both solutions
must be presented on the same plot.
Make final time large enough to include at least 10 periods.
Show that the period of the pendulum depends on the initial deflection.
Does it takes longer to make one swing with a larger or smaller initial
deflection?

\problem{(5 points)}
%---------------------------------------------------------------
Implement the forth-order Runge-Kutta method (RK4) according to the recipe
outlined in the slides for the lecture 20. It should be input compatible to the
home-made Euler's implementation \mat{odeeuler.m}. Compare the solution of
the above problem done with your own RK4 implementation to the built-in \mat{ode45}
solver.



\problem{(5 points)}
%---------------------------------------------------------------
Have a look at the predator and prey model (the \mat{ode_predator_prey_model.m}
file provided with the lecture 20 notes).

Find the non trivial solution (i.e. $x_0 \ne 0$ and $y_0 \ne 0$) for which
population of wolves and rabbits is independent of time (i.e.
$dx/dt=dy/dt=0$). You should get a system of two linear equations which is
super simple. However, I ask you to solve it using  \Matlab{}'s numerical solver
which we discussed during the lecture 21, i.e., form the matrix A
and the column vector
B, and find x which satisfies $A \times x=B$.  Note: use constants a, b, c, and d provided
in the \mat{ode_predator_prey_model.m} file.

So, we see that it possible to have stable populations (or economy without
ups and downs) but you need to be smart about initial conditions.

What is the expected shape of the plot of the  wolves population vs  rabbit's
one with calculated above initial conditions? 
Plot it. 


\problem{(5 points)}
%---------------------------------------------------------------
It is possible to draw a parabola through any 3 points in a plane.
Using \Matlab{}' linear equations solver find coefficients $a, b$ and $c$ for parabola
described as
$y=a x^2 +b x +c$. This parabola passes through the points $p_1=(-10,10), p_2=(-2,12)$,
and $p_3=(12,10)$.

\problem{(5 points)}
%---------------------------------------------------------------
Using \Matlab{}'s \mat{interp1} with \mat{spline} method, find where
the interpolation line crosses $y=0$ level. The interpolation is done over the
following points [(x,y) notation]: (2,10), (3,8), (4,4), (5,1), (6,-2).

Would it be wise  to search crossing with $x=0$ line using the above data?
Why is it so?

\end{homework}
%---------------------------------------------------------------
\end{document}
%---------------------------------------------------------------
