# math | 作业Python – Final Exam

### Final Exam

math | 作业Python – 这是一个Python解决math问题的practice, 考察math相关的理解, 涉及了math/Python等代写方面 ##### 2

(a) We have known that by Taylor Theorem :

``````f ( x + h )= f ( x )+ h f ( x )+
``````
``````h^2
2
f ( x )+
``````
``````h^3
3!
f ( x )+
``````
``````h^4
4!
f (4)(  1 )
``````
``````f ( x  h )= f ( x ) h f ( x )+
h^2
2
f ( x )
h^3
3!
f ( x )+
h^4
4!
f (4)(  2 )
``````
``````Therefore
``````
``````f ( x + h )+ f ( x  h )= 2 f ( x )+ h^2 f ( x )+
h^4 [ f (4)(  1 )+ f (4)(  2 )]
24
``````

which implies that f ( x + h ) 2 f ( x )+ f ( x h ) h^2

`````` f ( x )=
h^2 f (4)(  )
12
``````
###### .

Here this is a second-order centered difference approximation of the second derivative f ( x ).

``````f ( x )= x^2 ex
f ( x )= 2 ex + x^2 ex
f ( x )= 2 ex + 2 xe 6 x + 2 xex + x^2 ex =( x^2 + 4 x +2) ex.
At x =0, f ( x )=2, For h =1,
f ( x + h ) 2 f ( x )+ f ( x  h )
h^2
= e +
``````
###### 1
``````e
``````
###### 3.08616,
``````E r r or =1.
``````

For h =^14 , f ( x + h ) 2 f ( x )+ f ( x h ) h^2 = e +

###### 1
``````e
``````
###### 2.06283,
``````E r r or =0.
``````
``````For h = 1001 ,
f ( x + h ) 2 f ( x )+ f ( x  h )
h^2
``````
``````= e +
``````
###### 1
``````e
``````
###### 3.2.00010,
``````E r r or =0.
(b)
``````
``````f ( x + h )= f ( x )+ h f ( x )+
h^2
2
f ( x )+
h^3
3!
f ( x )+
``````
``````f ( x + 2 h )= f ( x )+ 2 h f ( x )+
4 h^2
2
``````
``````f ( x )+
8 h^3
3!
``````
``````f ( x )+
``````
``````which gives us
``````
``````4  f ( x + h )= 4 f ( x )+ 4 h f ( x )+
4 h^2
2
f ( x )+
4 h^3
3!
f ( x )+
``````
``````f ( x + 2 h ) 4 f ( x + h )= 3 f ( x ) 2 h f ( x )+
4 h^3
3!
f ( x )+
``````

which implies that f ( x + 2 h ) 4 f ( x + h )+ 3 f ( x )= 2 h f ( x )+ O ( h^3 )

or 3 f ( x )+ 4 f ( x + h ) f ( x + 2 h ) 2 h = f ( x )+ O ( h^2 )

whose truncation error is O ( h^2 ). (e)

``````f ( x + h )= f ( x )+ h f ( x )+
h^2
2
f ( x )+
h^3
3!
f ( x )+
h^4
4!
f (4)( x )+ (1)
``````
``````f ( x + 2 h )= f ( x )+ 2 h f ( x )+
4 h^2
2
f ( x )+
8 h^3
3!
f ( x )+
16 h^4
4!
f (4)"( x )+ (2)
``````
``````f ( x + 2 h ) 4 f ( x + h )= 3 f ( x ) 2 h f ( x ) a +
4 h^3
3!
f ( x )+
``````
###### 4!
``````f (4)( x )+ (3)
``````
``````f ( x )=
3 f ( x )+ 4 f ( x + h ) f ( x + 2 h )
2 h
``````
###### +
``````4 h^2
3!
``````
``````f ( x )+
``````
###### 4!
``````h^3 f (4)( x )+ (4)
``````
``````By passing h /2 to h , it yields
``````
``````f ( x )= D (
h
2
``````
###### )+
``````h^2
3!
f ( x )+
``````
###### 8
``````h^3
4!
f (4)( x )+ (5)
``````

(4) 4 (5) gives us:

``````3 f ( x )= 4 D ( h /2) D ( h )+ f
``````
###### 4!
``````h^3 f (4)( x )+.
``````

truncation error being O ( h^3 ), the better approximate is

``````4
3
``````
###### [ D
``````
h
2
``````
`````` D ( h )]
``````
##### 3

(a)

###### I =
``````Z b
``````
``````a
``````
``````f ( x ) d x  c 1 f ( x )+ c 2 f ( x )
``````
``````=
``````
``````b  a
2
f
``````
`````` b  a
2
``````
``````

``````
###### 1
``````p
3
``````
###### +
``````b + a
2
``````
###### +
``````b  a
2
f
``````
`````` b  a
2
``````
###### 1
``````p
3
``````
###### +
``````b + a
2
``````

Let, a =0, b =1,

``````x 1
``````
###### 1
``````p
3
``````
###### 2
``````p
3
``````
``````x 2 =
``````

p 3 + 1 2 then I

###### 2
``````cos[(
``````
###### 2
``````p
3
``````
``````)^2 ]+cos
``````
###### 2
``````p
3
``````
###### 0.9058564.

(b) According to "CS3b.py", we need 21 sub-intervals for the composite Simpons method to approximate Z 10 0

``````x
1 + x
d x
``````

to within 10^4. (c) We want Z b a

``````= h
``````
``````
w 1 f ( a )+ w 2 f ( a +
b  a
4
)+ w 3 f ( b )
``````

to hold for polynomials 1, x , x^2 ,. Plugging these into the formula, we obtain:

``````f ( x )= 1
``````
``````Z b
``````
``````a
``````
``````1 d x = b  a =
b  a
4
( w 1 + w 2 + w 3 )
``````
``````f ( x )= x
``````
``````Z b
``````
``````a
``````
``````xd x =
``````
###### 2
``````( b^2  a^2 )=
b  a
4
``````
``````
aw 1 + w 2 ( a +
b  a
4
``````
``````+ bw 3 )
``````
``````f ( x )= x^2
``````
``````Z b
``````
``````a
``````
``````x^2 d x =
``````
###### 3
``````( b^3  a^3 )=
b  a
4
``````
``````
a^2 w 1 +( a +
b  a
4
``````
``````)^2 w 2 + b^2 w 3
``````
``````f ( x )= x^3
``````
``````Z b
``````
``````a
``````
``````x^3 d x =
``````
###### 4
``````( b^4  a^4 )=
``````
``````b  a
4
``````
``````
a^3 w 1 +( a +
``````
``````b  a
4
)^3 w 2 + b^3 w 3
``````
``````According to the first three equations, it can be solved:
``````
``````w 1 + w 2 + w 3 = 4
``````
``````2( b + a )= aw 1 +
3 a + b
4
w 2 + bw 3
``````
###### 3
``````( b^2 + a^2 + ab )= a^2 w 1 +
``````
`````` 3 a + b
4
``````
###### 2
``````w 2 + b^2 w 3
``````
``````We get
w 1 =
``````
###### 3
``````, w 2 =
``````
###### 9
``````, w 3 =
``````
###### 9

``````Z b
``````
``````a
``````
``````f ( x ) d x =
b  a
4
``````
###### 3
``````f ( a )+
``````
###### 9
``````f ( a +
b  a
4
``````
###### 9
``````f ( b )
``````

The accuracy of this quadrature formula is n =2, since this formula holds true for polyno- mials 1, x , x^2. (d) Z 12

``````0
``````
###### Z 12
``````0
``````
###### 5
``````x^5
``````
``````12
0 =
``````
##### 4

(a) d y d t

``````= t^2 y ,
d y
y
``````
``````= t^2 d t , y = C et
``````

(^3) / Since the initial condition y (0)= 1 which implies that C = 1 Hence y = et (^3) / (i)The Forward Euler method: yn + 1 = yn + h f ( tn , yn ) Take h =1: y 1 = y + 0 + h f ( t 0 , y 0 )=1; y 2 = y 1 + h f ( t 1 , y 1 )= 1 + 1 =2; y 3 = y 2 + h f ( t 2 , y 2 )= 2 + 8 =10. (ii)Adams-Bashforth: Take h =1: y 2 = y 1 +1.5 h f ( t 1 , y 1 )0.5 h f ( t 0 , y 0 )= 1 +1.5=2.5;

``````y 3 = y 2 +1.5 h f ( t 2 , y 2 )0.5 h f ( t 1 , y 1 )=2.5+1.5 5 2.50.5 1 = 16
``````
``````(b) Backward Euler will be better, since the advantage of forward Euler is that give us an
explicit update equation, so it is easily to implement in practices. In the other hand, backward
Euler require soling an implicit equation. so it is more "expensive", but it has greater stability
properties.
(c) A multistep is constant.If
lim
h  0
( i ( h ))= 0
for each index i.
wi + 1 =
``````
###### 3
``````wi
``````
###### 3
``````wi  1 +
``````
###### 3
``````h f ( ti , wi )
where i ( h ) is the local truncation error. If in each step, there is a round-off error  , w 1 = 1 +  ,
then
``````
``````w 2 =
``````
###### 3
``````w 1
``````
###### 3
``````
``````
``````w 3 =
``````
###### 3
``````w 2
``````
###### 3
``````w 1 =
``````
###### 3
`````` )
``````
###### 3
``````(1+  )= 1 +
``````
###### 9
``````
``````
``````w 4 =
``````
###### 3
``````w 3
``````
###### 3
``````w 2 =
``````
###### 9
`````` )
``````
###### 3
`````` )= 1 +
``````
###### 27
``````.
``````
``````We will find the error grow with time. The characteristic polynomial of the method is
``````
``````P (  )= ^2 +
``````
###### 3
`````` +
``````
###### .
``````Since^43 >1. It doesnt satisfy the root condition, it is unstable.
``````

#### Appendix

Python code in file "CS3.py" is 1 # –– coding: utf -8 –– 2 """ 3 Created on Thu Apr 28 18:16:21 2022 4 5 @author: 13791 6 """ 7 import math 8 #function 9 def fun(x): 10 return x/(1+x) 11 #Compound trapezoid 12 def tx(a,b,n): 13 h=(b-a)/n 14 x=a 15 s=fun(x)-fun(b)

16 for k in range(1,n+1): 17 x=x+h 18 s=s+2fun(x) 19 result =(h/2)s 20 return result 21 #Composite Simpson 22 def xps(a,b,n): 23 h=(b-a)/n 24 x=a 25 s=fun(x)-fun(b) 26 for k in range(1,n+1): 27 x=x+h/ 28 s=s+4fun(x) 29 x=x+h/ 30 s=s+2fun(x) 31 result =(h/6)*s 32 return result 33 a= 34 b= 35 n= 36 t=tx(a,b,n) 37 p=xps(a,b,n) 38 for i in range (1,25): 39 n = i 40 p = xps(a, b, n) 41 print ("when n is",n,"The approximate solution is",p,"The error is", abs(p -(10.0 – math.log (11))))