# Minimising chi square to fit two parameters

#### S_1000

I am getting rather confused on the process of optimising the values of Two parameters I have given their estimated values.

import scipy.optimize as optimize

# Define the function you want to minimize
def my_function(x, y):
return x**2 + y**2

# Use the fmin function to find the minimum of the function
result = optimize.fmin(my_function, x0=[0, 0])

# The result will be an array containing the values of the parameters that minimize the function
print(result)

I understand this example but for my case I want to minimise a chi Squared function chi_square = np.sum(((y - f1)/yerr)**2) where f1 is a function, dependent on two parameters, with inital guess values, that need to be optimised.

The input values of the function, f, which calculates the model are : two parameters C1, C2 and then time which is input as an array of data values.

C1 = 0.002 C2 = 0.003 f(C1,C2,t) f = some function including C1,C2 constants and time input t return f

time_input = data[:,0]

f1= f(C1,C2, time_input) to get model values in order to compare to y in the chi square for the same time data points[:,0].

I want to use optimize.fmin to essentially find values of C1 and C2 for a minimised chi_square but I am unsure of how to do this as in the optimize example given, it is for a function with parameters x and y, whereas my function is a chi square function where 'model' is the function that needs its input parameters C1,C2, to be optimised.

Said the number of positional arguments was incorrect, when I put the guess values of 0.002 and 0.003 in X0.

#### Josmellon

To use the optimize.fmin function to minimize the chi-squared function in your case, you need to define a new function that takes two arguments, C1 and C2, and returns the chi-squared value. This function will be passed to optimize.fmin as the function to minimize.

Here's an example of how you can define and call optimize.fmin in your case:

Code:
``````import numpy as np
from scipy import optimize

# Define the function to minimize
def chi_square(C1, C2):
# Calculate the model using the input values of C1 and C2
model = f(C1, C2, time_input)

# Calculate the chi-squared value
chi_square = np.sum(((y - model)/yerr)**2)

# Return the chi-squared value
return chi_square

# Set the initial guess values for C1 and C2
x0 = [0.002, 0.003]

# Use the fmin function to find the minimum of the chi-square function
result = optimize.fmin(chi_square, x0=x0)

# The result will be an array containing the values of C1 and C2 that minimize the chi-square function
print(result)``````

I hope this helps! Let me know if you have any questions.