|
| 1 | +import numpy as np |
| 2 | +import matplotlib.pyplot as plt |
| 3 | +from Nievergelt import nievergelt |
| 4 | +from ForwardEuler import forwardEuler |
| 5 | + |
| 6 | +# Define the RHS of the ODE problem |
| 7 | +f = lambda t, u: np.cos(t) * np.exp(-u) |
| 8 | +u0 = np.log(2) # Initial solution |
| 9 | +T = 2 * np.pi # Final time |
| 10 | +N = 10 # Number of subintervals |
| 11 | +nSteps = 100 # Fine steps per subinterval |
| 12 | + |
| 13 | +# Approximate prediction using Forward Euler |
| 14 | +tPred, uPred = forwardEuler(f, [0, T], u0, N) |
| 15 | + |
| 16 | +# First set of Nievergelt's method parameters |
| 17 | +Mn = 2 |
| 18 | +width = 0.2 |
| 19 | +solver = lambda t0, t1, u0: forwardEuler(f, [t0, t1], u0, nSteps)[-1].ravel() |
| 20 | +U, uTraj = nievergelt(solver, T, u0, N, uPred, Mn, width) |
| 21 | + |
| 22 | +# Fine solution for comparison |
| 23 | +tFine, uFine = forwardEuler(f, [0, T], u0, N * nSteps) |
| 24 | + |
| 25 | +# Plot the first set of results |
| 26 | +plt.figure(1) |
| 27 | +plt.plot(tFine, uFine, label='Fine', zorder=3) |
| 28 | +plt.xlabel('Time') |
| 29 | +plt.ylabel('Solution') |
| 30 | +plt.plot(tPred, uPred, 'o', label='Prediction', zorder=2) |
| 31 | +plt.plot(tPred, U, '^', label='Nievergelt', zorder=2) |
| 32 | + |
| 33 | +# Plot trajectories |
| 34 | +TT = np.linspace(0, T, N + 1) |
| 35 | +x = uTraj[0, 0, :] |
| 36 | +plt.plot(np.linspace(TT[0], TT[1], nSteps + 1), x, 'k--', label='Trajectories', zorder=1) |
| 37 | + |
| 38 | +for n in range(1, N): |
| 39 | + for m in range(Mn): |
| 40 | + x = uTraj[n, m, :] |
| 41 | + plt.plot(np.linspace(TT[n], TT[n + 1], nSteps + 1), x, 'k--', zorder=1, alpha=0.7) |
| 42 | + |
| 43 | +plt.legend(loc='upper right') |
| 44 | +plt.gca().tick_params(labelsize=12) |
| 45 | +plt.savefig('NievergeltExampleNonLinear_1.eps', format='eps') |
| 46 | +plt.show() |
| 47 | + |
| 48 | +# Second set of Nievergelt's method parameters |
| 49 | +Mn = 4 |
| 50 | +width = 0.4 |
| 51 | +U, uTraj = nievergelt(solver, T, u0, N, uPred, Mn, width) |
| 52 | + |
| 53 | +# Fine solution for comparison |
| 54 | +tFine, uFine = forwardEuler(f, [0, T], u0, N * nSteps) |
| 55 | + |
| 56 | +# Plot the second set of results |
| 57 | +plt.figure(2) |
| 58 | +plt.plot(tFine, uFine, label='Fine', zorder=3) |
| 59 | +plt.xlabel('Time') |
| 60 | +plt.ylabel('Solution') |
| 61 | +plt.plot(tPred, uPred, 'o', label='Prediction', zorder=2) |
| 62 | +plt.plot(tPred, U, '^', label='Nievergelt', zorder=2) |
| 63 | + |
| 64 | +# Plot trajectories |
| 65 | +x = uTraj[0, 0, :] |
| 66 | +plt.plot(np.linspace(TT[0], TT[1], nSteps + 1), x, 'k--', label='Trajectories', zorder=1) |
| 67 | + |
| 68 | +for n in range(1, N): |
| 69 | + for m in range(Mn): |
| 70 | + x = uTraj[n, m, :] |
| 71 | + plt.plot(np.linspace(TT[n], TT[n + 1], nSteps + 1), x, 'k--', zorder=1, alpha=0.7) |
| 72 | + |
| 73 | +plt.legend(loc='upper right') |
| 74 | +plt.gca().tick_params(labelsize=12) |
| 75 | +plt.savefig('NievergeltExampleNonLinear_2.eps', format='eps') |
| 76 | +plt.show() |
0 commit comments