-1

Assuming that x is the hourly wind speed I am trying to fit using the Newton Raphson Method as shown below. K is a dimensionless parameter that I intend to solve by using the x values. def f(k) is the actual equation, def d_f(k) is the derivative for the equation and def f2(c) solves for a second parameter c. By default the Scipy optimization library requires the following inputs to find a zero using the Newton-Raphson or secant method.

scipy.optimize.newton(func, x0, fprime=None, args=(), tol=1.48e-08, maxiter=50, fprime2=None)

Therefore my question, is what could be wrong with my Newton Raphson iteration, Or How would you go about the Iteration. Is something wrong with my Inputs or what other inputs do I require? My error results can be seen below output generated.

The formulas are: enter image description here

import warnings
import numpy as np
import scipy
import scipy.stats
import sympy
from scipy import stats as st

x = np.random.randint(1, 23, size=3000)
x0 = np.random.choice(x, 100, replace=True)

def f(k):
    return np.sum(x**k*np.log (x))/np.sum(x**k)-1/k- \
              1/len(x)*np.sum(np.log(x))

def d_f(k):
   return len(x)*np.sum(np.log (x)*np.log(np.log-[1-f(k)])- \
              np.sum(np.log(x)*np.sum(np.log(np.log-[1-f(k)]))))

def f2(c):
   return np.exp(k*np.sum(np.log (x))-np.sum(np.log(x)* \
              np.sum(np.log(np.log-[1-f(k)]))))

#Initial Guess 

k = 1.2
scipy.optimize.newton(f(k), x0, fprime=d_f(k))

Generated Output
AttributeError: 'numpy.float64' object has no attribute 'concatenate'
Gwiji
  • 71
  • 8

1 Answers1

1

You should pass the function f and the parameter k to optimize.newton, not one evaluation of that function, as f(k) would return. The x0 that the optimization routines is an initial estimate for its variable, which in this case is your `k'. That is, you probably want:

scipy.optimize.newton(f, k, ...)

That's a bit of a guess, as I'm not sure I really understand your problem, and the error message isn't complete. But you definitely want to pass the function f not the result f(k).

M Newville
  • 5,556
  • 2
  • 11
  • 26