-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTaylor_Series_Method_O(2,4).py
78 lines (57 loc) · 1.59 KB
/
Taylor_Series_Method_O(2,4).py
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
# taylor order 2,4
import matplotlib as plt
from scipy.optimize import curve_fit
import numpy as np
def f(x, y):
# return np.cos(x) + np.sin(y)
return x**2 + y**2
def f1(x, y):
# return -np.sin(x) + np.cos(x)*np.cos(y) + np.sin(y)*np.cos(y)
return 2*x + 2*y*(x**2+y**2)
# def f2(x, y):
# return (-2+x-y)/8
# def f3(x, y):
# return (2-x+y)/16
def SOTSM(a, b, x0, y0, h):
x_list = []
y_list = []
while b > a:
y1 = y0 + h*f(x0, y0) + ((h**2)/2)*f1(x0, y0)
x0 = x0+h
y0 = y1
print("Value of y at x= %0.4f" % x0, " is %0.4f " % y0)
x_list.append(x0)
y_list.append(y0)
b = b-h
return x_list, y_list
# def FOTSM(a, b, x0, y0, h):
# while b > a:
# y1 = y0 + h*f(x0, y0) + ((h**2)/2)*f1(x0, y0) + \
# ((h**3)/6)*f2(x0, y0) + ((h**4)/24)*f3(x0, y0)
# x0 = x0+h
# y0 = y1
# print("Value of y at x= %0.4f" % x0, " is %0.4f " % y0)
# b = b-h
print("Enter the range.")
a = float(input("From : "))
b = float(input("To : "))
x0 = float(input("Enter value of x0: "))
y0 = float(input("Enter value of y0: "))
# N = float(input("Enter intervals: "))
# h = (b-a)/N
h = float(input('Enter the step length : '))
# taylor 2nd order
print("\nTaylor 2nd order \n")
x_list, y_list = SOTSM(a, b, x0, y0, h)
# taylor 4nd order
# print("\nTaylor 4th order \n")
# FOTSM(a, b, x0, y0, h)
x = np.array(x_list)
y = np.array(y_list)
popt, pcov = curve_fit(f, x, y)
y_fit_linear = (x, *popt)
# Plot the results
plt.scatter(x, y)
plt.plot(x, y, label='ODE Solution')
plt.legend()
plt.show()