MaverickPelvis
MaverickPelvis

Reputation: 1

How can I loop one parameter of a function

So I have defined a function that evaluates a number of parameters

def chisq(I,energy,Bethe,N,Z,InverseBethe,Range,SThickness,SigmaThickness):

    for i in range(len(energy)):

        Bethe.append( 3.801e-19*(N*Z/energy[i])*(np.log(energy[i])+6.307-np.log(I)));
        InverseBethe.append(1/Bethe[i])

    chisq = 0
    dof = 0

    for i in range(len(energy)):
        Range.append(-1*sci.simps(InverseBethe[0:i+1],energy[0:i+1], even='avg'))
        Diff = SThickness[i]-Range[i]
        Div = Diff/SigmaThickness[i]
        chii = pow(Div,2)
        chisq = chisq + chii    
        dof= dof+1

    redChisq = chisq/dof

    return redChisq;

I want to be able to loop the parameter I over a number of values, ie in an array or otherwise, until I can find the minimum value of the function.

I have used a 'for' loop when calling the function further down, and referred to the I[i], but it doesn't iterate over the array and only selects the first value of the array I have tried using scipy's minimize function, but that also only does one iteration.

How can I iterate over this one parameter whilst keeping the others the same?

Upvotes: 0

Views: 221

Answers (1)

Simon Crane
Simon Crane

Reputation: 2182

You should be able to do something like:

min([f(a,b,c) for a in list_of_a_values])

to get the minimum value of f for a looping through some values and fixed b and c

Upvotes: 1

Related Questions