% Created 2023-10-07 Sat 14:49
% Intended LaTeX compiler: pdflatex
\documentclass[11pt]{article}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{graphicx}
\usepackage{longtable}
\usepackage{wrapfig}
\usepackage{rotating}
\usepackage[normalem]{ulem}
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{capt-of}
\usepackage{hyperref}
\notindent \notag  \usepackage{amsmath} \usepackage[a4paper,margin=1in,portrait]{geometry}
\author{Elizabeth Hunt}
\date{\today}
\title{HW 03}
\hypersetup{
 pdfauthor={Elizabeth Hunt},
 pdftitle={HW 03},
 pdfkeywords={},
 pdfsubject={},
 pdfcreator={Emacs 28.2 (Org mode 9.7-pre)}, 
 pdflang={English}}
\begin{document}

\maketitle
\setlength\parindent{0pt}

\section{Question One}
\label{sec:org6f2bd27}
\subsection{Three Terms}
\label{sec:orgeb827ff}
\begin{align*}
Si_3(x) &= \int_0^x \frac{s - \frac{s^3}{3!} + \frac{s^5}{5!}}{s} dx \\
&= x - \frac{x^3}{(3!)(3)} + \frac{x^5}{(5!)(5)}
\end{align*}
\subsection{Five Terms}
\label{sec:orge6a15e4}
\begin{align*}
Si_3(x) &= \int_0^x \frac{s - \frac{s^3}{3!} + \frac{s^5}{5!} - \frac{s^7}{7!} + \frac{s^9}{9!}}{s} dx \\
&= x - \frac{x^3}{(3!)(3)} + \frac{x^5}{(5!)(5)} - \frac{x^7}{(7!)(7)} + \frac{s^9}{(9!)(9)}
\end{align*}
\subsection{Ten Terms}
\label{sec:orge87e346}
\begin{align*}
Si_{10}(x) &= \int_0^x \frac{s - \frac{s^3}{3!} + \frac{s^5}{5!} - \frac{s^7}{7!} + \frac{s^9}{9!} - \frac{s^{11}}{11!} + \frac{s^{13}}{13!} - \frac{s^{15}}{15!} + \frac{s^{17}}{17!} - \frac{s^{19}}{19!}}{s} ds \\
&= x - \frac{x^3}{(3!)(3)} + \frac{x^5}{(5!)(5)} - \frac{x^7}{(7!)(7)} + \frac{s^9}{(9!)(9)} - \frac{s^{11}}{(11!)(11)} + \frac{s^{13}}{(13!)(13)} - \frac{s^{15}}{(15!)(15)} \\
&+ \frac{s^{17}}{(17!)(17)} - \frac{s^{19}}{(19!)(19)}
\end{align*}
\section{Question Three}
\label{sec:org6e2f7fc}
For the second term in the difference quotient, we can expand the taylor series centered at x=a:

\begin{equation*}
f(x) = f(a) + f'(a)(x-a) + \frac{f''(a)}{2}(x-a)^2 + \cdots \\
\end{equation*}

Which we substitute into the difference quotient:

\begin{equation*}
\frac{f(a) - f(a - h)}{h} = \frac{f(a) - (f(a) + f'(a)(x-a) + \frac{f''(a)}{2}(x-a)^2 + \cdots)}{h}
\end{equation*}

And subs. \(x=a-h\):

\begin{align*}
\frac{f(a) - (f(a) + f'(a)(x-a) + \frac{f''(a)}{2}(x-a)^2 + \cdots)}{h} &= -f'(a)(-1) + -\frac{1}{2}f''(a)h \\
&= f'(a) - \frac{1}{2}f''(a)h + \cdots \\
\end{align*}

Which we now plug into the initial \(e_{\text{abs}}\):

\begin{align*}
e_{\text{abs}} &= |f'(a) - \frac{f(a) - f(a - h)}{h}| \\
&= |f'(a) - (f'(a) +  -\frac{f''(a)}{2}h + \cdots)| \\
&= |- \frac{1}{2}f''(a)h + \cdots | \\
\end{align*}

With the Taylor Remainder theorem we can absorb the series following the second term:

\begin{equation*}
e_{\text{abs}} = |- \frac{1}{2}f''(a)h + \cdots | = |\frac{1}{2}f''(\xi)h| \leq Ch
\end{equation*}

Thus our error is bounded linearly with \(h\).

\section{Question Four}
\label{sec:orga7d02a2}
For the first term in the difference quotient we know, from the given notes,

\begin{equation*}
f(a+h) = f(a) + f'(a)h + \frac{1}{2}f''(a)h^2 + \frac{1}{6}f'''(a)(h^3)
\end{equation*}

And from some of the work in Question Three,

\begin{equation*}
f(a - h) = f(a) + f'(a)(-h) + \frac{1}{2}f''(a)(-h)^2 + \frac{1}{6}f'''(a)(-h^3)
\end{equation*}

We can substitute immediately into \(e_{\text{abs}} = |f'(a) - (\frac{f(a+h) - f(a-h)}{2h})|\):

\begin{align*}
e_{\text{abs}} &= |f'(a) - \frac{1}{2h}((f(a) + f'(a)h + \frac{1}{2}f''(a)h^2 + \cdots) - (f(a) - f'(a)h + \frac{1}{2}f''(a)h^2 + \cdots))| \\
&= |f'(a) - \frac{1}{2h}(2f'(a)h + \frac{1}{6}f'''(a)h^3 + \cdots)| \\
&= |f'(a) - f'(a) - \frac{1}{12}f'''(a)h^2 + \cdots| \\
&= |-\frac{1}{12}f'''(a)h^2 + \cdots|
\end{align*}

Finally, with the Taylor Remainder theorem we can absorb the series following the third term:

\begin{equation*}
e_{\text{abs}} = |-\frac{1}{12}f'''(\xi)h^2| = |\frac{1}{12}f'''(\xi)h^2| \leq Ch^2
\end{equation*}

Meaning that as \(h\) scales linearly, our error is bounded by \(h^2\) as opposed to linearly as in Question Three.

\section{Question Six}
\label{sec:org7b05811}
\subsection{A}
\label{sec:org8341a77}
\begin{verbatim}
(load "../lizfcm.asd")
(ql:quickload :lizfcm)

(defun f (x)
  (/ (- x 1) (+ x 1)))

(defun fprime (x)
  (/ 2 (expt (+ x 1) 2)))

(let ((domain-values (loop for a from 0 to 2
                           append 
                           (loop for i from 0 to 9
                                 for h = (/ 1.0 (expt 2 i))
                                 collect (list a h)))))
  (lizfcm.utils:table (:headers '("a" "h" "f'" "\\approx f'" "e_{\\text{abs}}")
                       :domain-order (a h)
                       :domain-values domain-values)
    (fprime a)
    (lizfcm.approx:fwd-derivative-at 'f a h)
    (abs (- (fprime a)
            (lizfcm.approx:fwd-derivative-at 'f a h)))))
\end{verbatim}


\section{Question Nine}
\label{sec:orgeb1839f}
\subsection{C}
\label{sec:org5691277}

\begin{verbatim}
(load "../lizfcm.asd")
(ql:quickload :lizfcm)

(defun factorial (n)
  (if (= n 0)
      1
      (* n (factorial (- n 1)))))

(defun taylor-term (n x)
  (/ (* (expt (- 1) n)
        (expt x (+ (* 2 n) 1)))
     (* (factorial n)
        (+ (* 2 n) 1))))

(defun f (x &optional (max-iterations 30))
  (let ((sum 0.0))
    (dotimes (n max-iterations)
      (setq sum (+ sum (taylor-term n x))))
    (* sum (/ 2 (sqrt pi)))))

(defun fprime (x)
  (* (/ 2 (sqrt pi)) (exp (- 0 (* x x)))))

(let ((domain-values (loop for a from 0 to 1
                           append 
                           (loop for i from 0 to 9
                                 for h = (/ 1.0 (expt 2 i))
                                 collect (list a h)))))
  (lizfcm.utils:table (:headers '("a" "h" "f'" "\\approx f'" "e_{\\text{abs}}")
                       :domain-order (a h)
                       :domain-values domain-values)
    (fprime a)
    (lizfcm.approx:central-derivative-at 'f a h)
    (abs (- (fprime a)
            (lizfcm.approx:central-derivative-at 'f a h)))))
\end{verbatim}


\begin{center}
\begin{tabular}{rrrrr}
a & h & f' & \(\approx\) f' & e\textsubscript{\text{abs}}\\[0pt]
0 & 1.0 & 1.1283791670955126d0 & 0.8427006725464232d0 & 0.28567849454908933d0\\[0pt]
0 & 0.5 & 1.1283791670955126d0 & 1.0409997446922075d0 & 0.0873794224033051d0\\[0pt]
0 & 0.25 & 1.1283791670955126d0 & 1.1053055663206806d0 & 0.023073600774832004d0\\[0pt]
0 & 0.125 & 1.1283791670955126d0 & 1.122529655394656d0 & 0.005849511700856569d0\\[0pt]
0 & 0.0625 & 1.1283791670955126d0 & 1.1269116944798618d0 & 0.0014674726156507223d0\\[0pt]
0 & 0.03125 & 1.1283791670955126d0 & 1.1280120131008824d0 & 3.6715399463016496d-4\\[0pt]
0 & 0.015625 & 1.1283791670955126d0 & 1.1282873617826952d0 & 9.180531281738347d-5\\[0pt]
0 & 0.0078125 & 1.1283791670955126d0 & 1.128356232581468d0 & 2.293451404455915d-5\\[0pt]
0 & 0.00390625 & 1.1283791670955126d0 & 1.1283734502811613d0 & 5.71681435124205d-6\\[0pt]
0 & 0.001953125 & 1.1283791670955126d0 & 1.1283777547060847d0 & 1.4123894278572635d-6\\[0pt]
1 & 1.0 & 0.41510750774498784d0 & 0.4976611317561498d0 & 0.08255362401116195d0\\[0pt]
1 & 0.5 & 0.41510750774498784d0 & 0.44560523266293384d0 & 0.030497724917946d0\\[0pt]
1 & 0.25 & 0.41510750774498784d0 & 0.4234889628937013d0 & 0.008381455148713468d0\\[0pt]
1 & 0.125 & 0.41510750774498784d0 & 0.41725265825950153d0 & 0.002145150514513694d0\\[0pt]
1 & 0.0625 & 0.41510750774498784d0 & 0.41564710776310854d0 & 5.396000181207006d-4\\[0pt]
1 & 0.03125 & 0.41510750774498784d0 & 0.4152414157140871d0 & 1.3390796909928948d-4\\[0pt]
1 & 0.015625 & 0.41510750774498784d0 & 0.41514241394084905d0 & 3.490619586121735d-5\\[0pt]
1 & 0.0078125 & 0.41510750774498784d0 & 0.41510582632900395d0 & 1.6814159838896003d-6\\[0pt]
1 & 0.00390625 & 0.41510750774498784d0 & 0.415092913054238d0 & 1.4594690749825112d-5\\[0pt]
1 & 0.001953125 & 0.41510750774498784d0 & 0.4150670865046777d0 & 4.0421240310117845d-5\\[0pt]
\end{tabular}
\end{center}


\section{Question Twelve}
\label{sec:orgc55bfd1}

First we'll place a bound on \(h\); looking at a graph of \(f\) it's pretty obvious from the asymptotes that we don't want to go much further than \(|h| = 2 - \frac{pi}{2}\).

Following similar reasoning as Question Four, we can determine an optimal \(h\) by computing \(e_{\text{abs}}\) for the central difference, but now including a roundoff error for each time we run \(f\)
such that \(|f_{\text{machine}}(x) - f(x)| \le \epsilon_{\text{dblprec}}\) (we'll use double precision numbers, from HW 2 we know \(\epsilon_{\text{dblprec}} \approx 2.22045 (10^{-16})\)).

We'll just assume \(|f_{\text{machine}}(x) - f(x)| = \epsilon_{\text{dblprec}}\) so our new difference quotient becomes:

\begin{align*}
e_{\text{abs}} &= |f'(a) - (\frac{f(a+h) - f(a-h) + 2\epsilon_{\text{dblprec}}}{2h})| \\
&= |\frac{1}{12}f'''(\xi)h^2 + \frac{\epsilon_{\text{dblprec}}}{h}|
\end{align*}

Because we bounded our \(|h| = 2 - \frac{pi}{2}\) we'll find the maximum value of \(f'''\) between \(a - (2 - \frac{\pi}{2})\) and \(a - (2 - \frac{\pi}{3})\). Using \href{https://www.desmos.com/calculator/gen1zpohh2}{desmos} I found this to be -2.

Thus, \(e_{\text{abs}} \leq \frac{1}{6}h^2 + \frac{\epsilon_{\text{dblprec}}}{h}\). Finding the derivative:

\begin{equation*}
e' = \frac{1}{3}h - \frac{\epsilon_{\text{dblprec}}}{h^2}
\end{equation*}

And solving at \(e' = 0\):

\begin{equation*}
\frac{1}{3}h = \frac{\epsilon_{\text{dblprec}}}{h^2} \Rightarrow h^3 = 3\epsilon_{\text{dblprec}} \Rightarrow h = (3\epsilon_{\text{dblprec}})^{1/3}
\end{equation*}

Which is \(\approx (3(2.22045 (10^{-16}))^{\frac{1}{3}} \approx 8.7335 10^{-6}\).
\end{document}