-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheuler.py
96 lines (71 loc) · 2.84 KB
/
euler.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
import math
from matplotlib import pyplot
def equation1():
f = lambda t,y: t*y
return f
def equation2():
f = lambda t,y: y*(1+(t**2))
return f
def equation3():
f = lambda t,y: (49/5) - ((3*y)/20)
return f
def equation4():
f = lambda t,y: t+1+(1/t)-((2*y)/t)
return f
def equation5():
f = lambda t,V: (-1400) + (t*math.exp(-t)) - (V/0.05)
return f
def equation6():
f = lambda t,V: (-1360) + (math.exp(-t/(0.03))) - (math.exp(-t/(0.04)))- (V/0.05)
return f
def Euler(f, t0, tf, y0, n):
h = (tf -t0)/n
t = []
y = []
t.append(t0)
y.append(y0)
for i in range(n+1):
tx = t[i]+h
t.append(tx)
y1 = y[i]+(h*(f(t[i], y[i])))
y.append(y1)
for j in range(n+1):
print("%0.1f %f"%(t[j], y[j]))
pyplot.plot(t, y, 0)
pyplot.xlabel("Value of t")
pyplot.ylabel("Value of y")
pyplot.title("Approximate Solutions of IVPs using Euler's Method")
pyplot.show()
return
def main():
try:
print("\n 1: dy/dt = yt\n 2: dy/dt=y(1+t^2)\n 3: dy/dt = 9.8 - 0.15y\n 4: t(dy/dt) + 2y = t^2-t+1\n 5: dV/dt + (1/0.05)V = -1400 +te^-t\n 6: dV/dt + (1/0.05)V = -1360+e^(-t/0.03)-e(-t/0.04)\n")
response = input("Please select a sample equation from the above options\nEnter number or enter 'q' to quit: ")
while(response != "q"):
response = int(response)
a = float(input("\nPlease enter 't0': "))
c = float(input("\nPlease enter 'y(t0)': "))
b = float(input("\nPlease enter 'y(tfinal)': "))
n = int(input("\nPlease enter 'Number of steps (n)': "))
if(0<response<7):
if(response == 1):
Euler(equation1() , a, b, c, n)
elif(response == 2):
Euler(equation2() , a, b, c, n)
elif(response == 3):
Euler(equation3() , a, b, c, n)
elif(response == 4):
Euler(equation4() , a, b, c, n)
elif(response == 5):
Euler(equation5() , a, b, c, n)
elif(response == 6):
Euler(equation6() , a, b, c, n)
else:
print("Wrong input")
print("-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-")
print("\n 1: dy/dt = yt\n 2: dy/dt=y(1+t^2)\n 3: dy/dt = 9.8 - 0.15y\n 4: t(dy/dt) + 2y = t^2-t+1\n 5: dV/dt + (1/0.05)V = -1400 +te^-t\n 6: dV/dt + (1/0.05)V = -1360+e^(-t/0.03)-e(-t/0.04)\n")
response = input("Please select a sample equation from the above options\nEnter number or enter 'q' to quit: ")
except ValueError:
print("\nError: Invalid Input\n")
if __name__ == "__main__":
main()