%%%%%%%%%S%%%%%%%%%%%%%%%%% Præambel %%%%%%%%%%%%%%%%%%%%%%%%%%%
\typeout{---------- Preamble start -------------} % vises i log

%Dokumenttype og skriftstrrelsSe sættes:
\documentclass[11pt,a4paper]{article} %fjern draft i færdigt dokument. Brug 11pt eller 12pt

% Oplysninger til article-klassen:
\title{Astropy}
\author{Martin Sparre}
\date{\today }

%linjeafstand:
%\linespread{1.3}

%Laveste niveau i indholdsfortegnelse:
\setcounter{tocdepth}{2}
%Numereringsniveau:
\setcounter{secnumdepth}{2}


%Ligningsnummerering tager formen (section . nr)
%\numberwithin{equation}{section}

%ingen indrykning ved nyt afsnit:
%\parindent=0pt

%Der vises labels i margin - skal kun anvendes i draft
%\usepackage[notref,notcite]{showkeys}

%klikbar indholdsfortegnelse m.m.
%\usepackage{hyperref}

%%%%%%%%%%%%%%%%%%%%%%%%%%% Vigtige Pakker %%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%natbib - en bibTeX-pakke. [Curly] gr, at der bruges {} ved citationer. Natbib skal være før babel
%\usepackage[curly]{natbib}
\usepackage{natbib}

% En række pakker, der skal inkluderes, hvis man skriver på dansk
\usepackage[utf8]{inputenc} %Danske bogstaver kan bruges, ansinew for windoze - latin1 for linux
%\usepackage[danish]{babel} %Kapitler mv. får danske navne
%\usepackage{babel} %Kapitler mv. får danske navne
\usepackage[T1]{fontenc}

%Font:
%\usepackage{lmodern}
\usepackage{palatino}

\usepackage{url}
\usepackage{hyperref}

%\usepackage{sectsty}
%\allsectionsfont{\large\sffamily}

%Marginer:
\usepackage{vmargin}
\setpapersize{A4}
%argumenter:{hleftmargini}{htopmargini}{hrightmargini}{hbottommargini}....
\setmarginsrb{30mm}{20mm}{30mm}{20mm}{12pt}{11mm}{0pt}{11mm}

%fancyhdr - sidehoved og sidefod
\usepackage{fancyhdr}
%Indholdet af sidehoved og sidefod:
\renewcommand{\headheight}{14.5pt} %er obligatorisk v. fancyhdr
\renewcommand{\headrulewidth}{0.5pt}
\renewcommand{\footrulewidth}{0.5pt}
\lhead{Potsdam Uni.} \chead{Advanced Programming} \rhead{}
%\lfoot{bla}
\cfoot{Page \thepage~of \pageref{lastpage}}
%\rfoot{bla}

%En række ams-pakker til matematik:
\usepackage{amsfonts,amsmath,amssymb}

%ntheorem
%\usepackage{amsthm}
\usepackage[amsmath,thmmarks]{ntheorem}
%\theoremsymbol{$\vartriangleleft$}
%\theoremheaderfont{\bfseries\sffamily}
\theorembodyfont{\normalfont}
\newtheorem{Opgave}{Opgave}
%\newtheorem{definition}{Definition}
%\newtheorem{eksempel}{Eksempel}
%\newtheorem{Metode}{Metode}

%inkludering af kildekode:
%\usepackage{verbatim}

%overfull hbox under 3pt ignoreres:
\hfuzz=4pt


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%% Pakker til inkludering af grafik: %%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\newcommand{\msun}{~\mathrm{M}_{\odot}}

\usepackage{graphicx} 

%pakke så floats ikke flyder, indsæt [H] som argument efter \begin{figur}.
\usepackage{float}

%Denne pakke srger for at floats ikke flyder ind i andre afsnit ved kommandoen \FloatBarrier (dette medfrer ikke ny side i modsætning til \clearpage)
\usepackage{placeins}

%labelfonts i captions bliver fede og captionmargin forstrres:
\usepackage{caption}
\captionsetup{font=small,labelfont=bf}
\setlength{\captionmargin}{20pt}

\pagestyle{fancy}
\usepackage{listings}
%%%%%%%%%%%%%%%%%%%%% NYE KOMMANDOER %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%\newcommand{\mc}[1]{\mathcal{#1}}
%\newcommand{\mb}[1]{\mathbf{#1}}

%Horizontale tykke streger via \HRule :
%\newcommand{\HRule}{\rule{\textwidth}{1mm}}

%d'er som bruges ved infinitesimaler:
\DeclareMathOperator{\di}{d\!}

%orddeling
%\hyphenation{Mar-tin}


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%% DOKUMENT START %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\typeout{---------- Dokument start -------------} % vises i log
\begin{document}
\section*{Numerical integration}


Use a midpoint method with $\Delta x = 0.1$ to estimate the following integrals


\begin{itemize}
\item $\int_0^2 x^3 dx$.
\item $\int_2^4 x^3 dx$.
\item $\int_0^\pi \sin x dx$.
\item Repeat the above integrals with $\Delta x = 0.01$. Show that we get closer to the analytical solution.
\item Calculate the integrals using the Simpson's method (scipy.integrate.simps).
\end{itemize}

\noindent{}Here is the code for the midpoint method, as described in the lecture:

\begin{lstlisting}[showstringspaces=false,columns=fullflexible]
import numpy
dx = 0.01
x = numpy.arange(0.0,2.0,dx)
x += dx/2
y = x**2
IntegralValue = numpy.sum(y*dx)
print('Numerical estimate of integral is %8.8g. Analytical solution is %8.8g.'\
%(IntegralValue,1.0/3.0*2.0**3))
\end{lstlisting}


\subsection*{Bonus exercise}

Calculating numerical integrals involving $\infty$ can be tricky at first sight, because it is impossible to numerically create an array extending to $\infty$. In the following we show that doing a substituion will help us evaluating the following integral
\begin{align}
\int_1^\infty \frac{1}{x^2} dx
\end{align}

%\begin{align}
%\int_1^\infty \frac{1}{x^2} dx = \int_{\arctan(1)}^{\arctan(\infty)} \frac{1+x^2}{x^2} du = \int_{\arctan(1)}^{\frac{\pi}{2}} \frac{1+\tan^2(u)}{\tan^2(u)} du
%\end{align}

\begin{itemize}
\item Do the substitution $u=\arctan x$, use the identity $du/dx = 1/(1+x^2)$, and show that the integral can be rewritten as
\begin{align}
\int_1^\infty \frac{1}{x^2} dx = \int_{\arctan(1)}^{\frac{\pi}{2}} \frac{1+\tan^2(u)}{\tan^2(u)} du.\label{eq1}
\end{align}
... this is a well-behaved integral without infinities.
\item Evaluate the integral on right-hand-side of Eq.~\ref{eq1} numerically.
\item Numerically evaluate $ \int_1^\infty \frac{1}{x^3} dx $
\end{itemize}






\def\aj{AJ}
\def\araa{ARA\&A}
\def\apj{ApJ}
\def\apjl{ApJ}
\def\apjs{ApJS}
\def\apss{Ap\&SS}
\def\aap{A\&A}
\def\aapr{A\&A~Rev.}
\def\aaps{A\&AS}
\def\mnras{MNRAS}
\def\nat{Nature}
\def\pasp{PASP}
\def\aplett{Astrophys.~Lett.}
\def\physrep{Physical Reviews}
\def\nar{New A Rev.}

%\footnotesize{
%\bibliographystyle{mnras}
%\bibliography{ref}
%}

%\section*{Acknowledgements}

%Thanks to Chuck Steidel and Denise Schmitz, who provided inspiration for this exercise, \url{http://www.astro.caltech.edu/~ccs/ay124/ps5.pdf}.



\label{lastpage}
\end{document}
