@@ -341,6 +341,8 @@ def _golden_sect_DataFrame(params, lower, upper, func, atol=1e-8):
341341 --------
342342 pvlib.singlediode._pwr_optfcn
343343 """
344+ if np .any (upper - lower < 0. ):
345+ raise ValueError ('upper >= lower is required' )
344346
345347 phim1 = (np .sqrt (5 ) - 1 ) / 2
346348
@@ -349,16 +351,8 @@ def _golden_sect_DataFrame(params, lower, upper, func, atol=1e-8):
349351 df ['VL' ] = lower
350352
351353 converged = False
352- iterations = 0
353354
354- # handle all NaN case gracefully
355- with warnings .catch_warnings ():
356- warnings .filterwarnings (action = 'ignore' ,
357- message = 'All-NaN slice encountered' )
358- iterlimit = 1 + np .nanmax (
359- np .trunc (np .log (atol / (df ['VH' ] - df ['VL' ])) / np .log (phim1 )))
360-
361- while not converged and (iterations <= iterlimit ):
355+ while not converged :
362356
363357 phi = phim1 * (df ['VH' ] - df ['VL' ])
364358 df ['V1' ] = df ['VL' ] + phi
@@ -373,22 +367,16 @@ def _golden_sect_DataFrame(params, lower, upper, func, atol=1e-8):
373367
374368 err = abs (df ['V2' ] - df ['V1' ])
375369
376- # works with single value because err is np.float64
377- converged = (err [~ np .isnan (err )] < atol ).all ()
378- # err will be less than atol before iterations hit the limit
379- # but just to be safe
380- iterations += 1
381-
382- if iterations > iterlimit :
383- raise Exception ("Iterations exceeded maximum. Check that func" ,
384- " is not NaN in (lower, upper)" ) # pragma: no cover
370+ # handle all NaN case gracefully
371+ with warnings .catch_warnings ():
372+ warnings .filterwarnings (action = 'ignore' ,
373+ message = 'All-NaN slice encountered' )
374+ converged = np .all (err [~ np .isnan (err )] < atol )
385375
386- try :
387- func_result = func (df , 'V1' )
388- x = np .where (np .isnan (func_result ), np .nan , df ['V1' ])
389- except KeyError :
390- func_result = np .full_like (upper , np .nan )
391- x = func_result .copy ()
376+ # best estimate of location of maximum
377+ df ['max' ] = 0.5 * (df ['V1' ] + df ['V2' ])
378+ func_result = func (df , 'max' )
379+ x = np .where (np .isnan (func_result ), np .nan , df ['max' ])
392380
393381 return func_result , x
394382
0 commit comments