I am trying to implement the discrete Burger's equation in python and Jupyter notebook and I am trying to test it using a step function but I am facing an issue:
could not broadcast input array from shape (99,) into shape (1,)
which I do not entirely understand why it's doing that or why the shapes are different and I think it might be related to how I am using my step function?
The error occurs inside my discreteBurgers function: f[0] = ....
I am quite new to python and Jupyter notebook and would appreciate any insight into why this might be occurring. I've provides some code below where I think the issues might be and also provided a fully reproducible example burgers_model.py
def step_function(x):
if x <= 0.1:
return 1
else:
return 0
x_axis = np.linspace(0, 1, 400)
y = np.zeros(400)
for i in range(400):
y[i] = step_function(x_axis[i])
plt.plot(xxis, y)
plt.title('Step Function')
plt.xlabel('Spatial coordinate x')
plt.ylabel('Solution u')
plt.grid(True)
plt.show()
def setupInitialData(m):
# set up the initial data for the model
xL = 0
xR = 1
h = (xR - xL) / (m-1)
x = np.linspace(xL, xR, m).reshape((m, 1)) # grid points
v = np.zeros(len(x))
for i in range(len(x)):
v[i] = step_function(x[i]) # initial data
return v
Function with error:
def discreteBurgers(uk, ukp, dt, h, nu, ua, ub):
# ua is boundary condition
# ub is boundary condition
# nu is kinematic viscosity
m = uk.size
# f to store values of the function for each point uk in space
f = np.zeros((m-2, 1))
# boundary conditions
uL = ua
uR = ub
# left boundary
f[0] = (uk[0] - ukp[1])/dt + uk[0] * (uk[0] - uL)/h - nu * (uk[1] - 2*uk[0] + uL)/h**2
# Difference equations each internal node
for i in range(1, m-3):
f[i] = (uk[i] - ukp[i+1])/dt + uk[i] * (uk[i] - uk[i-1])/h - nu * (uk[i+1] - 2*uk[i] + uk[i-1])/h**2
# right boundary
f[m-3] = (uk[m-3] - ukp[m-2])/dt + uk[m-3] * (uk[m-3] - uk[m-4])/h - nu * (uR - 2*uk[m-3] + uk[m-4])/h**2
return f
Instead of initializing the 2D array
f
insidediscreteBurgers
, you should initializef
as a 1D array, like:This helps surpass the mentioned error:
After that, different errors seem to occur when you update
delta
intox
withinnewton_system
.