May-02-2019, 08:13 PM
My program should Sketch the graph of ?(?) and the approximations ?ℎ(??) with ℎ=1,0.5,0.1 where p(t) is the approximation using Euler's method. I'm new to python and for some reason, I'm not getting three graphs for the three different h values, how can I fix this?
import numpy as np
import matplotlib.pylab as plt
def Euler():
K = 12; r = 0.43; Po = 1; N=30;
h=[.1,.5,1]
#defining dP/dt as a function f(P)
f = lambda P: r*P*(1-P/K)
P = np.array([])
P = np.append(P,Po) #initializing P with Po
for i in range (len(h)):
for n in range(N+1):#n=0 as index for P[0], then 1<=n<=N
Pn = P[n] + h[i]*f(P[n]) #euler
P = np.append(P,Pn)
plt. plot ( n, P[n] , 'ro' )
plt. xlabel (' Value of n ”' )
plt. ylabel (" Value of p[n] ”")
plt. title (" Approximate Solution with Euler’s Method " )
plt. show ( )
Euler()
