#import "@local/common:0.0.0": *
#import "@local/templates:0.0.0": *
#import "@local/drawing:0.0.0": *

#import "@local/theorem:0.0.0": *
#let thmstyle = thmstyle.with(base: none)
#show: thmrules
#let args = (base: none, titlefmt: it => underline(strong(it)))
#let lm = statestyle("lemma", "Lemma", ..args)
#let def = plainstyle("definition", "Definition", ..args)
#let note = plainstyle("note", "Note", ..args)

#show: math-preamble("Part 6", "Fri, Oct 25")

// #show math.lim: math.limits(math.lim)

#let ee = math.epsilon
#let dd = math.delta

#math-problem("2.1.1")
Prove the continuity of $-|x|$ and $|x+a|$ at any $x_0$.\
#math-solution
To prove the continuity of $-|x|$, let $x_0 in RR$ and $ee > 0$.
Take $dd = ee$. We have
$
  |x-x_0| <= dd ==>
  |-|x|-(-|x_0|)| = abs(|x_0|-|x|) <= |x_0 - x| = |x - x_0| <= dd = ee.
$
To prove the continuity of $|x+a|$, consider $x_0 in RR$ and $ee > 0$.
With $dd = ee$, we write
$
  |x-x_0| <= dd ==>
  abs(|x+a|-|x_0+a|) <= abs((x+a)-(x_0 + a)) = |x - x_0| <= dd = ee.
$

#math-problem("2.1.3") Prove the continuity of $sqrt(x)$ at any $x_0 > 0$.\
#math-solution
Let $x_0 > 0$ and $ee > 0$ be arbitrary.
For some $x > 0$, the difference $abs(sqrt(x) - sqrt(x_0))$ can be expressed in the following way:
$
  abs(sqrt(x) - sqrt(x_0)) = (abs(sqrt(x) - sqrt(x_0)) dot abs(sqrt(x) + sqrt(x_0)))/abs(sqrt(x) + sqrt(x_0)) = abs(x - x_0)/(sqrt(x) + sqrt(x_0)) < abs(x - x_0)/sqrt(x_0).
$
Now we choose $dd = ee sqrt(x_0)$.
For $x$ satisfying $abs(x-x_0) <= dd$, we have
$
  abs(sqrt(x) - sqrt(x_0)) < abs(x - x_0)/sqrt(x_0) <= dd/sqrt(x_0) = ee.
$
 
#math-problem("2.1.5") Prove the continuity at 0:
+ $sin 2x$;
+ $cos 3x$;
+ $sin^2 x$;
+ $cos^2 x$.
#math-solution
+ Let $ee > 0$. Since $sin$ is continuous at 0, there is $dd > 0$ such that
  #centering[
    $abs(x) <= dd$ implies $abs(sin x) <= ee$.
  ]
  Take $dd' = dd/2$. We have
  $
    abs(x) <= dd' ==> abs(2x) <= dd ==> abs(sin 2x) <= ee.
  $
+ Let $ee > 0$. Since $cos$ is continuous at 0, there is $dd > 0$ such that
  #centering[
    $abs(x) <= dd$ implies $abs(cos x - 1) <= ee$.
  ]
  Take $dd' = dd/3$. We have
  $
    abs(x) <= dd' ==> abs(3x) <= dd ==> abs(cos 3x - 1) <= ee.
  $
+ Let $ee > 0$. Take $ee' = min(ee, 1)$. There is a $dd > 0$ such that
  #centering[
    $abs(x) <= dd$ implies $abs(sin x) <= ee'$.
  ]
  Since $abs(sin x) <= 1$ for such $x$, we have
  $
    abs(x) <= dd ==> abs(sin^2 x) <= abs(sin x) <= ee' <= ee.
  $
+ Let $ee > 0$. Take $ee' = ee/2$. There is a $dd > 0$ such that
  #centering[
    #h(1fr) $abs(x) <= dd$ implies $abs(sin x) <= ee'$. #h(1fr)
  ]
  Since $abs(cos x + 1) <= 2$ for all $x$, we have
  $
    abs(x) <= dd ==> abs(cos^2 x - 1) = abs(cos x - 1) dot abs(cos x + 1) <= ee' dot 2 = ee.
  $

#math-problem("2.1.6 (1)")
Prove the following is equivalent to the definition of the continuity of $f(x)$ at $x_0$:
#centering[
  #h(1fr) For any $epsilon > 0$, there is $delta > 0$, such that $abs(x - x_0) < delta$ implies $abs(f(x) - l) < epsilon$. #h(1fr) (1)
]
#math-solution
We prove the equivalence by proving two implications:
/ $==>:$:
  Assuming that $f$ is continuous at $x_0$, we prove condition (1).
  Consider an arbitrary $ee > 0$. Take\ $ee' := ee\/2$.
  Since $f$ is continuous at $x_0$, for our $ee'$ there is $dd' > 0$ such that
  #centering[
    $abs(x - x_0) <= dd'$ implies $abs(f(x) - l) <= ee'$.
  ]
  Now, take $dd := dd'$. We write
  $
    abs(x - x_0) < delta ==> abs(x - x_0) <= dd = dd' ==> abs(f(x) - l) <= ee' ==> abs(f(x) - l) < ee,
  $
  hence we have found $delta$ such that condition (1) holds.

/ $<==:$:
  Assuming condition (1), we prove that $f$ is continuous at $x_0$.
  Consider an arbitrary $ee > 0$. Take\ $ee' := ee$. By condition (1), for this $ee'$ there is $dd' > 0$ such that
  #centering[
    $abs(x - x_0) < dd'$ implies $abs(f(x) - l) < ee'$.
  ]
  We now take $dd := dd'\/2$ and have
  $
    abs(x - x_0) <= dd = dd'/2 ==> abs(x - x_0) < dd' ==> abs(f(x) - l) < ee' = ee ==> abs(f(x) - l) <= ee,
  $
  hence $f$ is continuous at $x_0$.

#math-problem("2.1.7 (2, 4"))
Find the limits: #v(2mm)
2. $limits(lim)_(x -> 1) (x^3-1)/(x-1)$; #v(2mm)
4. $limits(lim)_(x -> 4) (x-4)/(sqrt(x)-2)$.
#math-solution
2.
  $
    (x^3 - 1)/(x-1) = ((x-1)(x^2 + x + 1))/(x-1) = x^2 + x + 1 ->_(x -> 1) 3.
  $
4.
  $
    (x-4)/(sqrt(x)-2) = ((sqrt(x)-2)(sqrt(x)+2))/(sqrt(x)-2) = sqrt(x)+2 ->_(x -> 2) sqrt(4) + 2 = 4.
  $

#math-problem("2.1.10")
Suppose $limits(lim)_(x -> x_0) f(x) = l != 0$.
Prove $limits(lim)_(x -> x_0) 1/f(x) = 1/l$.\
#math-solution
Let $ee > 0$.
First of all, since $l != 0$, there is a sufficiently small $alpha > 0$ such that $0 in.not [l - alpha, l + alpha]$.
Now, define $ee'$ as follows:
$
  ee' = min{alpha,#h(2mm) epsilon abs((l-alpha)l),#h(2mm) epsilon abs((l+alpha)l)}.
$
Since $f(x)$ approaches $l$ when $x -> x_0$, for our $ee'$ there is $dd'$ such that
#centering[
  $0 < abs(x - x_0) <= dd'$ implies $abs(f(x) - l) <= ee'$.
]
When $abs(f(x) - l) <= ee'$, we also have $abs(f(x) - l) <= alpha$ (by the definition of $ee'$), hence $f(x) != 0$. What's more, $f(x)$ lies between $l - alpha$ and $l + alpha$, meaning that
$
  abs(f(x)) >= min{abs(l-alpha), abs(l+alpha)},
$
and
$
  1/abs(f(x)) <= 1/min{abs(l-alpha), abs(l+alpha)} = 1/A,
$
where $A = min{abs(l-alpha), abs(l+alpha)}$. Finally, by the definition of $ee'$ we have that $ee' <= ee dot A dot abs(l)$.

Now, take $dd := dd'$. Our goal is to show
$
  abs(x - x_0) <= dd ==> abs(1/f(x) - 1/l) <= ee.
$
Indeed,
$
  abs(x - x_0) <= dd = dd' ==> abs(f(x) - l) <= ee' ==>\
  ==> abs(1/f(x) - 1/l) = abs((l - f(x))/(f(x) l)) = abs(f(x) - l)/(abs(l) dot abs(f(x))) <= ee'/(abs(l) dot A) <= ee,
$
q.e.d.

#math-problem("2.1.11 (4)")
Show that $(x cos x)/(x+1)$ converges to 0 at 0.\
#math-solution
// When $x$ is close to 0, we can assume that $x+1 > 0$.
First, consider the following convergence:
$
  abs(x/(x+1)) = abs(1 - 1/(x+1)) ->_(x -> 0) abs(1 - 1/1) = 0.
$
Now, since
$
  -abs(x/(x+1)) <= -abs((x cos x)/(x+1)) <= (x cos x)/(x+1) <= abs((x cos x)/(x+1)) <= abs(x/(x+1)),
$
we have that the original function converges to 0 at 0 by the sandwich rule.

#math-problem("2.1.13")
Prove the second part of the order rule.\
#math-solution
Let $limits(lim)_(x -> x_0) f(x) = k$ and $limits(lim)_(x -> x_0) g(x) = l$, such that $k < l$.
Denote the difference $l-k$ by $alpha > 0$.
Take $ee_1 = alpha/3$ and $ee_2 = alpha/3$.
By the definition of limit, we have $dd_1, dd_2 > 0$ such that
$
  0 < abs(x - x_0) <= dd_1 ==> abs(f(x) - k) <= ee_1 = alpha/3,\
  0 < abs(x - x_0) <= dd_2 ==> abs(g(x) - l) <= ee_2 = alpha/3.\
$
Take $dd = min (dd_1, dd_2)$. We have
$
  0 < abs(x - x_0) <= dd ==> f(x) <= k + alpha/3 < l - alpha/3 <= g(x).
$

#math-problem("2.1.15 (2)")
Explain the divergence of $limits(lim)_(x -> 0) 1/x cos 1/x$.\
#math-solution
We will do a proof by contradiction.
Assume that the function *does* converge at 0, and denote its limit by $l$.
Then, choose $ee > 0$ to be arbitrary.
There is a $dd > 0$ such that
$
  0 < abs(x) <= dd ==> abs(1/x cos 1/x - l) <= ee
$
For $0 < x <= dd$, the latter implies
$
  abs(cos 1/x - l x) <= ee x.
$
Choose $x_0$ such that $0 < x_0 <= dd$, $abs(l) x < 1/3$, and $ee x < 1/3$.
Then, for all $0 < x < x_0$ we have
$
  abs(cos 1/x) <= abs(cos 1/x - l x) + abs(l x) <= ee x + abs(l) x < 2/3.
$
Therefore, for all $x > 1/x_0$, we have $abs(cos x) < 2/3$.
This is a contradiction with the fact that $cos x$ is a periodic function.

#math-problem("2.1.16 (4)")
Find the limit, $a, b > 0$:
$
  limits(lim)_(x -> 0) 1/x (sqrt((a+x)/b) - sqrt(a/(b+x))).
$
#math-solution
We transform the expression:
$
  1/x (sqrt((a+x)/b) - sqrt(a/(b+x))).
$

/*
#math-problem("2.1.18")
#math-problem("2.1.19")
#math-problem("2.1.24 (1, 3, 6)")
#math-problem("2.2.1")
#math-problem("2.2.4 (2)")
#math-problem("2.2.5 (1, 4)")
#math-problem("2.2.7")
#math-problem("2.2.10 (3)")
#math-problem("2.2.12 (first extra condition)")
#math-problem("2.2.14 (5)")
#math-problem("2.2.15")
#math-problem("2.2.18")
#math-problem("2.2.21")
#math-problem("2.2.22 (2, 8, 9)")
#math-problem("2.2.23 (2)")
#math-problem("2.2.27")
*/


// Let us first prove the following lemma:
// #lm[
//   Let $f$ and $g$ be functions such that $f$ is continuous at $x_0 in RR$ and $g$ is continuous at $f(x_0)$. Then their composition $g circ f$ is continuous at $x_0$.
// ]
// #pf[
//   Let $ee > 0$.
//   Then, since $g$ is continuous at $f(x_0)$, there is $dd > 0$ such that $
//     abs(x - f(x_0)) <= dd ==> abs(g(x) - g(f(x_0))) <= ee.
//   $
//   For this acquired $dd$, there is $gamma > 0$ such that
//   $
//     abs(x - x_0) <= gamma ==> abs(f(x) - f(x_0)) <= dd.
//   $
//   Finally, we have
//   $
//     abs(x - x_0) <= gamma ==> abs(f(x) - f(x_0)) <= dd ==> abs(g(f(x)) - f(g(x_0))) <= ee,
//   $
//   meaning that $(g circ f)(x) = g(f(x))$ is continuous at $x_0$.
// ]
// Now, consider a constant $k > 0$. We shall prove that $f(x) = k x$ is continuous at any $x_0$. Indeed, for every $ee > 0$ choose $dd = ee/k$. We have
// $
//   abs(x - x_0) <= dd ==> abs(x - x_0) <= ee/k ==> abs(k x - k x_0) <= ee.
// $
// Finally, we easily see that $sin 2x$ is continuous as the composition of $f(x) = 2x$ and $g(x) = sin x$, and so is $cos 3x$. The function $sin^2 x$ is continuous at 0 as the composition of $f(x) = sin x$ and $g(x) = x^2$, and $cos^2 x$ is continuous at 0 as the composition of $f(x) = cos x$ and $g(x) = x^2$.
