Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

1) Use python to fit data with user-defined functions 2) Plot raw data and a best-fit curve on the same figure We will use

1) Use python to fit data with user-defined functions 2) Plot raw data and a best-fit curve on the same figure We will use the scipy module (link) for the first and matplotlib (see lab) for the second. You should be able to install both in terminal using pip; e.g.: pip install scipy If you are struggling, you may consider Anaconda (link) - an all-in-one python install designed for data science. Anaconda includes many common packages, including the ones used here. Fitting Data The curve fitting functionality we are interested in is in scipy's optimize module. We can fit data to a function of our choice as follows: from scipy import optimize params, params_cov optimize.curve_fit(func, xdata, ydata) optimize.curve_fit returns two collections - the parameters that make func best fit our data, and the covariance of those parameters (which can be ignored for this assignment). An example: from scipy import optimize from matplotlib import pyplot as plt # linear fitting function # the first parameter of a fitting funciton must be a # subsequent parameters are automatically adjusted by curve_fit def lin(x, m, b): return mx + b # generate data on y = 1*2 +0, then add some "noise" xdata= [i for i in range (10)] ydata = [i for i in range (10)] y [1] = 2 y [8] = 7 # Find parameters for lin that best fit data and ydata params, params_cov = optimize.curve_fit (lin, xdata, ydata) #unpack the calculated parameters m = params [0] b = params [1] # create an empty list for the line of best fit y_fit = [] for x in xdata: y_fit.append(lin(x, m, b)) # plot the raw data and the line of best fit plt.figure()

Step by Step Solution

There are 3 Steps involved in it

Step: 1

import numpy as np from scipyoptimize import curvefit import matplotlibpyplot as ... blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Fixed Income Securities Valuation Risk and Risk Management

Authors: Pietro Veronesi

1st edition

0470109106, 978-0470109106

More Books

Students also viewed these Programming questions

Question

Identify and explain factors that affect quality of earnings.

Answered: 1 week ago

Question

How would you respond to each of the girls?

Answered: 1 week ago