<SYSTEM_TASK:>
Given the following text description, write Python code to implement the functionality described below step by step
<END_TASK>
<USER_TASK:>
Description:
 Fitting Models Exercise 1
Imports
Step1:  Fitting a quadratic curve
For this problem we are going to work with the following model
Step2:  First, generate a dataset using this model using these parameters and the following characteristics
Step3:  Now fit the model to the dataset to recover estimates for the model's parameters
<ASSISTANT_TASK:>
Python Code:
%matplotlib inline
import matplotlib.pyplot as plt
import numpy as np
import scipy.optimize as opt
Explanation: Fitting Models Exercise 1
Imports
End of explanation
a_true = 0.5
b_true = 2.0
c_true = -4.0
Explanation: Fitting a quadratic curve
For this problem we are going to work with the following model:
$$ y_{model}(x) = a x^2 + b x + c $$
The true values of the model parameters are as follows:
End of explanation
# YOUR CODE HERE
x = np.linspace(-5, 5, 30)
y = a_true*(x**2) + b_true*(x) + [c_true]*30 + 2*np.random.randn(30)
plt.scatter(x, y)
assert True # leave this cell for grading the raw data generation and plot
Explanation: First, generate a dataset using this model using these parameters and the following characteristics:
For your $x$ data use 30 uniformly spaced points between $[-5,5]$.
Add a noise term to the $y$ value at each point that is drawn from a normal distribution with zero mean and standard deviation 2.0. Make sure you add a different random number to each point (see the size argument of np.random.normal).
After you generate the data, make a plot of the raw data (use points).
End of explanation
# YOUR CODE HERE
def model(x, a, b, c):
    return a*x**2 + b*x + c
theta_best, theta_cov = opt.curve_fit(model, x, y, sigma=2)
print("a = ", theta_best[0], " +- ", theta_cov[0,0])
print("b = ", theta_best[1], " +- ", theta_cov[1,1])
print("c = ", theta_best[2], " +- ", theta_cov[2,2])
fitline = theta_best[0]*x**2 + theta_best[1]*x + theta_best[2]
plt.plot(x, fitline, color="r")
plt.scatter(x, y)
plt.xlabel("x")
plt.ylabel("y")
assert True # leave this cell for grading the fit; should include a plot and printout of the parameters+errors
Explanation: Now fit the model to the dataset to recover estimates for the model's parameters:
Print out the estimates and uncertainties of each parameter.
Plot the raw data and best fit of the model.
End of explanation
<END_TASK>