Rosenbrock function and Curve fiting#
The Rosenbrock function#
The Rosenbrock function is a classical benchmark for optimization algorithms. It is defined by the following equation:
%matplotlib widget
import numpy as np
import matplotlib.pyplot as plt
def Rosen(X):
"""
Rosenbrock function
"""
x, y = X
return (1 - x) ** 2 + 100.0 * (y - x**2) ** 2
Questions#
Find the minimum of the function using brute force. Comment the accuracy and number of function evaluations.
Same question with the simplex (Nelder-Mead) algorithm.
Curve fitting#
The data to be fitted are available in the file data.csv. The first column contains the \(x\) values, and the second column contains the corresponding \(y\) values.
The goal is to find the parameters of a mathematical function that best fits the data. We will use the curve_fit function from the scipy.optimize module to perform the curve fitting.
Questions#
Build a mathematical function \(y = f(x, ...)\) and code it. This function should be able to represent the trend of the data.
Identify the parameters of the function that best fit the data. You can use the
curve_fitfunction from thescipy.optimizemodule to perform the curve fitting.Evaluate the quality of the fit by calculating the residuals and plotting the fitted curve against the original data points.
# load data from csv file
data = np.loadtxt("data.csv", delimiter=",", skiprows=1)
# plot data
plt.figure(1)
plt.clf()
plt.scatter(data[:, 0], data[:, 1], c="red", label="data")
plt.xlabel("x")
plt.ylabel("y")
plt.legend()
plt.grid()
plt.show()