Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
graphing euler method
#2
I slightly rewrote your code, you can adapt it for your needs.

import numpy as np
import matplotlib.pylab as plt

def volterra(P, K=1, r=1):
    return r * P * (1 - P / K)

def euler(fun, Po, h, N, **kwargs):
    acc = [Po]
    times = np.cumsum(np.r_[0, np.ones(N) * h])
    for _ in range(N):
        acc.append(acc[-1] + h * fun(acc[-1], K=K, r=r))  # for autonomous system's only
    return np.array(acc), times

K = 12
r = 0.43
Po = 1
N = 30;
step_values = [1, 0.5, 0.1]
fig = plt.figure()
ax = fig.add_subplot(111)
for h, c in zip(step_values, 'rgb'):
    Ps, Ts = euler(volterra, Po, h, N, K=K, r=r)
    ax.plot(Ts, Ps, c)

plt.show( )   
Reply


Messages In This Thread
graphing euler method - by rondo - May-02-2019, 08:13 PM
RE: graphing euler method - by scidam - May-03-2019, 01:03 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Forcing matplotlib to NOT use scientific notation when graphing sawtooth500 4 353 Mar-25-2024, 03:00 AM
Last Post: sawtooth500
  Calculate the Euler Number with more decimal places Pedroski55 10 4,497 Oct-31-2021, 04:45 AM
Last Post: Pedroski55
  graphing inequalities Dasiey12 0 3,464 Mar-30-2021, 01:47 AM
Last Post: Dasiey12
  IndexError in Project Euler attempt CRISPRmetoopls 3 2,309 Aug-10-2020, 10:00 AM
Last Post: perfringo
  Help Graphing Arduino Data Real Time in Python nschulz 0 2,532 Mar-12-2020, 06:15 PM
Last Post: nschulz
  Matplotlib graphing help (dual y axis, groupby, filter) keml 0 2,194 Feb-07-2020, 02:35 AM
Last Post: keml
  Graphing from a while loop McDweevo 1 2,873 Nov-13-2019, 08:02 PM
Last Post: micseydel
  Graphing three database in one graph Python r_e 1 2,369 Jun-20-2019, 05:10 PM
Last Post: r_e
  3rd problem from project Euler Richard_SS 6 3,183 Jun-05-2019, 02:06 PM
Last Post: DeaD_EyE
  Graphing process execution time Temennigru 4 4,771 Nov-15-2017, 11:54 PM
Last Post: Temennigru

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020