-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathLab-9.py
More file actions
212 lines (180 loc) · 4.65 KB
/
Lab-9.py
File metadata and controls
212 lines (180 loc) · 4.65 KB
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
"""Converted from Lab-9.ipynb"""
# 1. dy/dx − 2y = 3ex with y(0) = 0 using Taylor series method at x = 0.1(0.1)0.3.
from numpy import array, zeros, exp
def taylor(deriv, x, y, xStop, h):
X = []
Y = []
X.append(x)
Y.append(y)
while x < xStop:
D = deriv(x, y)
H = 1.0
for j in range(3):
H = H * h / (j + 1)
y = y + D[j] * H
x = x + h
X.append(x)
Y.append(y)
return array(X), array(Y)
def deriv(x, y):
D = zeros((4, 1))
D[0] = 2 * y[0] + 3 * exp(x)
D[1] = 4 * y[0] + 9 * exp(x)
D[2] = 8 * y[0] + 21 * exp(x)
D[3] = 16 * y[0] + 45 * exp(x)
return D
x = 0.0
xStop = 0.3
y = array([0.0])
h = 0.1
X, Y = taylor(deriv, x, y, xStop, h)
for i in range(len(X)):
print("The required values are: at x=%0.2f, y=%0.5f" % (X[i], Y[i]))
# Q3
from numpy import array, zeros
def taylor(deriv, x, y, xStop, h):
X = []
Y = []
X.append(x)
Y.append(y)
while x < xStop:
D = deriv(x, y)
H = 1.0
for j in range(3):
H = H * h / (j + 1)
y = y + D[j] * H
x = x + h
X.append(x)
Y.append(y)
return array(X), array(Y)
def deriv(x, y):
D = zeros((4, 1))
D[0] = x - y[0]
D[1] = 1 - y[0]
D[2] = -1
D[3] = 0
return D
x = 0.0
xStop = 0.1
y = array([1.0])
h = 0.1
X, Y = taylor(deriv, x, y, xStop, h)
for i in range(len(X)):
print("At x = %0.2f, y = %0.5f" % (X[i], Y[i]))
# Q4.
from numpy import array, zeros
def taylor(deriv, x, y, xStop, h):
X = []
Y = []
X.append(x)
Y.append(y)
while x < xStop:
D = deriv(x, y)
H = 1.0
for j in range(3):
H = H * h / (j + 1)
y = y + D[j] * H
x = x + h
X.append(x)
Y.append(y)
return array(X), array(Y)
def deriv(x, y):
D = zeros((4, 1))
D[0] = x + y[0]**2
D[1] = 1 + 2*y[0]
D[2] = 2
D[3] = 0
return D
x = 0.0
xStop = 0.1
y = array([1.0])
h = 0.1
X, Y = taylor(deriv, x, y, xStop, h)
for i in range(len(X)):
print("At x = %0.2f, y = %0.5f" % (X[i], Y[i]))
# 2. To solve the ODE of the form dy
# dx = f(x, y) with initial conditions y(x0) = y0. The iterative
# formula is given by : y(x(i+1) = y(xi) + hf(xi
# , y(xi)).
# Solve: y
# ′ = e
# −x with y(0) = −1 using Euler’s method at x = 0.2(0.2)0.6
import numpy as np
import matplotlib.pyplot as plt
# Define parameters
f = lambda x, y: np.exp(-x)
h = 0.2
y0 = -1
n = 3
# Explicit Euler Method
y = np.zeros(n + 1)
x = np.zeros(n + 1)
y[0] = y0
x[0] = 0
for i in range(0, n):
x[i + 1] = x[i] + h
y[i + 1] = y[i] + h * f(x[i], y[i])
print("The required values are at x = {0:0.2f}, y = {1:0.5f}, x = {2:0.2f}, y = {3:0.5f}, x = {4:0.2f}, y = {5:0.5f}, x = {6:0.2f}, y = {7:0.5f}".format(x[0], y[0], x[1], y[1], x[2], y[2], x[3], y[3]))
# Plot the results
plt.plot(x, y, 'bo--', label='Approximate')
plt.plot(x, -np.exp(-x), 'g*-', label='Exact')
plt.title('Approximate and Exact Solution')
plt.xlabel('x')
plt.ylabel('f(x)')
plt.grid()
plt.legend(loc='best')
plt.show()
# 3. Solve: y′ = −2y + x3e −2x with y(0) = 1 using Euler’s method at x = 0.1, 0.2.
import numpy as np
import matplotlib . pyplot as plt
# Define parameters
f = lambda x , y: -2*y+( x ** 3 )*np .exp(-2*x ) # ODE
h = 0.1 # Step size
y0 = 1 # Initial Condition
n=2
# Explicit Euler Method
y[0] = y0
x[0]=0
for i in range (0 , n ):
x[i+1]=x[i]+h
y[i + 1] = y[i] + h*f ( x[i], y[i])
print ("The required values are at x= %0.2f , y=%0.5f , x=%0.2f , y=%0.5f ,x=%0.2f , y=%0.5f\n\n"%( x[0],y[0],x[1],y[1],x[2],y[2]) )
plt . plot (x , y , 'bo--', label =" Approximate ( Euler 's method )")
plt . title (" Solution by Euler 's method ")
plt . xlabel ('x')
plt . ylabel ('f(x)')
plt . grid ()
plt . legend ( loc ='best')
plt . show ()
# 9.4. Solve y
# ′ = −ky with y(0) = 100 using modified Euler’s method at x = 100, by taking
# h = 25.
import numpy as np
import matplotlib . pyplot as plt
def modified_euler (f , x0 , y0 , h , n ):
x = np . zeros ( n+1 )
y = np . zeros ( n+1 )
x[0] = x0
y[0] = y0
for i in range ( n ):
x[i+1] = x[i] + h
k1 = h * f ( x[i], y[i])
k2 = h * f ( x[i+1], y[i] + k1 )
y[i+1] = y[i] + 0.5 * ( k1 + k2 )
return x , y
def f (x , y ):
return -0.01 * y # ODE dy/dx = -ky
x0 = 0.0
y0 = 100.0
h = 25
n = 4
x , y = modified_euler (f , x0 , y0 , h , n )
print ("The required value at x= %0.2f , y=%0.5f"%( x[4],y[4]) )
print ("\n\n")
# Plotting the results
plt . plot (x , y , 'bo--')
plt . xlabel ('x')
plt . ylabel ('y')
plt . title ('Solution of dy/dx = -ky using Modified Euler \'s Method ')
plt . grid ( True )
plt . show ()