40 lines
914 B
Python
40 lines
914 B
Python
|
import numpy as np
|
||
|
from scipy.optimize import minimize
|
||
|
|
||
|
|
||
|
# example model
|
||
|
def model(params, x):
|
||
|
return params[0] * x + params[1]
|
||
|
|
||
|
|
||
|
# Cost function
|
||
|
def cost_function(params, x_data, y_data):
|
||
|
return np.sum((model(params, x_data) - y_data) ** 2)
|
||
|
|
||
|
|
||
|
# Global cost function
|
||
|
def global_cost_function(params, experiments):
|
||
|
total_cost = 0
|
||
|
for x_data, y_data in experiments:
|
||
|
total_cost += cost_function(params, x_data, y_data)
|
||
|
return total_cost
|
||
|
|
||
|
|
||
|
# Example data for two experiments
|
||
|
x_data_1 = np.array([0, 1, 2, 3])
|
||
|
y_data_1 = np.array([1, 3, 5, 7])
|
||
|
|
||
|
x_data_2 = np.array([0, 1, 2, 3])
|
||
|
y_data_2 = np.array([2, 4, 6, 8])
|
||
|
|
||
|
experiments = [(x_data_1, y_data_1), (x_data_2, y_data_2)]
|
||
|
|
||
|
# Initial guess/parameters
|
||
|
initial_params = [1, 0]
|
||
|
|
||
|
# Run the optimization
|
||
|
result = minimize(global_cost_function, initial_params, args=(experiments,))
|
||
|
|
||
|
# Optimized parameters
|
||
|
print("Optimized parameters:", result.x)
|