0
tau = 0.5 
dt = 0.01
Nt = int(tau/dt)
grid_size = 100
dx = 1/grid_size
x = np.linspace(0, grid_size, grid_size)

t = np.linspace(dt, tau, Nt+1)

Sw_g = [[0 for x1 in range(grid_size)] for y in range(Nt)] 

Sw_g[:Nt][grid_size-1] = 0.2 # Initialization
fw_g = [[1 for x1 in range(grid_size)] for y in range(Nt)] 
fw_g[:Nt][grid_size-1] = 0

When I set the grid size to 10, it runs smoothly but when the grid size is 100, it says list assignment index out of range for initialization of Sw_g. I can't seem to rectify this.

dǝɥɔS ʇoıןןƎ
  • 1,674
  • 5
  • 19
  • 42
  • What do you expect `Sw_g[:Nt][grid_size-1] = 0.2` to do? `Sw_g[:Nt]` creates a new list, and if you don't get an index error with `[grid_size-1]` it sets the `grid_size-1` element of that new list to 0.2. The new list is then thrown away. BTW, the previous line can be written more efficiently as `Sw_g = [[0] * grid_size for y in range(Nt)]`. – PM 2Ring Jun 05 '17 at 13:54
  • I basically need a 50x100 matrix with the last element of each row having a value of 0.2 and to do it without using a loop if possible. – Nitya Mohan Jun 05 '17 at 14:11

0 Answers0