简体   繁体   中英

Symmetrical log scale array in python

I am trying to solve a 1D non linear Poisson equation (the charge distribution depends on the potential). I have been treating it as a minimization problem and have been using the fsolve function from the scipy.optimize module.

While qualitatively i get a reasonable answer, i had noticed that it varies with the distance between points in the array. It is reasonable as the solution (and its derivatives) are exponential. The solution if most affected near the boundaries of the space over which the problem is defined.

It appears that the amount of time required for 'fsolve' to complete its calculation increases dramatically with the number of points in the array. I have been looking into the option of using nonlinear spacing with the help of the 'logspace' function from numpy. However, this function gives tighter spacing at one side of the array only. I have been trying to generate two arrays using 'logspace' and concatenating them but have not managed to get the required outcome.

To clarify, i require an array in the range [0,x] (x is some float value) where the spacing between array points becomes smaller as they get closer to 0 or x. Any suggestions on how to accomplish this?

The following should give you a log-scale spacing between 0 and 1, so you can scale it to your requirements. I've included two solutions, with and without the boundary values.

import numpy
import math

#set number of spaces: num=?
logrange = numpy.logspace(0,math.log10(11),num=6)

#including boudary points
inclusive = numpy.hstack([logrange -1,21-logrange[-2:0:-1],20])/20
print(inclusive)

#excluding boundary points
exclusive = numpy.hstack([logrange[1:] -1,21-logrange[-2:0:-1]])/20
print(exclusive)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM