# how can I plot specific iterations?

I have made this code for an explicit Euler method but I want to plot only the 0,1,25,50 iterations not all of them.How can I do it?

import numpy as np
from scipy import sparse
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
from matplotlib import cm

Nx = 19  # GRID POINTS on space interval
Nt = 800 # GRID POINTS on time interval

a = 0
b = 1

# ----- Spatial discretization step -----
dx = (b - a)/(Nx - 1)

t0 = 0
Tf = 1

# ----- Time step -----
dt = (Tf - t0)/(Nt - 1)

h = (b-a)/(Nx+1)

t = Tf / Nt

m = t/h**2

print("m =", m)

# ----- Creates grids -----
xspan = np.linspace(a, b, Nx)
tspan = np.linspace(t0, Tf, Nt)

# ----- Initialize matrix solution U -----
U1 = np.zeros((Nx, Nt))

# ----- Initial condition -----
U1[:,0] = np.where((xspan>=0) & (xspan<=1/2),2*xspan,2-2*xspan)

for k in range(0, Nt-1):
for i in range(1, Nx-1):
U1[i, k+1] = m*U1[i-1, k] + (1-2*m)*U1[i,k] + m*U1[i+1,k]

X1, T1 = np.meshgrid(tspan, xspan)

plt.figure(figsize=(14,10))
plt.plot(T1,U1)
plt.show()

$$$$

• I'm not sure I understand, but could you not just do plt.plot(T1[[0,1,25,50]],U1[[0,1,25,50]])` ? – tmph Nov 10 '20 at 22:05
• Welcome to scicomp! This is essentially a question about how to plot with matplotlib, so it might be better suited for stackoverflow. That being said, you could either only write every nth result to the solution array U1 within the two for loops by adding a condition if (k%25==0) then ... or you could check the documentation of matplotlib if there is built in functionality to plot only every nth datapoint within an array. – MPIchael Nov 11 '20 at 14:24