1 import matplotlib.
pyplot as plt
2 import numpy as np
3 #[Link]('text', usetex=True)
4 #[Link]('font', family='serif')
5
6 "first, let's define some basic functions"
7 pi= [Link]
8 def sqrt(x): return [Link](x)
9 def exp(x): return [Link](x)
10 def sin(x): return [Link](x)
11 def cos(x): return [Link](x)
12 def log(x): return [Link](x)
13 def poly0(x): return (pow(x,3)/6.) + (pow(x,4)/8.0)
1 """
2 *****************************************
3 *****************************************
4 solving linear differential equations
5 *****************************************
6 *****************************************
7 """
8
9 def lin_diff_eq(g, f0, xs, a):
10
11 """
12 this solves the diff. eq. df/dx = g(x) by discretizing it
13 (f(x+a) - f(x-a))/2a = g(x)
14 f(x+a) = f(x-a) + 2*a*g(x)
15
16 or equivalently
17
18 f(x+2 a) = f(x) + 2*a*g(x+a)
19
20 we put the list of points into fs
21
22 we initiate this with
23 the initial value of f, which is f0 = f(xs[0])
24
25 """
26
27 fs=[]
28
29 "note in this loop, we skip over the first element"
30 for i0 in range(1,len(xs)):
31
32 "we grab the previous term in the list"
33 f0 =
34
35 """
36 we need the derivative at x+a,
37 since xs[i0] = x+2*a
38 we can use the fact that x+a = xs[i0]-a
39 """
40 df =
41
42 "we add these together"
43
44
45 return fs
1 def master_diff_eq():
2
3 [Link](figsize=(9.0,5))
4 [Link](211)
5 [Link](fontsize=15)
6 [Link](fontsize=15)
7
8 [Link](ylabel1,size=25)
9
10 [Link](xs, ydata1, color='r')
11
12 [Link](x2, ydata2, markersize=8,fmt='o',color='g',mfc='white',mec='g', elinewidth=2, capsize=4, mew=1.4)
13
14 [Link](x=0,color='k',linewidth=1)
15
16 [Link](212)
17 [Link](fontsize=15)
18 [Link](fontsize=15)
19
20 [Link](y=0,color='k',linewidth=1)
21 [Link](x=0,color='k',linewidth=1)
22
23 [Link](ylabel2,size=20)
24 [Link](xs, ydata3, color='g')
25
26 [Link](xlabel0,size=20, position=(1,1.2))
27
28
1 a = pow(10,-3)
2 xs=[Link](0,5, 2*a)
3 x2=[Link](0,5, .5)
4
5 print("a",a)
6 """
7 ******************************
8 ******************************
9 g(x) = exp(x)
10 f(x) = exp(x)
11 ******************************
12 ******************************
13 """
14
15 fs = lin_diff_eq(, , , )
16 ydata1 = fs
17 ydata2 = exp(x2)
18 ydata3 = exp(xs)
19 ylabel1 = "f(x)"
20 ylabel2 = "g(x) = exp(x)"
21 xlabel0 = "x"
22 master_diff_eq()
1 """
2 ******************************
3 ******************************
4 g(x) = sin(x)
5 f(x) = -cos(x)
6 ******************************
7 ******************************
8 """
9
10 fs = lin_diff_eq(, , , )
11 ydata1 = fs
12 ydata2 = -cos(x2)
13 ydata3 = sin(xs)
14 ylabel1 = "f(x)"
15 ylabel2 = "g(x) = sin(x)"
16 xlabel0 = "x"
17
18 master_diff_eq()
1 """
2 ******************************
3 ******************************
4 g(x) = cos(x)
5 f(x) = sin(x)
6 ******************************
7 ******************************
8 """
9
10 fs = lin_diff_eq(, , , )
11 ydata1 = fs
12 ydata2 = sin(x2)
13 ydata3 = cos(xs)
14 ylabel1 = "f(x)"
15 ylabel2 = "g(x) = cos(x)"
16 xlabel0 = "x"
17
18 master_diff_eq()
1 """
2 *****************************************
3 *****************************************
4 solving second differential equations
5 *****************************************
6 *****************************************
7 """
8
9 def second_diff_eq(h, f0, g0, ts, a):
10
11 """
12 this solves the diff. eq. d^2f/dx^2 = h(x) by discretizing
13 re-writting it as a coupled linear equations
14
15 dg/dt = h(t)
16 df/dt = g(t)
17
18 g(t + 2*a) = g(t) + 2 * a * h(t + a)
19 f(t + 2*a) = f(t) + 2 * a * g(t + a)
20
21 we put the list of points into fs, gs
22
23 we initiate this with
24 the initial value of f, which is f0 = f(ts[0])
25 the initial value of g, which is g0 = g(ts[0])
26
27 note, we need to calculate g(t + a), but we have g(t + 2*a) and g(t)
28 we will estimate this with the average of these two, which we will call
29
30 gbar = ( g(t + 2*a) + g(t) ) / 2
31 """
32
33 fs=
34 gs=
35
36 "note in this loop, we skip over the first element"
37 for i0 in range(1,len(ts)):
38 "we start with g(t): we grab the previous term in the list"
39 g0 =
40 "the shift in g"
41 dg =
42
43 [Link]( )
44
45 "next we calculate f(t): we grab the previous term in the list"
46 f0 =
47 "we estimate g(t+a) with the average of the last two point in gs"
48 gbar =
49 df =
50
51 "we add these together"
52 [Link]()
53
54 return fs, gs
1 def master_sec_diff_eq():
2 [Link](figsize=(9.0,8))
3
4 "x(t) plots"
5 [Link](311)
6 [Link](fontsize=15)
7 [Link](fontsize=15)
8
9 [Link](ylabel1,size=25)
10
11 [Link](ts, ydata1, color='r')
12 [Link](t2, ydata2, markersize=8,fmt='o',color='g',mfc='white',mec='g', elinewidth=2, capsize=4, mew=1.4)
13
14 [Link](x=0,color='k',linewidth=1)
15
16 "v(t) plots"
17 [Link](312)
18 [Link](fontsize=15)
19 [Link](fontsize=15)
20
21 [Link](x=0,color='k',linewidth=1)
22
23 [Link](ylabel2,size=20)
24 [Link](ts, ydata3, color='r')
25 [Link](t2, ydata4, markersize=8,fmt='o',color='g',mfc='white',mec='g', elinewidth=2, capsize=4, mew=1.4)
26
27 "a plots"
28 [Link](313)
29 [Link](fontsize=15)
30 [Link](fontsize=15)
31
32 [Link](y=0,color='k',linewidth=1)
33 [Link](x=0,color='k',linewidth=1)
34
35 [Link](ylabel3,size=20)
36 [Link](ts, ydata5, color='b')
37
38 [Link](xlabel0,size=20, position=(1,1.2))
1 a = pow(10,-3)
2 ts=[Link](0,5, 2*a)
3 t2=[Link](0,5, .5)
4
5 print("a",a)
6
7
8 def x_func(t, x0, v0, a0):
9 x1 = v0 * t
10 x2 = a0 * pow(t,2)/2.0
11 return x0 + x1 + x2
12
13 def v_func(t, v0, a0):
14 return v0 + a0 * t
15
16
17 """
18 ******************************
19 ******************************
20 a(t) = 0
21 x(t) = v0*t
22 ******************************
23 ******************************
24 """
25
26 x0, v0 =
27 def a_func(t): return
28
29 xs, vs = second_diff_eq(, , , , )
30 ydata1 = xs
31 ydata2 = x_func(t2, x0, v0, a_func(ts))
32 ydata3 = vs
33 ydata4 = v_func(t2, v0, a_func(ts))
34 ydata5 = a_func(ts)*[Link](len(ts))
35 ylabel1 = r'$x(t) $ '
36 ylabel2 = r'$v(t)$ '
37 ylabel3 = r'$a(t) = 0$ '
38 xlabel0 = r'$t$'
39
40 master_sec_diff_eq()
1 """
2 ******************************
3 ******************************
4 a(t) = -9.8
5 x(t) = v0*t + a t^2/2
6 ******************************
7 ******************************
8 """
9
10 x0, v0 =
11 def a_func(t): return
12
13 xs, vs = second_diff_eq(, , , , )
14 ydata1 = xs
15 ydata2 = x_func(t2, x0, v0, a_func(ts))
16 ydata3 = vs
17 ydata4 = v_func(t2, v0, a_func(ts))
18 ydata5 = a_func(ts)*[Link](len(ts))
19 ylabel1 = r'$x(t) $ '
20 ylabel2 = r'$v(t)$ '
21 ylabel3 = r'$a(t) = -9.8$ '
22 xlabel0 = r'$t$'
23
24 master_sec_diff_eq()
1 Start coding or generate with AI.