Professional Documents
Culture Documents
Đ Xuân Trư ng-IEIESB21003-Lab4
Đ Xuân Trư ng-IEIESB21003-Lab4
import numpy as np
import matplotlib.pyplot as plt
from numpy.linalg import norm
#######################################################
#slope of the solution from the given differential equation
def f(x,y):
return 7 * np.exp(4*x) - 3 * y
########################################################
#true solution
def true_sol(x):
return np.exp(4*x) + np.exp(-3*x)
y_true =true_sol(x) #true value of y(x)
#######################################################
#Use Euler's method to find a numerical solution
#######################################################
#plotting
#compare true and approximate value
plt.figure()
plt.plot(x,y_true,linewidth=5, color = "yellow")
plt.plot(x,y_Euler, 'b--')
plt.xlabel('x')
plt.legend(["'approximation solution with Euler method'", "exact
solution"], loc="lower left")
plt.title(f"Comparison the true solution and approximation solution
with h = {h}")
#display error
plt.figure()
plt.show()
#######################################################
#compute global relative error
ErrorG_Euler = norm(error_Euler)/norm(y_true)
print("h = ", h)
print("ErrorG_Euler = ", ErrorG_Euler )
Question 2:
import numpy as np
import matplotlib.pyplot as plt
from numpy.linalg import norm
def f(t, y): # Slope of the solution from the given differential
equation
return 7 * np.exp(4 * t) - 3 * y
def true_sol(t):
return np.exp(4 * t) + np.exp(-3 * t)
# Parameters
a = 0 # Starting time
b = 0.4 # Ending time
h = 0.1 # Step size
n = int((b - a) / h) + 1 # Number of points in the interval
t = np.linspace(a, b, n) # Partition of the interval
y_true = true_sol(t) # True value of y(t)
# Modified Euler's method
y_mEuler = np.zeros(n) # Initialize an array to store values of
approximation of y(t)
y_mEuler[0] = 2 # Initial condition
# Evaluate error
error_mEuler = np.abs(y_mEuler - y_true)
# Plotting
plt.figure()
plt.plot(t, y_true, linewidth=5, color="yellow")
plt.plot(t, y_mEuler, 'b--')
plt.xlabel('t')
plt.legend(["Approximation solution with Modified Euler method", "Exact
solution"], loc="best")
plt.title(f"Comparison of the true solution and approximation solution
with h = {h}")
plt.figure()
plt.plot(t, error_mEuler)
plt.title("Error in approximation using Modified Euler method")
plt.xlabel('t')
plt.show()
Question 3:
########################################################
#Slope function and true solution
def f(x,y): #slope of the solution from the given differential equation
return 7 * np.exp(4*x) - 3 * y
def true_sol(x):
return np.exp(4*x) + np.exp(-3*x)
y_true =true_sol(x) #true value of y(x)
########################################################
for i in range(n-1):
k1 = f(x[i],y_iEuler[i]) #slope at the beginning of the period
y_temp = y_iEuler[i] + h*k1 #temporary value of y at the end of the
period
k2 = f(x[i+1],y_temp) #temporary value of y' at the end of the period
y_iEuler[i+1] = y_iEuler[i]+ 0.5*h*(k1+k2)#update approximate value
of y in the next step
########################################################
#Evaluate error
error_iEuler = np.abs(y_iEuler-y_true) #evaluate error
########################################################
#plotting
#compare true and approximation solution
plt.figure()
plt.plot(x,y_true,linewidth=5, color = "yellow")
plt.plot(x,y_iEuler, 'b--')
plt.xlabel('x')
plt.legend(["'approximation solution with modifiedEuler method'",
"exact solution"], loc="lower left")
plt.title(f"Comparison the true solution and approximation solution
with h = {h}")
#display error
plt.figure()
plt.plot(x, error_iEuler)
plt.plot(x, error_mEuler)
plt.plot(x, error_Euler)
plt.show()
########################################################
#compute global relative error
ErrorG_iEuler = norm(error_iEuler)/norm(y_true)
print("h = ", h)
print("ErrorG_iEuler = ", ErrorG_iEuler )
Question 4:
import numpy as np
return t, y
def true_solution(t):
return np.exp(-t**3)
def euler_method(f, a, b, y_initial, h):
n = int((b - a) / h)
t = np.linspace(a, b, n + 1)
y = np.zeros(n + 1)
y[0] = y_initial
for i in range(n):
y[i + 1] = y[i] + h * f(t[i], y[i])
return t, y
def modified_euler_method(f, a, b, y_initial, h):
n = int((b - a) / h)
t = np.linspace(a, b, n + 1)
y = np.zeros(n + 1)
y[0] = y_initial
for i in range(n):
k1 = f(t[i], y[i])
k2 = f(t[i] + h, y[i] + h * k1)
y[i + 1] = y[i] + h * (k1 + k2) / 2
return t, y
for i in range(n):
k1 = f(t[i], y[i])
k2 = f(t[i] + h, y[i] + h * k1)
y[i + 1] = y[i] + h * (k1 + k2) / 2
return t, y
# Euler's method
t_euler, y_euler = euler_method(func, 0, 0.4, 1, h)
euler_approx = y_euler[-1]
euler_absolute_error = np.abs(euler_approx - rk4_true)
euler_relative_error = euler_absolute_error / rk4_true