This repository was archived by the owner on Jul 24, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 290
Expand file tree
/
Copy pathtaylor.lean
More file actions
393 lines (349 loc) · 18.2 KB
/
taylor.lean
File metadata and controls
393 lines (349 loc) · 18.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
/-
Copyright (c) 2022 Moritz Doll. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Moritz Doll
-/
import analysis.calculus.iterated_deriv
import analysis.calculus.mean_value
import data.polynomial.module
/-!
# Taylor's theorem
> THIS FILE IS SYNCHRONIZED WITH MATHLIB4.
> Any changes to this file require a corresponding PR to mathlib4.
This file defines the Taylor polynomial of a real function `f : ℝ → E`,
where `E` is a normed vector space over `ℝ` and proves Taylor's theorem,
which states that if `f` is sufficiently smooth, then
`f` can be approximated by the Taylor polynomial up to an explicit error term.
## Main definitions
* `taylor_coeff_within`: the Taylor coefficient using `deriv_within`
* `taylor_within`: the Taylor polynomial using `deriv_within`
## Main statements
* `taylor_mean_remainder`: Taylor's theorem with the general form of the remainder term
* `taylor_mean_remainder_lagrange`: Taylor's theorem with the Lagrange remainder
* `taylor_mean_remainder_cauchy`: Taylor's theorem with the Cauchy remainder
* `exists_taylor_mean_remainder_bound`: Taylor's theorem for vector valued functions with a
polynomial bound on the remainder
## TODO
* the Peano form of the remainder
* the integral form of the remainder
* Generalization to higher dimensions
## Tags
Taylor polynomial, Taylor's theorem
-/
open_locale big_operators interval topology nat
open set
variables {𝕜 E F : Type*}
variables [normed_add_comm_group E] [normed_space ℝ E]
/-- The `k`th coefficient of the Taylor polynomial. -/
noncomputable
def taylor_coeff_within (f : ℝ → E) (k : ℕ) (s : set ℝ) (x₀ : ℝ) : E :=
(k! : ℝ)⁻¹ • (iterated_deriv_within k f s x₀)
/-- The Taylor polynomial with derivatives inside of a set `s`.
The Taylor polynomial is given by
$$∑_{k=0}^n \frac{(x - x₀)^k}{k!} f^{(k)}(x₀),$$
where $f^{(k)}(x₀)$ denotes the iterated derivative in the set `s`. -/
noncomputable
def taylor_within (f : ℝ → E) (n : ℕ) (s : set ℝ) (x₀ : ℝ) : polynomial_module ℝ E :=
(finset.range (n+1)).sum (λ k,
polynomial_module.comp (polynomial.X - polynomial.C x₀)
(polynomial_module.single ℝ k (taylor_coeff_within f k s x₀)))
/-- The Taylor polynomial with derivatives inside of a set `s` considered as a function `ℝ → E`-/
noncomputable
def taylor_within_eval (f : ℝ → E) (n : ℕ) (s : set ℝ) (x₀ x : ℝ) : E :=
polynomial_module.eval x (taylor_within f n s x₀)
lemma taylor_within_succ (f : ℝ → E) (n : ℕ) (s : set ℝ) (x₀ : ℝ) :
taylor_within f (n+1) s x₀ = taylor_within f n s x₀
+ polynomial_module.comp (polynomial.X - polynomial.C x₀)
(polynomial_module.single ℝ (n+1) (taylor_coeff_within f (n+1) s x₀)) :=
begin
dunfold taylor_within,
rw finset.sum_range_succ,
end
@[simp] lemma taylor_within_eval_succ (f : ℝ → E) (n : ℕ) (s : set ℝ) (x₀ x : ℝ) :
taylor_within_eval f (n+1) s x₀ x = taylor_within_eval f n s x₀ x
+ (((n + 1 : ℝ) * n!)⁻¹ * (x - x₀)^(n+1)) • iterated_deriv_within (n + 1) f s x₀ :=
begin
simp_rw [taylor_within_eval, taylor_within_succ, linear_map.map_add, polynomial_module.comp_eval],
congr,
simp only [polynomial.eval_sub, polynomial.eval_X, polynomial.eval_C,
polynomial_module.eval_single, mul_inv_rev],
dunfold taylor_coeff_within,
rw [←mul_smul, mul_comm, nat.factorial_succ, nat.cast_mul, nat.cast_add, nat.cast_one,
mul_inv_rev],
end
/-- The Taylor polynomial of order zero evaluates to `f x`. -/
@[simp] lemma taylor_within_zero_eval (f : ℝ → E) (s : set ℝ) (x₀ x : ℝ) :
taylor_within_eval f 0 s x₀ x = f x₀ :=
begin
dunfold taylor_within_eval,
dunfold taylor_within,
dunfold taylor_coeff_within,
simp,
end
/-- Evaluating the Taylor polynomial at `x = x₀` yields `f x`. -/
@[simp] lemma taylor_within_eval_self (f : ℝ → E) (n : ℕ) (s : set ℝ) (x₀ : ℝ) :
taylor_within_eval f n s x₀ x₀ = f x₀ :=
begin
induction n with k hk,
{ exact taylor_within_zero_eval _ _ _ _},
simp [hk]
end
lemma taylor_within_apply (f : ℝ → E) (n : ℕ) (s : set ℝ) (x₀ x : ℝ) :
taylor_within_eval f n s x₀ x = ∑ k in finset.range (n+1),
((k! : ℝ)⁻¹ * (x - x₀)^k) • iterated_deriv_within k f s x₀ :=
begin
induction n with k hk,
{ simp },
rw [taylor_within_eval_succ, finset.sum_range_succ, hk],
simp,
end
/-- If `f` is `n` times continuous differentiable on a set `s`, then the Taylor polynomial
`taylor_within_eval f n s x₀ x` is continuous in `x₀`. -/
lemma continuous_on_taylor_within_eval {f : ℝ → E} {x : ℝ} {n : ℕ} {s : set ℝ}
(hs : unique_diff_on ℝ s) (hf : cont_diff_on ℝ n f s) :
continuous_on (λ t, taylor_within_eval f n s t x) s :=
begin
simp_rw taylor_within_apply,
refine continuous_on_finset_sum (finset.range (n+1)) (λ i hi, _),
refine (continuous_on_const.mul ((continuous_on_const.sub continuous_on_id).pow _)).smul _,
rw cont_diff_on_iff_continuous_on_differentiable_on_deriv hs at hf,
cases hf,
specialize hf_left i,
simp only [finset.mem_range] at hi,
refine (hf_left _),
simp only [with_top.coe_le_coe],
exact nat.lt_succ_iff.mp hi,
end
/-- Helper lemma for calculating the derivative of the monomial that appears in Taylor expansions.-/
lemma monomial_has_deriv_aux (t x : ℝ) (n : ℕ) :
has_deriv_at (λ y, (x - y)^(n+1)) (-(n+1) * (x - t)^n) t :=
begin
simp_rw sub_eq_neg_add,
rw [←neg_one_mul, mul_comm (-1 : ℝ), mul_assoc, mul_comm (-1 : ℝ), ←mul_assoc],
convert @has_deriv_at.pow _ _ _ _ _ (n+1) ((has_deriv_at_id t).neg.add_const x),
simp only [nat.cast_add, nat.cast_one],
end
lemma has_deriv_within_at_taylor_coeff_within {f : ℝ → E} {x y : ℝ} {k : ℕ} {s t : set ℝ}
(ht : unique_diff_within_at ℝ t y) (hs : s ∈ 𝓝[t] y)
(hf : differentiable_within_at ℝ (iterated_deriv_within (k+1) f s) s y) :
has_deriv_within_at (λ z,
(((k+1 : ℝ) * k!)⁻¹ * (x - z)^(k+1)) • iterated_deriv_within (k+1) f s z)
((((k+1 : ℝ) * k!)⁻¹ * (x - y)^(k+1)) • iterated_deriv_within (k+2) f s y -
((k! : ℝ)⁻¹ * (x - y)^k) • iterated_deriv_within (k+1) f s y) t y :=
begin
replace hf : has_deriv_within_at (iterated_deriv_within (k+1) f s)
(iterated_deriv_within (k+2) f s y) t y :=
begin
convert (hf.mono_of_mem hs).has_deriv_within_at,
rw iterated_deriv_within_succ (ht.mono_nhds (nhds_within_le_iff.mpr hs)),
exact (deriv_within_of_mem hs ht hf).symm
end,
have : has_deriv_within_at (λ t, (((k+1 : ℝ) * k!)⁻¹ * (x - t)^(k+1)))
(-((k! : ℝ)⁻¹ * (x - y)^k)) t y,
{ -- Commuting the factors:
have : (-((k! : ℝ)⁻¹ * (x - y)^k)) = (((k+1 : ℝ) * k!)⁻¹ * (-(k+1) *(x - y)^k)),
{ field_simp [nat.cast_add_one_ne_zero k, nat.factorial_ne_zero k], ring_nf },
rw this,
exact (monomial_has_deriv_aux y x _).has_deriv_within_at.const_mul _ },
convert this.smul hf,
field_simp [nat.cast_add_one_ne_zero k, nat.factorial_ne_zero k],
rw [neg_div, neg_smul, sub_eq_add_neg],
end
/-- Calculate the derivative of the Taylor polynomial with respect to `x₀`.
Version for arbitrary sets -/
lemma has_deriv_within_at_taylor_within_eval {f : ℝ → E} {x y : ℝ} {n : ℕ} {s s' : set ℝ}
(hs'_unique : unique_diff_within_at ℝ s' y) (hs_unique : unique_diff_on ℝ s)
(hs' : s' ∈ 𝓝[s] y) (hy : y ∈ s') (h : s' ⊆ s)
(hf : cont_diff_on ℝ n f s)
(hf' : differentiable_within_at ℝ (iterated_deriv_within n f s) s y) :
has_deriv_within_at (λ t, taylor_within_eval f n s t x)
(((n! : ℝ)⁻¹ * (x - y)^n) • (iterated_deriv_within (n+1) f s y)) s' y :=
begin
induction n with k hk,
{ simp only [taylor_within_zero_eval, nat.factorial_zero, nat.cast_one, inv_one, pow_zero,
mul_one, zero_add, one_smul],
simp only [iterated_deriv_within_zero] at hf',
rw iterated_deriv_within_one (hs_unique _ (h hy)),
exact hf'.has_deriv_within_at.mono h },
simp_rw [nat.add_succ, taylor_within_eval_succ],
simp only [add_zero, nat.factorial_succ, nat.cast_mul, nat.cast_add, nat.cast_one],
have hdiff : differentiable_on ℝ (iterated_deriv_within k f s) s',
{ have coe_lt_succ : (k : with_top ℕ) < k.succ := nat.cast_lt.2 k.lt_succ_self,
refine differentiable_on.mono _ h,
exact hf.differentiable_on_iterated_deriv_within coe_lt_succ hs_unique },
specialize hk hf.of_succ ((hdiff y hy).mono_of_mem hs'),
convert hk.add (has_deriv_within_at_taylor_coeff_within hs'_unique
(nhds_within_mono _ h self_mem_nhds_within) hf'),
exact (add_sub_cancel'_right _ _).symm
end
/-- Calculate the derivative of the Taylor polynomial with respect to `x₀`.
Version for open intervals -/
lemma taylor_within_eval_has_deriv_at_Ioo {f : ℝ → E} {a b t : ℝ} (x : ℝ) {n : ℕ}
(hx : a < b) (ht : t ∈ Ioo a b)
(hf : cont_diff_on ℝ n f (Icc a b))
(hf' : differentiable_on ℝ (iterated_deriv_within n f (Icc a b)) (Ioo a b)) :
has_deriv_at (λ y, taylor_within_eval f n (Icc a b) y x)
(((n! : ℝ)⁻¹ * (x - t)^n) • (iterated_deriv_within (n+1) f (Icc a b) t)) t :=
have h_nhds : Ioo a b ∈ 𝓝 t := is_open_Ioo.mem_nhds ht,
have h_nhds' : Ioo a b ∈ 𝓝[Icc a b] t := nhds_within_le_nhds h_nhds,
(has_deriv_within_at_taylor_within_eval (unique_diff_within_at_Ioo ht) (unique_diff_on_Icc hx)
h_nhds' ht Ioo_subset_Icc_self hf $ (hf' t ht).mono_of_mem h_nhds').has_deriv_at h_nhds
/-- Calculate the derivative of the Taylor polynomial with respect to `x₀`.
Version for closed intervals -/
lemma has_deriv_within_taylor_within_eval_at_Icc {f : ℝ → E} {a b t : ℝ} (x : ℝ) {n : ℕ}
(hx : a < b) (ht : t ∈ Icc a b) (hf : cont_diff_on ℝ n f (Icc a b))
(hf' : differentiable_on ℝ (iterated_deriv_within n f (Icc a b)) (Icc a b)) :
has_deriv_within_at (λ y, taylor_within_eval f n (Icc a b) y x)
(((n! : ℝ)⁻¹ * (x - t)^n) • (iterated_deriv_within (n+1) f (Icc a b) t)) (Icc a b) t :=
has_deriv_within_at_taylor_within_eval (unique_diff_on_Icc hx t ht) (unique_diff_on_Icc hx)
self_mem_nhds_within ht rfl.subset hf (hf' t ht)
/-! ### Taylor's theorem with mean value type remainder estimate -/
/-- **Taylor's theorem** with the general mean value form of the remainder.
We assume that `f` is `n+1`-times continuously differentiable in the closed set `Icc x₀ x` and
`n+1`-times differentiable on the open set `Ioo x₀ x`, and `g` is a differentiable function on
`Ioo x₀ x` and continuous on `Icc x₀ x`. Then there exists a `x' ∈ Ioo x₀ x` such that
$$f(x) - (P_n f)(x₀, x) = \frac{(x - x')^n}{n!} \frac{g(x) - g(x₀)}{g' x'},$$
where $P_n f$ denotes the Taylor polynomial of degree $n$. -/
lemma taylor_mean_remainder {f : ℝ → ℝ} {g g' : ℝ → ℝ} {x x₀ : ℝ} {n : ℕ} (hx : x₀ < x)
(hf : cont_diff_on ℝ n f (Icc x₀ x))
(hf' : differentiable_on ℝ (iterated_deriv_within n f (Icc x₀ x)) (Ioo x₀ x))
(gcont : continuous_on g (Icc x₀ x))
(gdiff : ∀ (x_1 : ℝ), x_1 ∈ Ioo x₀ x → has_deriv_at g (g' x_1) x_1)
(g'_ne : ∀ (x_1 : ℝ), x_1 ∈ Ioo x₀ x → g' x_1 ≠ 0) :
∃ (x' : ℝ) (hx' : x' ∈ Ioo x₀ x), f x - taylor_within_eval f n (Icc x₀ x) x₀ x =
((x - x')^n /n! * (g x - g x₀) / g' x') •
(iterated_deriv_within (n+1) f (Icc x₀ x) x')
:=
begin
-- We apply the mean value theorem
rcases exists_ratio_has_deriv_at_eq_ratio_slope (λ t, taylor_within_eval f n (Icc x₀ x) t x)
(λ t, ((n! : ℝ)⁻¹ * (x - t)^n) • (iterated_deriv_within (n+1) f (Icc x₀ x) t)) hx
(continuous_on_taylor_within_eval (unique_diff_on_Icc hx) hf)
(λ _ hy, taylor_within_eval_has_deriv_at_Ioo x hx hy hf hf')
g g' gcont gdiff with ⟨y, hy, h⟩,
use [y, hy],
-- The rest is simplifications and trivial calculations
simp only [taylor_within_eval_self] at h,
rw [mul_comm, ←div_left_inj' (g'_ne y hy), mul_div_cancel _ (g'_ne y hy)] at h,
rw ←h,
field_simp [g'_ne y hy, n.factorial_ne_zero],
ring,
end
/-- **Taylor's theorem** with the Lagrange form of the remainder.
We assume that `f` is `n+1`-times continuously differentiable in the closed set `Icc x₀ x` and
`n+1`-times differentiable on the open set `Ioo x₀ x`. Then there exists a `x' ∈ Ioo x₀ x` such that
$$f(x) - (P_n f)(x₀, x) = \frac{f^{(n+1)}(x') (x - x₀)^{n+1}}{(n+1)!},$$
where $P_n f$ denotes the Taylor polynomial of degree $n$ and $f^{(n+1)}$ is the $n+1$-th iterated
derivative. -/
lemma taylor_mean_remainder_lagrange {f : ℝ → ℝ} {x x₀ : ℝ} {n : ℕ} (hx : x₀ < x)
(hf : cont_diff_on ℝ n f (Icc x₀ x))
(hf' : differentiable_on ℝ (iterated_deriv_within n f (Icc x₀ x)) (Ioo x₀ x)) :
∃ (x' : ℝ) (hx' : x' ∈ Ioo x₀ x), f x - taylor_within_eval f n (Icc x₀ x) x₀ x =
(iterated_deriv_within (n+1) f (Icc x₀ x) x') * (x - x₀)^(n+1) /(n+1)! :=
begin
have gcont : continuous_on (λ (t : ℝ), (x - t) ^ (n + 1)) (Icc x₀ x) :=
by { refine continuous.continuous_on _, continuity },
have xy_ne : ∀ (y : ℝ), y ∈ Ioo x₀ x → (x - y)^n ≠ 0 :=
begin
intros y hy,
refine pow_ne_zero _ _,
rw [mem_Ioo] at hy,
rw sub_ne_zero,
exact hy.2.ne.symm,
end,
have hg' : ∀ (y : ℝ), y ∈ Ioo x₀ x → -(↑n + 1) * (x - y) ^ n ≠ 0 :=
λ y hy, mul_ne_zero (neg_ne_zero.mpr (nat.cast_add_one_ne_zero n)) (xy_ne y hy),
-- We apply the general theorem with g(t) = (x - t)^(n+1)
rcases taylor_mean_remainder hx hf hf' gcont (λ y _, monomial_has_deriv_aux y x _) hg'
with ⟨y, hy, h⟩,
use [y, hy],
simp only [sub_self, zero_pow', ne.def, nat.succ_ne_zero, not_false_iff, zero_sub, mul_neg] at h,
rw [h, neg_div, ←div_neg, neg_mul, neg_neg],
field_simp [n.cast_add_one_ne_zero, n.factorial_ne_zero, xy_ne y hy],
ring,
end
/-- **Taylor's theorem** with the Cauchy form of the remainder.
We assume that `f` is `n+1`-times continuously differentiable on the closed set `Icc x₀ x` and
`n+1`-times differentiable on the open set `Ioo x₀ x`. Then there exists a `x' ∈ Ioo x₀ x` such that
$$f(x) - (P_n f)(x₀, x) = \frac{f^{(n+1)}(x') (x - x')^n (x-x₀)}{n!},$$
where $P_n f$ denotes the Taylor polynomial of degree $n$ and $f^{(n+1)}$ is the $n+1$-th iterated
derivative. -/
lemma taylor_mean_remainder_cauchy {f : ℝ → ℝ} {x x₀ : ℝ} {n : ℕ} (hx : x₀ < x)
(hf : cont_diff_on ℝ n f (Icc x₀ x))
(hf' : differentiable_on ℝ (iterated_deriv_within n f (Icc x₀ x)) (Ioo x₀ x)) :
∃ (x' : ℝ) (hx' : x' ∈ Ioo x₀ x), f x - taylor_within_eval f n (Icc x₀ x) x₀ x =
(iterated_deriv_within (n+1) f (Icc x₀ x) x') * (x - x')^n /n! * (x - x₀) :=
begin
have gcont : continuous_on id (Icc x₀ x) := continuous.continuous_on (by continuity),
have gdiff : (∀ (x_1 : ℝ), x_1 ∈ Ioo x₀ x → has_deriv_at id
((λ (t : ℝ), (1 : ℝ)) x_1) x_1) := λ _ _, has_deriv_at_id _,
-- We apply the general theorem with g = id
rcases taylor_mean_remainder hx hf hf' gcont gdiff (λ _ _, by simp) with ⟨y, hy, h⟩,
use [y, hy],
rw h,
field_simp [n.factorial_ne_zero],
ring,
end
/-- **Taylor's theorem** with a polynomial bound on the remainder
We assume that `f` is `n+1`-times continuously differentiable on the closed set `Icc a b`.
The difference of `f` and its `n`-th Taylor polynomial can be estimated by
`C * (x - a)^(n+1) / n!` where `C` is a bound for the `n+1`-th iterated derivative of `f`. -/
lemma taylor_mean_remainder_bound {f : ℝ → E} {a b C x : ℝ} {n : ℕ}
(hab : a ≤ b) (hf : cont_diff_on ℝ (n+1) f (Icc a b)) (hx : x ∈ Icc a b)
(hC : ∀ y ∈ Icc a b, ‖iterated_deriv_within (n + 1) f (Icc a b) y‖ ≤ C) :
‖f x - taylor_within_eval f n (Icc a b) a x‖ ≤ C * (x - a)^(n+1) / n! :=
begin
rcases eq_or_lt_of_le hab with rfl|h,
{ rw [Icc_self, mem_singleton_iff] at hx,
simp [hx] },
-- The nth iterated derivative is differentiable
have hf' : differentiable_on ℝ (iterated_deriv_within n f (Icc a b)) (Icc a b) :=
hf.differentiable_on_iterated_deriv_within (with_top.coe_lt_coe.mpr n.lt_succ_self)
(unique_diff_on_Icc h),
-- We can uniformly bound the derivative of the Taylor polynomial
have h' : ∀ (y : ℝ) (hy : y ∈ Ico a x),
‖((n! : ℝ)⁻¹ * (x - y) ^ n) • iterated_deriv_within (n + 1) f (Icc a b) y‖
≤ (n! : ℝ)⁻¹ * |(x - a)|^n * C,
{ rintro y ⟨hay, hyx⟩,
rw [norm_smul, real.norm_eq_abs],
-- Estimate the iterated derivative by `C`
refine mul_le_mul _ (hC y ⟨hay, hyx.le.trans hx.2⟩) (by positivity) (by positivity),
-- The rest is a trivial calculation
rw [abs_mul, abs_pow, abs_inv, nat.abs_cast],
mono* with [0 ≤ (n! : ℝ)⁻¹],
any_goals { positivity },
linarith [hx.1, hyx] },
-- Apply the mean value theorem for vector valued functions:
have A : ∀ t ∈ Icc a x, has_deriv_within_at (λ y, taylor_within_eval f n (Icc a b) y x)
(((↑n!)⁻¹ * (x - t) ^ n) • iterated_deriv_within (n + 1) f (Icc a b) t) (Icc a x) t,
{ assume t ht,
have I : Icc a x ⊆ Icc a b := Icc_subset_Icc_right hx.2,
exact (has_deriv_within_taylor_within_eval_at_Icc x h (I ht) hf.of_succ hf').mono I },
have := norm_image_sub_le_of_norm_deriv_le_segment' A h' x (right_mem_Icc.2 hx.1),
simp only [taylor_within_eval_self] at this,
refine this.trans_eq _,
-- The rest is a trivial calculation
rw [abs_of_nonneg (sub_nonneg.mpr hx.1)],
ring_exp,
end
/-- **Taylor's theorem** with a polynomial bound on the remainder
We assume that `f` is `n+1`-times continuously differentiable on the closed set `Icc a b`.
There exists a constant `C` such that for all `x ∈ Icc a b` the difference of `f` and its `n`-th
Taylor polynomial can be estimated by `C * (x - a)^(n+1)`. -/
lemma exists_taylor_mean_remainder_bound {f : ℝ → E} {a b : ℝ} {n : ℕ}
(hab : a ≤ b) (hf : cont_diff_on ℝ (n+1) f (Icc a b)) :
∃ C, ∀ x ∈ Icc a b, ‖f x - taylor_within_eval f n (Icc a b) a x‖ ≤ C * (x - a)^(n+1) :=
begin
rcases eq_or_lt_of_le hab with rfl|h,
{ refine ⟨0, λ x hx, _⟩,
have : a = x, by simpa [← le_antisymm_iff] using hx,
simp [← this] },
-- We estimate by the supremum of the norm of the iterated derivative
let g : ℝ → ℝ := λ y, ‖iterated_deriv_within (n + 1) f (Icc a b) y‖,
use [has_Sup.Sup (g '' Icc a b) / n!],
intros x hx,
rw div_mul_eq_mul_div₀,
refine taylor_mean_remainder_bound hab hf hx (λ y, _),
exact (hf.continuous_on_iterated_deriv_within rfl.le $ unique_diff_on_Icc h)
.norm.le_Sup_image_Icc,
end