1(* Title: HOL/MacLaurin.thy 2 Author: Jacques D. Fleuriot, 2001 University of Edinburgh 3 Author: Lawrence C Paulson, 2004 4 Author: Lukas Bulwahn and Bernhard H��upler, 2005 5*) 6 7section \<open>MacLaurin and Taylor Series\<close> 8 9theory MacLaurin 10imports Transcendental 11begin 12 13subsection \<open>Maclaurin's Theorem with Lagrange Form of Remainder\<close> 14 15text \<open>This is a very long, messy proof even now that it's been broken down 16 into lemmas.\<close> 17 18lemma Maclaurin_lemma: 19 "0 < h \<Longrightarrow> 20 \<exists>B::real. f h = (\<Sum>m<n. (j m / (fact m)) * (h^m)) + (B * ((h^n) /(fact n)))" 21 by (rule exI[where x = "(f h - (\<Sum>m<n. (j m / (fact m)) * h^m)) * (fact n) / (h^n)"]) simp 22 23lemma eq_diff_eq': "x = y - z \<longleftrightarrow> y = x + z" 24 for x y z :: real 25 by arith 26 27lemma fact_diff_Suc: "n < Suc m \<Longrightarrow> fact (Suc m - n) = (Suc m - n) * fact (m - n)" 28 by (subst fact_reduce) auto 29 30lemma Maclaurin_lemma2: 31 fixes B 32 assumes DERIV: "\<forall>m t. m < n \<and> 0\<le>t \<and> t\<le>h \<longrightarrow> DERIV (diff m) t :> diff (Suc m) t" 33 and INIT: "n = Suc k" 34 defines "difg \<equiv> 35 (\<lambda>m t::real. diff m t - 36 ((\<Sum>p<n - m. diff (m + p) 0 / fact p * t ^ p) + B * (t ^ (n - m) / fact (n - m))))" 37 (is "difg \<equiv> (\<lambda>m t. diff m t - ?difg m t)") 38 shows "\<forall>m t. m < n \<and> 0 \<le> t \<and> t \<le> h \<longrightarrow> DERIV (difg m) t :> difg (Suc m) t" 39proof (rule allI impI)+ 40 fix m t 41 assume INIT2: "m < n \<and> 0 \<le> t \<and> t \<le> h" 42 have "DERIV (difg m) t :> diff (Suc m) t - 43 ((\<Sum>x<n - m. real x * t ^ (x - Suc 0) * diff (m + x) 0 / fact x) + 44 real (n - m) * t ^ (n - Suc m) * B / fact (n - m))" 45 by (auto simp: difg_def intro!: derivative_eq_intros DERIV[rule_format, OF INIT2]) 46 moreover 47 from INIT2 have intvl: "{..<n - m} = insert 0 (Suc ` {..<n - Suc m})" and "0 < n - m" 48 unfolding atLeast0LessThan[symmetric] by auto 49 have "(\<Sum>x<n - m. real x * t ^ (x - Suc 0) * diff (m + x) 0 / fact x) = 50 (\<Sum>x<n - Suc m. real (Suc x) * t ^ x * diff (Suc m + x) 0 / fact (Suc x))" 51 unfolding intvl by (subst sum.insert) (auto simp: sum.reindex) 52 moreover 53 have fact_neq_0: "\<And>x. (fact x) + real x * (fact x) \<noteq> 0" 54 by (metis add_pos_pos fact_gt_zero less_add_same_cancel1 less_add_same_cancel2 55 less_numeral_extra(3) mult_less_0_iff of_nat_less_0_iff) 56 have "\<And>x. (Suc x) * t ^ x * diff (Suc m + x) 0 / fact (Suc x) = diff (Suc m + x) 0 * t^x / fact x" 57 by (rule nonzero_divide_eq_eq[THEN iffD2]) auto 58 moreover 59 have "(n - m) * t ^ (n - Suc m) * B / fact (n - m) = B * (t ^ (n - Suc m) / fact (n - Suc m))" 60 using \<open>0 < n - m\<close> by (simp add: field_split_simps fact_reduce) 61 ultimately show "DERIV (difg m) t :> difg (Suc m) t" 62 unfolding difg_def by (simp add: mult.commute) 63qed 64 65lemma Maclaurin: 66 assumes h: "0 < h" 67 and n: "0 < n" 68 and diff_0: "diff 0 = f" 69 and diff_Suc: "\<forall>m t. m < n \<and> 0 \<le> t \<and> t \<le> h \<longrightarrow> DERIV (diff m) t :> diff (Suc m) t" 70 shows 71 "\<exists>t::real. 0 < t \<and> t < h \<and> 72 f h = sum (\<lambda>m. (diff m 0 / fact m) * h ^ m) {..<n} + (diff n t / fact n) * h ^ n" 73proof - 74 from n obtain m where m: "n = Suc m" 75 by (cases n) (simp add: n) 76 from m have "m < n" by simp 77 78 obtain B where f_h: "f h = (\<Sum>m<n. diff m 0 / fact m * h ^ m) + B * (h ^ n / fact n)" 79 using Maclaurin_lemma [OF h] .. 80 81 define g where [abs_def]: "g t = 82 f t - (sum (\<lambda>m. (diff m 0 / fact m) * t^m) {..<n} + B * (t^n / fact n))" for t 83 have g2: "g 0 = 0" "g h = 0" 84 by (simp_all add: m f_h g_def lessThan_Suc_eq_insert_0 image_iff diff_0 sum.reindex) 85 86 define difg where [abs_def]: "difg m t = 87 diff m t - (sum (\<lambda>p. (diff (m + p) 0 / fact p) * (t ^ p)) {..<n-m} + 88 B * ((t ^ (n - m)) / fact (n - m)))" for m t 89 have difg_0: "difg 0 = g" 90 by (simp add: difg_def g_def diff_0) 91 have difg_Suc: "\<forall>m t. m < n \<and> 0 \<le> t \<and> t \<le> h \<longrightarrow> DERIV (difg m) t :> difg (Suc m) t" 92 using diff_Suc m unfolding difg_def [abs_def] by (rule Maclaurin_lemma2) 93 have difg_eq_0: "\<forall>m<n. difg m 0 = 0" 94 by (auto simp: difg_def m Suc_diff_le lessThan_Suc_eq_insert_0 image_iff sum.reindex) 95 have isCont_difg: "\<And>m x. m < n \<Longrightarrow> 0 \<le> x \<Longrightarrow> x \<le> h \<Longrightarrow> isCont (difg m) x" 96 by (rule DERIV_isCont [OF difg_Suc [rule_format]]) simp 97 have differentiable_difg: "\<And>m x. m < n \<Longrightarrow> 0 \<le> x \<Longrightarrow> x \<le> h \<Longrightarrow> difg m differentiable (at x)" 98 using difg_Suc real_differentiable_def by auto 99 have difg_Suc_eq_0: 100 "\<And>m t. m < n \<Longrightarrow> 0 \<le> t \<Longrightarrow> t \<le> h \<Longrightarrow> DERIV (difg m) t :> 0 \<Longrightarrow> difg (Suc m) t = 0" 101 by (rule DERIV_unique [OF difg_Suc [rule_format]]) simp 102 103 have "\<exists>t. 0 < t \<and> t < h \<and> DERIV (difg m) t :> 0" 104 using \<open>m < n\<close> 105 proof (induct m) 106 case 0 107 show ?case 108 proof (rule Rolle) 109 show "0 < h" by fact 110 show "difg 0 0 = difg 0 h" 111 by (simp add: difg_0 g2) 112 show "continuous_on {0..h} (difg 0)" 113 by (simp add: continuous_at_imp_continuous_on isCont_difg n) 114 qed (simp add: differentiable_difg n) 115 next 116 case (Suc m') 117 then have "\<exists>t. 0 < t \<and> t < h \<and> DERIV (difg m') t :> 0" 118 by simp 119 then obtain t where t: "0 < t" "t < h" "DERIV (difg m') t :> 0" 120 by fast 121 have "\<exists>t'. 0 < t' \<and> t' < t \<and> DERIV (difg (Suc m')) t' :> 0" 122 proof (rule Rolle) 123 show "0 < t" by fact 124 show "difg (Suc m') 0 = difg (Suc m') t" 125 using t \<open>Suc m' < n\<close> by (simp add: difg_Suc_eq_0 difg_eq_0) 126 have "\<And>x. 0 \<le> x \<and> x \<le> t \<Longrightarrow> isCont (difg (Suc m')) x" 127 using \<open>t < h\<close> \<open>Suc m' < n\<close> by (simp add: isCont_difg) 128 then show "continuous_on {0..t} (difg (Suc m'))" 129 by (simp add: continuous_at_imp_continuous_on) 130 qed (use \<open>t < h\<close> \<open>Suc m' < n\<close> in \<open>simp add: differentiable_difg\<close>) 131 with \<open>t < h\<close> show ?case 132 by auto 133 qed 134 then obtain t where "0 < t" "t < h" "DERIV (difg m) t :> 0" 135 by fast 136 with \<open>m < n\<close> have "difg (Suc m) t = 0" 137 by (simp add: difg_Suc_eq_0) 138 show ?thesis 139 proof (intro exI conjI) 140 show "0 < t" by fact 141 show "t < h" by fact 142 show "f h = (\<Sum>m<n. diff m 0 / (fact m) * h ^ m) + diff n t / (fact n) * h ^ n" 143 using \<open>difg (Suc m) t = 0\<close> by (simp add: m f_h difg_def) 144 qed 145qed 146 147lemma Maclaurin2: 148 fixes n :: nat 149 and h :: real 150 assumes INIT1: "0 < h" 151 and INIT2: "diff 0 = f" 152 and DERIV: "\<forall>m t. m < n \<and> 0 \<le> t \<and> t \<le> h \<longrightarrow> DERIV (diff m) t :> diff (Suc m) t" 153 shows "\<exists>t. 0 < t \<and> t \<le> h \<and> f h = (\<Sum>m<n. diff m 0 / (fact m) * h ^ m) + diff n t / fact n * h ^ n" 154proof (cases n) 155 case 0 156 with INIT1 INIT2 show ?thesis by fastforce 157next 158 case Suc 159 then have "n > 0" by simp 160 from INIT1 this INIT2 DERIV 161 have "\<exists>t>0. t < h \<and> f h = (\<Sum>m<n. diff m 0 / fact m * h ^ m) + diff n t / fact n * h ^ n" 162 by (rule Maclaurin) 163 then show ?thesis by fastforce 164qed 165 166lemma Maclaurin_minus: 167 fixes n :: nat and h :: real 168 assumes "h < 0" "0 < n" "diff 0 = f" 169 and DERIV: "\<forall>m t. m < n \<and> h \<le> t \<and> t \<le> 0 \<longrightarrow> DERIV (diff m) t :> diff (Suc m) t" 170 shows "\<exists>t. h < t \<and> t < 0 \<and> f h = (\<Sum>m<n. diff m 0 / fact m * h ^ m) + diff n t / fact n * h ^ n" 171proof - 172 txt \<open>Transform \<open>ABL'\<close> into \<open>derivative_intros\<close> format.\<close> 173 note DERIV' = DERIV_chain'[OF _ DERIV[rule_format], THEN DERIV_cong] 174 let ?sum = "\<lambda>t. 175 (\<Sum>m<n. (- 1) ^ m * diff m (- 0) / (fact m) * (- h) ^ m) + 176 (- 1) ^ n * diff n (- t) / (fact n) * (- h) ^ n" 177 from assms have "\<exists>t>0. t < - h \<and> f (- (- h)) = ?sum t" 178 by (intro Maclaurin) (auto intro!: derivative_eq_intros DERIV') 179 then obtain t where "0 < t" "t < - h" "f (- (- h)) = ?sum t" 180 by blast 181 moreover have "(- 1) ^ n * diff n (- t) * (- h) ^ n / fact n = diff n (- t) * h ^ n / fact n" 182 by (auto simp: power_mult_distrib[symmetric]) 183 moreover 184 have "(\<Sum>m<n. (- 1) ^ m * diff m 0 * (- h) ^ m / fact m) = (\<Sum>m<n. diff m 0 * h ^ m / fact m)" 185 by (auto intro: sum.cong simp add: power_mult_distrib[symmetric]) 186 ultimately have "h < - t \<and> - t < 0 \<and> 187 f h = (\<Sum>m<n. diff m 0 / (fact m) * h ^ m) + diff n (- t) / (fact n) * h ^ n" 188 by auto 189 then show ?thesis .. 190qed 191 192 193subsection \<open>More Convenient "Bidirectional" Version.\<close> 194 195lemma Maclaurin_bi_le: 196 fixes n :: nat and x :: real 197 assumes "diff 0 = f" 198 and DERIV : "\<forall>m t. m < n \<and> \<bar>t\<bar> \<le> \<bar>x\<bar> \<longrightarrow> DERIV (diff m) t :> diff (Suc m) t" 199 shows "\<exists>t. \<bar>t\<bar> \<le> \<bar>x\<bar> \<and> f x = (\<Sum>m<n. diff m 0 / (fact m) * x ^ m) + diff n t / (fact n) * x ^ n" 200 (is "\<exists>t. _ \<and> f x = ?f x t") 201proof (cases "n = 0") 202 case True 203 with \<open>diff 0 = f\<close> show ?thesis by force 204next 205 case False 206 show ?thesis 207 proof (cases rule: linorder_cases) 208 assume "x = 0" 209 with \<open>n \<noteq> 0\<close> \<open>diff 0 = f\<close> DERIV have "\<bar>0\<bar> \<le> \<bar>x\<bar> \<and> f x = ?f x 0" 210 by auto 211 then show ?thesis .. 212 next 213 assume "x < 0" 214 with \<open>n \<noteq> 0\<close> DERIV have "\<exists>t>x. t < 0 \<and> diff 0 x = ?f x t" 215 by (intro Maclaurin_minus) auto 216 then obtain t where "x < t" "t < 0" 217 "diff 0 x = (\<Sum>m<n. diff m 0 / fact m * x ^ m) + diff n t / fact n * x ^ n" 218 by blast 219 with \<open>x < 0\<close> \<open>diff 0 = f\<close> have "\<bar>t\<bar> \<le> \<bar>x\<bar> \<and> f x = ?f x t" 220 by simp 221 then show ?thesis .. 222 next 223 assume "x > 0" 224 with \<open>n \<noteq> 0\<close> \<open>diff 0 = f\<close> DERIV have "\<exists>t>0. t < x \<and> diff 0 x = ?f x t" 225 by (intro Maclaurin) auto 226 then obtain t where "0 < t" "t < x" 227 "diff 0 x = (\<Sum>m<n. diff m 0 / fact m * x ^ m) + diff n t / fact n * x ^ n" 228 by blast 229 with \<open>x > 0\<close> \<open>diff 0 = f\<close> have "\<bar>t\<bar> \<le> \<bar>x\<bar> \<and> f x = ?f x t" by simp 230 then show ?thesis .. 231 qed 232qed 233 234lemma Maclaurin_all_lt: 235 fixes x :: real 236 assumes INIT1: "diff 0 = f" 237 and INIT2: "0 < n" 238 and INIT3: "x \<noteq> 0" 239 and DERIV: "\<forall>m x. DERIV (diff m) x :> diff(Suc m) x" 240 shows "\<exists>t. 0 < \<bar>t\<bar> \<and> \<bar>t\<bar> < \<bar>x\<bar> \<and> f x = 241 (\<Sum>m<n. (diff m 0 / fact m) * x ^ m) + (diff n t / fact n) * x ^ n" 242 (is "\<exists>t. _ \<and> _ \<and> f x = ?f x t") 243proof (cases rule: linorder_cases) 244 assume "x = 0" 245 with INIT3 show ?thesis .. 246next 247 assume "x < 0" 248 with assms have "\<exists>t>x. t < 0 \<and> f x = ?f x t" 249 by (intro Maclaurin_minus) auto 250 then obtain t where "t > x" "t < 0" "f x = ?f x t" 251 by blast 252 with \<open>x < 0\<close> have "0 < \<bar>t\<bar> \<and> \<bar>t\<bar> < \<bar>x\<bar> \<and> f x = ?f x t" 253 by simp 254 then show ?thesis .. 255next 256 assume "x > 0" 257 with assms have "\<exists>t>0. t < x \<and> f x = ?f x t" 258 by (intro Maclaurin) auto 259 then obtain t where "t > 0" "t < x" "f x = ?f x t" 260 by blast 261 with \<open>x > 0\<close> have "0 < \<bar>t\<bar> \<and> \<bar>t\<bar> < \<bar>x\<bar> \<and> f x = ?f x t" 262 by simp 263 then show ?thesis .. 264qed 265 266lemma Maclaurin_zero: "x = 0 \<Longrightarrow> n \<noteq> 0 \<Longrightarrow> (\<Sum>m<n. (diff m 0 / fact m) * x ^ m) = diff 0 0" 267 for x :: real and n :: nat 268 by simp 269 270 271lemma Maclaurin_all_le: 272 fixes x :: real and n :: nat 273 assumes INIT: "diff 0 = f" 274 and DERIV: "\<forall>m x. DERIV (diff m) x :> diff (Suc m) x" 275 shows "\<exists>t. \<bar>t\<bar> \<le> \<bar>x\<bar> \<and> f x = (\<Sum>m<n. (diff m 0 / fact m) * x ^ m) + (diff n t / fact n) * x ^ n" 276 (is "\<exists>t. _ \<and> f x = ?f x t") 277proof (cases "n = 0") 278 case True 279 with INIT show ?thesis by force 280next 281 case False 282 show ?thesis 283 proof (cases "x = 0") 284 case True 285 with \<open>n \<noteq> 0\<close> have "(\<Sum>m<n. diff m 0 / (fact m) * x ^ m) = diff 0 0" 286 by (intro Maclaurin_zero) auto 287 with INIT \<open>x = 0\<close> \<open>n \<noteq> 0\<close> have " \<bar>0\<bar> \<le> \<bar>x\<bar> \<and> f x = ?f x 0" 288 by force 289 then show ?thesis .. 290 next 291 case False 292 with INIT \<open>n \<noteq> 0\<close> DERIV have "\<exists>t. 0 < \<bar>t\<bar> \<and> \<bar>t\<bar> < \<bar>x\<bar> \<and> f x = ?f x t" 293 by (intro Maclaurin_all_lt) auto 294 then obtain t where "0 < \<bar>t\<bar> \<and> \<bar>t\<bar> < \<bar>x\<bar> \<and> f x = ?f x t" .. 295 then have "\<bar>t\<bar> \<le> \<bar>x\<bar> \<and> f x = ?f x t" 296 by simp 297 then show ?thesis .. 298 qed 299qed 300 301lemma Maclaurin_all_le_objl: 302 "diff 0 = f \<and> (\<forall>m x. DERIV (diff m) x :> diff (Suc m) x) \<longrightarrow> 303 (\<exists>t::real. \<bar>t\<bar> \<le> \<bar>x\<bar> \<and> f x = (\<Sum>m<n. (diff m 0 / fact m) * x ^ m) + (diff n t / fact n) * x ^ n)" 304 for x :: real and n :: nat 305 by (blast intro: Maclaurin_all_le) 306 307 308subsection \<open>Version for Exponential Function\<close> 309 310lemma Maclaurin_exp_lt: 311 fixes x :: real and n :: nat 312 shows 313 "x \<noteq> 0 \<Longrightarrow> n > 0 \<Longrightarrow> 314 (\<exists>t. 0 < \<bar>t\<bar> \<and> \<bar>t\<bar> < \<bar>x\<bar> \<and> exp x = (\<Sum>m<n. (x ^ m) / fact m) + (exp t / fact n) * x ^ n)" 315 using Maclaurin_all_lt [where diff = "\<lambda>n. exp" and f = exp and x = x and n = n] by auto 316 317lemma Maclaurin_exp_le: 318 fixes x :: real and n :: nat 319 shows "\<exists>t. \<bar>t\<bar> \<le> \<bar>x\<bar> \<and> exp x = (\<Sum>m<n. (x ^ m) / fact m) + (exp t / fact n) * x ^ n" 320 using Maclaurin_all_le_objl [where diff = "\<lambda>n. exp" and f = exp and x = x and n = n] by auto 321 322corollary exp_lower_Taylor_quadratic: "0 \<le> x \<Longrightarrow> 1 + x + x\<^sup>2 / 2 \<le> exp x" 323 for x :: real 324 using Maclaurin_exp_le [of x 3] by (auto simp: numeral_3_eq_3 power2_eq_square) 325 326corollary ln_2_less_1: "ln 2 < (1::real)" 327proof - 328 have "2 < 5/(2::real)" by simp 329 also have "5/2 \<le> exp (1::real)" using exp_lower_Taylor_quadratic[of 1, simplified] by simp 330 finally have "exp (ln 2) < exp (1::real)" by simp 331 thus "ln 2 < (1::real)" by (subst (asm) exp_less_cancel_iff) simp 332qed 333 334subsection \<open>Version for Sine Function\<close> 335 336lemma mod_exhaust_less_4: "m mod 4 = 0 \<or> m mod 4 = 1 \<or> m mod 4 = 2 \<or> m mod 4 = 3" 337 for m :: nat 338 by auto 339 340 341text \<open>It is unclear why so many variant results are needed.\<close> 342 343lemma sin_expansion_lemma: "sin (x + real (Suc m) * pi / 2) = cos (x + real m * pi / 2)" 344 by (auto simp: cos_add sin_add add_divide_distrib distrib_right) 345 346lemma Maclaurin_sin_expansion2: 347 "\<exists>t. \<bar>t\<bar> \<le> \<bar>x\<bar> \<and> 348 sin x = (\<Sum>m<n. sin_coeff m * x ^ m) + (sin (t + 1/2 * real n * pi) / fact n) * x ^ n" 349proof (cases "n = 0 \<or> x = 0") 350 case False 351 let ?diff = "\<lambda>n x. sin (x + 1/2 * real n * pi)" 352 have "\<exists>t. 0 < \<bar>t\<bar> \<and> \<bar>t\<bar> < \<bar>x\<bar> \<and> sin x = 353 (\<Sum>m<n. (?diff m 0 / fact m) * x ^ m) + (?diff n t / fact n) * x ^ n" 354 proof (rule Maclaurin_all_lt) 355 show "\<forall>m x. ((\<lambda>t. sin (t + 1/2 * real m * pi)) has_real_derivative 356 sin (x + 1/2 * real (Suc m) * pi)) (at x)" 357 by (rule allI derivative_eq_intros | use sin_expansion_lemma in force)+ 358 qed (use False in auto) 359 then show ?thesis 360 apply (rule ex_forward, simp) 361 apply (rule sum.cong[OF refl]) 362 apply (auto simp: sin_coeff_def sin_zero_iff elim: oddE simp del: of_nat_Suc) 363 done 364qed auto 365 366lemma Maclaurin_sin_expansion: 367 "\<exists>t. sin x = (\<Sum>m<n. sin_coeff m * x ^ m) + (sin (t + 1/2 * real n * pi) / fact n) * x ^ n" 368 using Maclaurin_sin_expansion2 [of x n] by blast 369 370lemma Maclaurin_sin_expansion3: 371 assumes "n > 0" "x > 0" 372 shows "\<exists>t. 0 < t \<and> t < x \<and> 373 sin x = (\<Sum>m<n. sin_coeff m * x ^ m) + (sin (t + 1/2 * real n * pi) / fact n) * x ^ n" 374proof - 375 let ?diff = "\<lambda>n x. sin (x + 1/2 * real n * pi)" 376 have "\<exists>t. 0 < t \<and> t < x \<and> sin x = (\<Sum>m<n. ?diff m 0 / (fact m) * x ^ m) + ?diff n t / fact n * x ^ n" 377 proof (rule Maclaurin) 378 show "\<forall>m t. m < n \<and> 0 \<le> t \<and> t \<le> x \<longrightarrow> 379 ((\<lambda>u. sin (u + 1/2 * real m * pi)) has_real_derivative 380 sin (t + 1/2 * real (Suc m) * pi)) (at t)" 381 apply (simp add: sin_expansion_lemma del: of_nat_Suc) 382 apply (force intro!: derivative_eq_intros) 383 done 384 qed (use assms in auto) 385 then show ?thesis 386 apply (rule ex_forward, simp) 387 apply (rule sum.cong[OF refl]) 388 apply (auto simp: sin_coeff_def sin_zero_iff elim: oddE simp del: of_nat_Suc) 389 done 390qed 391 392lemma Maclaurin_sin_expansion4: 393 assumes "0 < x" 394 shows "\<exists>t. 0 < t \<and> t \<le> x \<and> sin x = (\<Sum>m<n. sin_coeff m * x ^ m) + (sin (t + 1/2 * real n * pi) / fact n) * x ^ n" 395proof - 396 let ?diff = "\<lambda>n x. sin (x + 1/2 * real n * pi)" 397 have "\<exists>t. 0 < t \<and> t \<le> x \<and> sin x = (\<Sum>m<n. ?diff m 0 / (fact m) * x ^ m) + ?diff n t / fact n * x ^ n" 398 proof (rule Maclaurin2) 399 show "\<forall>m t. m < n \<and> 0 \<le> t \<and> t \<le> x \<longrightarrow> 400 ((\<lambda>u. sin (u + 1/2 * real m * pi)) has_real_derivative 401 sin (t + 1/2 * real (Suc m) * pi)) (at t)" 402 apply (simp add: sin_expansion_lemma del: of_nat_Suc) 403 apply (force intro!: derivative_eq_intros) 404 done 405 qed (use assms in auto) 406 then show ?thesis 407 apply (rule ex_forward, simp) 408 apply (rule sum.cong[OF refl]) 409 apply (auto simp: sin_coeff_def sin_zero_iff elim: oddE simp del: of_nat_Suc) 410 done 411qed 412 413 414subsection \<open>Maclaurin Expansion for Cosine Function\<close> 415 416lemma sumr_cos_zero_one [simp]: "(\<Sum>m<Suc n. cos_coeff m * 0 ^ m) = 1" 417 by (induct n) auto 418 419lemma cos_expansion_lemma: "cos (x + real (Suc m) * pi / 2) = - sin (x + real m * pi / 2)" 420 by (auto simp: cos_add sin_add distrib_right add_divide_distrib) 421 422lemma Maclaurin_cos_expansion: 423 "\<exists>t::real. \<bar>t\<bar> \<le> \<bar>x\<bar> \<and> 424 cos x = (\<Sum>m<n. cos_coeff m * x ^ m) + (cos(t + 1/2 * real n * pi) / fact n) * x ^ n" 425proof (cases "n = 0 \<or> x = 0") 426 case False 427 let ?diff = "\<lambda>n x. cos (x + 1/2 * real n * pi)" 428 have "\<exists>t. 0 < \<bar>t\<bar> \<and> \<bar>t\<bar> < \<bar>x\<bar> \<and> cos x = 429 (\<Sum>m<n. (?diff m 0 / fact m) * x ^ m) + (?diff n t / fact n) * x ^ n" 430 proof (rule Maclaurin_all_lt) 431 show "\<forall>m x. ((\<lambda>t. cos (t + 1/2 * real m * pi)) has_real_derivative 432 cos (x + 1/2 * real (Suc m) * pi)) (at x)" 433 apply (rule allI derivative_eq_intros | simp)+ 434 using cos_expansion_lemma by force 435 qed (use False in auto) 436 then show ?thesis 437 apply (rule ex_forward, simp) 438 apply (rule sum.cong[OF refl]) 439 apply (auto simp: cos_coeff_def cos_zero_iff elim: evenE simp del: of_nat_Suc) 440 done 441qed auto 442 443lemma Maclaurin_cos_expansion2: 444 assumes "x > 0" "n > 0" 445 shows "\<exists>t. 0 < t \<and> t < x \<and> 446 cos x = (\<Sum>m<n. cos_coeff m * x ^ m) + (cos (t + 1/2 * real n * pi) / fact n) * x ^ n" 447proof - 448 let ?diff = "\<lambda>n x. cos (x + 1/2 * real n * pi)" 449 have "\<exists>t. 0 < t \<and> t < x \<and> cos x = (\<Sum>m<n. ?diff m 0 / (fact m) * x ^ m) + ?diff n t / fact n * x ^ n" 450 proof (rule Maclaurin) 451 show "\<forall>m t. m < n \<and> 0 \<le> t \<and> t \<le> x \<longrightarrow> 452 ((\<lambda>u. cos (u + 1 / 2 * real m * pi)) has_real_derivative 453 cos (t + 1 / 2 * real (Suc m) * pi)) (at t)" 454 by (simp add: cos_expansion_lemma del: of_nat_Suc) 455 qed (use assms in auto) 456 then show ?thesis 457 apply (rule ex_forward, simp) 458 apply (rule sum.cong[OF refl]) 459 apply (auto simp: cos_coeff_def cos_zero_iff elim: evenE) 460 done 461qed 462 463lemma Maclaurin_minus_cos_expansion: 464 assumes "n > 0" "x < 0" 465 shows "\<exists>t. x < t \<and> t < 0 \<and> 466 cos x = (\<Sum>m<n. cos_coeff m * x ^ m) + ((cos (t + 1/2 * real n * pi) / fact n) * x ^ n)" 467proof - 468 let ?diff = "\<lambda>n x. cos (x + 1/2 * real n * pi)" 469 have "\<exists>t. x < t \<and> t < 0 \<and> cos x = (\<Sum>m<n. ?diff m 0 / (fact m) * x ^ m) + ?diff n t / fact n * x ^ n" 470 proof (rule Maclaurin_minus) 471 show "\<forall>m t. m < n \<and> x \<le> t \<and> t \<le> 0 \<longrightarrow> 472 ((\<lambda>u. cos (u + 1 / 2 * real m * pi)) has_real_derivative 473 cos (t + 1 / 2 * real (Suc m) * pi)) (at t)" 474 by (simp add: cos_expansion_lemma del: of_nat_Suc) 475 qed (use assms in auto) 476 then show ?thesis 477 apply (rule ex_forward, simp) 478 apply (rule sum.cong[OF refl]) 479 apply (auto simp: cos_coeff_def cos_zero_iff elim: evenE) 480 done 481qed 482 483 484(* Version for ln(1 +/- x). Where is it?? *) 485 486 487lemma sin_bound_lemma: "x = y \<Longrightarrow> \<bar>u\<bar> \<le> v \<Longrightarrow> \<bar>(x + u) - y\<bar> \<le> v" 488 for x y u v :: real 489 by auto 490 491lemma Maclaurin_sin_bound: "\<bar>sin x - (\<Sum>m<n. sin_coeff m * x ^ m)\<bar> \<le> inverse (fact n) * \<bar>x\<bar> ^ n" 492proof - 493 have est: "x \<le> 1 \<Longrightarrow> 0 \<le> y \<Longrightarrow> x * y \<le> 1 * y" for x y :: real 494 by (rule mult_right_mono) simp_all 495 let ?diff = "\<lambda>(n::nat) (x::real). 496 if n mod 4 = 0 then sin x 497 else if n mod 4 = 1 then cos x 498 else if n mod 4 = 2 then - sin x 499 else - cos x" 500 have diff_0: "?diff 0 = sin" by simp 501 have "DERIV (?diff m) x :> ?diff (Suc m) x" for m and x 502 using mod_exhaust_less_4 [of m] 503 by (auto simp: mod_Suc intro!: derivative_eq_intros) 504 then have DERIV_diff: "\<forall>m x. DERIV (?diff m) x :> ?diff (Suc m) x" 505 by blast 506 from Maclaurin_all_le [OF diff_0 DERIV_diff] 507 obtain t where t1: "\<bar>t\<bar> \<le> \<bar>x\<bar>" 508 and t2: "sin x = (\<Sum>m<n. ?diff m 0 / (fact m) * x ^ m) + ?diff n t / (fact n) * x ^ n" 509 by fast 510 have diff_m_0: "?diff m 0 = (if even m then 0 else (- 1) ^ ((m - Suc 0) div 2))" for m 511 using mod_exhaust_less_4 [of m] 512 by (auto simp: minus_one_power_iff even_even_mod_4_iff [of m] dest: even_mod_4_div_2 odd_mod_4_div_2) 513 show ?thesis 514 unfolding sin_coeff_def 515 apply (subst t2) 516 apply (rule sin_bound_lemma) 517 apply (rule sum.cong[OF refl]) 518 apply (subst diff_m_0, simp) 519 using est 520 apply (auto intro: mult_right_mono [where b=1, simplified] mult_right_mono 521 simp: ac_simps divide_inverse power_abs [symmetric] abs_mult) 522 done 523qed 524 525 526section \<open>Taylor series\<close> 527 528text \<open> 529 We use MacLaurin and the translation of the expansion point \<open>c\<close> to \<open>0\<close> 530 to prove Taylor's theorem. 531\<close> 532 533lemma Taylor_up: 534 assumes INIT: "n > 0" "diff 0 = f" 535 and DERIV: "\<forall>m t. m < n \<and> a \<le> t \<and> t \<le> b \<longrightarrow> DERIV (diff m) t :> (diff (Suc m) t)" 536 and INTERV: "a \<le> c" "c < b" 537 shows "\<exists>t::real. c < t \<and> t < b \<and> 538 f b = (\<Sum>m<n. (diff m c / fact m) * (b - c)^m) + (diff n t / fact n) * (b - c)^n" 539proof - 540 from INTERV have "0 < b - c" by arith 541 moreover from INIT have "n > 0" "(\<lambda>m x. diff m (x + c)) 0 = (\<lambda>x. f (x + c))" 542 by auto 543 moreover 544 have "\<forall>m t. m < n \<and> 0 \<le> t \<and> t \<le> b - c \<longrightarrow> DERIV (\<lambda>x. diff m (x + c)) t :> diff (Suc m) (t + c)" 545 proof (intro strip) 546 fix m t 547 assume "m < n \<and> 0 \<le> t \<and> t \<le> b - c" 548 with DERIV and INTERV have "DERIV (diff m) (t + c) :> diff (Suc m) (t + c)" 549 by auto 550 moreover from DERIV_ident and DERIV_const have "DERIV (\<lambda>x. x + c) t :> 1 + 0" 551 by (rule DERIV_add) 552 ultimately have "DERIV (\<lambda>x. diff m (x + c)) t :> diff (Suc m) (t + c) * (1 + 0)" 553 by (rule DERIV_chain2) 554 then show "DERIV (\<lambda>x. diff m (x + c)) t :> diff (Suc m) (t + c)" 555 by simp 556 qed 557 ultimately obtain x where 558 "0 < x \<and> x < b - c \<and> 559 f (b - c + c) = 560 (\<Sum>m<n. diff m (0 + c) / fact m * (b - c) ^ m) + diff n (x + c) / fact n * (b - c) ^ n" 561 by (rule Maclaurin [THEN exE]) 562 then have "c < x + c \<and> x + c < b \<and> f b = 563 (\<Sum>m<n. diff m c / fact m * (b - c) ^ m) + diff n (x + c) / fact n * (b - c) ^ n" 564 by fastforce 565 then show ?thesis by fastforce 566qed 567 568lemma Taylor_down: 569 fixes a :: real and n :: nat 570 assumes INIT: "n > 0" "diff 0 = f" 571 and DERIV: "(\<forall>m t. m < n \<and> a \<le> t \<and> t \<le> b \<longrightarrow> DERIV (diff m) t :> diff (Suc m) t)" 572 and INTERV: "a < c" "c \<le> b" 573 shows "\<exists>t. a < t \<and> t < c \<and> 574 f a = (\<Sum>m<n. (diff m c / fact m) * (a - c)^m) + (diff n t / fact n) * (a - c)^n" 575proof - 576 from INTERV have "a-c < 0" by arith 577 moreover from INIT have "n > 0" "(\<lambda>m x. diff m (x + c)) 0 = (\<lambda>x. f (x + c))" 578 by auto 579 moreover 580 have "\<forall>m t. m < n \<and> a - c \<le> t \<and> t \<le> 0 \<longrightarrow> DERIV (\<lambda>x. diff m (x + c)) t :> diff (Suc m) (t + c)" 581 proof (rule allI impI)+ 582 fix m t 583 assume "m < n \<and> a - c \<le> t \<and> t \<le> 0" 584 with DERIV and INTERV have "DERIV (diff m) (t + c) :> diff (Suc m) (t + c)" 585 by auto 586 moreover from DERIV_ident and DERIV_const have "DERIV (\<lambda>x. x + c) t :> 1 + 0" 587 by (rule DERIV_add) 588 ultimately have "DERIV (\<lambda>x. diff m (x + c)) t :> diff (Suc m) (t + c) * (1 + 0)" 589 by (rule DERIV_chain2) 590 then show "DERIV (\<lambda>x. diff m (x + c)) t :> diff (Suc m) (t + c)" 591 by simp 592 qed 593 ultimately obtain x where 594 "a - c < x \<and> x < 0 \<and> 595 f (a - c + c) = 596 (\<Sum>m<n. diff m (0 + c) / fact m * (a - c) ^ m) + diff n (x + c) / fact n * (a - c) ^ n" 597 by (rule Maclaurin_minus [THEN exE]) 598 then have "a < x + c \<and> x + c < c \<and> 599 f a = (\<Sum>m<n. diff m c / fact m * (a - c) ^ m) + diff n (x + c) / fact n * (a - c) ^ n" 600 by fastforce 601 then show ?thesis by fastforce 602qed 603 604theorem Taylor: 605 fixes a :: real and n :: nat 606 assumes INIT: "n > 0" "diff 0 = f" 607 and DERIV: "\<forall>m t. m < n \<and> a \<le> t \<and> t \<le> b \<longrightarrow> DERIV (diff m) t :> diff (Suc m) t" 608 and INTERV: "a \<le> c " "c \<le> b" "a \<le> x" "x \<le> b" "x \<noteq> c" 609 shows "\<exists>t. 610 (if x < c then x < t \<and> t < c else c < t \<and> t < x) \<and> 611 f x = (\<Sum>m<n. (diff m c / fact m) * (x - c)^m) + (diff n t / fact n) * (x - c)^n" 612proof (cases "x < c") 613 case True 614 note INIT 615 moreover have "\<forall>m t. m < n \<and> x \<le> t \<and> t \<le> b \<longrightarrow> DERIV (diff m) t :> diff (Suc m) t" 616 using DERIV and INTERV by fastforce 617 moreover note True 618 moreover from INTERV have "c \<le> b" 619 by simp 620 ultimately have "\<exists>t>x. t < c \<and> f x = 621 (\<Sum>m<n. diff m c / (fact m) * (x - c) ^ m) + diff n t / (fact n) * (x - c) ^ n" 622 by (rule Taylor_down) 623 with True show ?thesis by simp 624next 625 case False 626 note INIT 627 moreover have "\<forall>m t. m < n \<and> a \<le> t \<and> t \<le> x \<longrightarrow> DERIV (diff m) t :> diff (Suc m) t" 628 using DERIV and INTERV by fastforce 629 moreover from INTERV have "a \<le> c" 630 by arith 631 moreover from False and INTERV have "c < x" 632 by arith 633 ultimately have "\<exists>t>c. t < x \<and> f x = 634 (\<Sum>m<n. diff m c / (fact m) * (x - c) ^ m) + diff n t / (fact n) * (x - c) ^ n" 635 by (rule Taylor_up) 636 with False show ?thesis by simp 637qed 638 639end 640