2. [30pts] Consider the initial value problem (predator-prey model) d [u][0.8u 0
ID: 3184527 • Letter: 2
Question
2. [30pts] Consider the initial value problem (predator-prey model) d [u][0.8u 0.0[(0)]_[10 We want to simulate the solution of (5) in the time interval [0,40] using N = 100 constant time steps of length t = h. Explicit Euler's method defines numerical approximations at tn+1 = (n + 1)h by 10 1101 1 60 V0 a) Write a Python file pb2.py implementing the explicit Euler solution and plotting the solution b) Compare the solution obtained in a) (with N 100) with the solution obtained using N = 1000, what happens?Explanation / Answer
a)
Python Code
-----------------------------------------------------------------------------------
import numpy as np
from matplotlib import pyplot as plt
u0=10
v0=60
n1=100
n2=1000
h1=(n1)/40
h2=n2/40
t1=np.linspace(0,40,n1)
t2=np.linspace(0,40,n2)
u1=np.zeros([n1])
u2=np.zeros([n2])
v1=np.zeros([n1])
v2=np.zeros([n2])
u1[0]=u0
u2[0]=u0
v1[0]=v0
v2[0]=v0
for i in range(0,n1):
u1[i+1]=u1[i]+h1*(0.8*u1[i]-0.01*u1[i]*v1[i])
for i in range(0,n1):
v1[i+1]=v1[i]+h1*(0.02*u1[i]*v1[i]-0.4*v1[i])
for i in range(0,n1):
u2[i+1]=u2[i]+h2*(0.8*u2[i]-0.01*u2[i]*v2[i])
for i in range(0,n2):
v2[i+1]=v2[i]+h2*(0.02*u2[i]*v2[i]-0.4*v2[i])
solarray1=np.array([u1,v1])
solarray2=np.array([u2,v2])
for i in range(n1)
print(u1[i],v1[i])
for i in range(n2)
print(u2[i],v2[2])
plt.plot(t1,u1,'o')
plt.plot(t1,v1,'x')
plt.plot(t2,u2,'o','r')
plt.plot(t2,u2,'x','g')
plt.show()
--------------------------------------------------------------------------------------------------
b) As the number of iterations are increased, i.e., N, the step size decreases. This ensures the better accuracy of the end result. But increasing N also means number of times one has to compute values for the expression to get next iteration. That increases the effort and also baseless for doing it for very small values of h as it will slows down the process. One should always choose value of N or the step size keeping in mind both things accuracy and time.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.