Reputation: 4406
I was looking at this post since I would like to create an array where each column is one x
vector arange
d by dx
with the corresponding dx
, respectively. Hopefully this makes sense.
import numpy as np
L = 80.0
N = 2 ** np.arange(-4, 10, dtype = np.float64)
dx = L / N
With my original code, I was on looking at one dx
where now I have an array of dx
values.
When I was only using one dx
, I set up my x vector up as follows:
x = np.arange(-L / 2., L / 2. - dx, dx)
However, I need an x
for each dx
but I am not sure on how to do this. I looked at the post I mentioned in the beginning which has provided, I think, some insight. I can't seem to tailor it to my needs though--maybe it isn't even the correct approach.
Maybe I need a for
loop?
for i in len(dx):
x[i] = np.arange(-L / 2., L / 2. - dx, dx)
Then I would probably need to nest another for
loop to pick one dx
for each iteration.
I am not sure what would be the correct approach or most efficient though.
To clarify the confusion, in the one dx
situation, I had the following set up:
x = np.arange(-L / 2.0, L / 2.0 - dx, dx)
k = np.hstack((np.arange(0, N / 2.0 - 1.0),
np.arange(-N / 2.0, 0))).T * 2.0 * np.pi / L
k1 = 1j * k
k3 = (1j * k) ** 3
u = 2 * (2 / (np.exp(x + 20.0) + np.exp(-x - 20.0))) ** 2
udata = u
tdata = 0.0
Integration here
I then ran the pseudo spectral method with Runge Kutta 4 integration to numerical determine u
of the nonlinear KdV equation. I would like to run the code on different dx
values so I can find the error and plot 1/dx
vs the error where 1/dx
is the on the x-axis.
I hope this helps with what I am trying to accomplish.
Since I want to find the error, would I need the same step size? I know the error will plot in the form of exp(-c * dx)
where c
is an arbitrary constant. I know this because the pseudo spectral method has error of exp(-c / dx)
but I will be plotting against 1 / dx
.
Upvotes: 2
Views: 812
Reputation: 46530
I'm not sure how you want to deal with the issue that @Joel brought up, because as it stands, having
x = np.arange(-L / 2., L / 2. - dx, dx)
For different dx
will give different sized arrays, which cannot stack. You could create a list of such arrays by using the for loop you propose:
L = 10
dxs = np.array([1,2,3])
xs = [ np.arange(-L/2, L/2, dx) for dx in dxs ]
Then, xs
is:
[array([-5, -4, -3, -2, -1, 0, 1, 2, 3, 4]),
array([-5, -3, -1, 1, 3]),
array([-5, -2, 1, 4])]
N.B.: I removed the -dx
from the upper limit (L/2 - dx
=> L/2
) because arange
already excludes the last point, which you can see because the result never ends with L/2
which is 5
.
If you want the stepsize to increase while keeping the same boundaries, this is unavoidable.
If you can change the boundaries and want the step size to increase, but keeping the same number of elements, then I'd suggest something like the following, which allows the boundaries to increase.
x = np.arange(-L/2, L/2)
x
#array([-5, -4, -3, -2, -1, 0, 1, 2, 3, 4])
x * dxs[...,None]
#array([[ -5, -4, -3, -2, -1, 0, 1, 2, 3, 4],
# [-10, -8, -6, -4, -2, 0, 2, 4, 6, 8],
# [-15, -12, -9, -6, -3, 0, 3, 6, 9, 12]])
Upvotes: 2