[Solved] PHY224 Lab2-Introduction to fitting methods

$25

File Name: PHY224_Lab2_Introduction_to_fitting_methods.zip
File Size: 405.06 KB

SKU: [Solved] PHY224 Lab2-Introduction to fitting methods Category: Tag:
5/5 - (1 vote)

Introduction to fitting methods

Ohms law

The function curve_fit() is introduced for fitting linear data. You will perform voltage and current measurements on a resistor to find the resistance via Ohms law.

This lab contains explicit questions for you to answer labelled with Q. However, you still need to maintain a notebook for the lab, make plots and write Python code.

Background knowledge for exercise 2

Python arrays, numpy, pyplot, scipy, curve_fit()

Error Analysis Chi squared (2), goodness of fit Physics Ohms law. Review this from your first year text.

1 Introduction

In this exercise, we will introduce how to fit experimental data to a certain function, and get quantitative observations from experiment.

The package Notes on Error Analysis can be found at http://www.physics.utoronto.ca/~phy225h/ web-pages/Notes_Error.pdf. It introduces linear and non-linear regression methods, aimed at finding the values of parameters which give the closest agreement between the data and the theoretical model. You should read the section on least-squares fitting.

Suppose our data consisted of N measurements (yi,xi) with i = 1,2,,N. A first step is to plot the data with error bars representing the reading error (or the standard deviation i) on each point. This gives a visual check for what relation our data has.

Next, assume we know a fitting function which is close to the experimental data. The function depends on unknown parameters, p1,p2,. We shall calculate the parameters pi so that the function y(xi) would be in closest agreement with (yi,xi). Note that y is probably not the true value of the relation, but rather the best value we can determine from the data.

The least-squares method is a maximum likelihood estimation using the minimization of chi-squared,

. (1)

The function y(xi) that minimizes 2 is the curve of best fit.

In this exercise, we whall look at a linear fitting method, which uses the fitting function y(x) = ax + b, where a and b are model parameters to be determined.

We shall not attempt a complete analysis of the goodness of the fit. This will be done in exercise 3.

2 Curve fitting in Python

The scipy package in Python contains functions for performing a least-squares curve fit. We will use the curve_fit() function from the scipy.optimize module.

The inputs/arguments to curve_fit() are: the model function, x data, y data, and an initial guess at the model parameters. The reading errors i can also be included. In linear fitting, the model function is f(x;a,b) = ax + b, which can be defined in Python as,

def model_function(x, a, b):return a*x + b

Note that the dependent variable x must be the first argument.

The return values are the optimal parameter values, and the covariance matrix for those parameters. Given xdata and ydata, the full call for a linear fit to find the parameters would be,

def model_function(x, a, b):return a*x + bp_opt, p_cov = curve_fit(model_function, xdata, ydata, (1, 0), sigmadata, True)

The slope of the line of best fit is p_opt[0]. Passing the value True instructs curve_fit() to use the absolute magnitudes of sigma to calculate covariance; the value p_opt is unaffected by it.

There are many keyword arguments to curve_fit(), which can be found in the documentation. Commonly, you may use maxfev to control the maximum number of function calls. The default value is 200.

3 The experiment

We will be testing Ohms Law the linear relation between voltage and current in electrical circuits. The voltage is our independent variable, current is the dependent variable, and resistance is the parameter wed like to measure. There are two parts to the experiment: testing a known resistor, and testing a potentiometer.

3.1 Uncertainty from multimeters

Our later analysis requires an idea of the uncertainty in the measurements. Digital multimeters have two types of uncertainty:

Error of accuracy which depends on function (DCV, ACV, DCA, ACA and Ohm ()). This error is provided by the instrument manufacturer. This error is expressed as a percentage of the reading. Please check the link below for complete instrument specifications. The reading can be done on different scales of the same function, which changes the number of digits on display and also the error of accuracy.

Error of precision which is due to fluctuations at the level of the last digit. Take one digit (with position on the last digit and value 1) to be the error of precision.

The uncertainty is taken to be the largest of the two errors above.

For example, using a multimeter with an error of accuracy of 3%, you read 2.06 mA of the instrument.

Error of accuracy is 3% of reading:

2.06mA 0.03 = 0.06mA

Error of precision is 0.01 (fluctuations in the last digit). Therefore, by choosing the largest of the two errors, we state that the uncertainty in reading 2.06mA is 0.06mA. The instrument specifications for the multimeter youll use can be found at http://www.upscale.utoronto.ca/specs/tegam130a.html

Be sure to track the uncertainty in measurements from the multimeter. Also, keep in mind that there are other sources of uncertainty that are harder to measure, like the quality of electrical connections.

3.2 The experiment

You will be provided with two multimeters, a board with electrical components (see Figure 1), and enough wires to connect everything. Choose one resistor, and one potentiometer for the experiment.

Figure 1: The electrical components board.

For the known resistor, perform the following.

  1. Connect the ameter, voltmeter, and power supply to the resistor (Figure 2). Ask the TA if youre in doubt
  2. Vary the voltage on the power supply.
  3. Record the voltage and current from the multimeters, along with uncertainty.
  4. Change the voltage, and record the new values repeat.
  5. Save the data on a memory stick as a text file (extension .txt) with the data in two columns: the first column being the independent variable (voltage).
  6. After performing all the above measurements, disconnect the power, and switch the voltmeter to resistance. This will give you a reference value to compare against.

Repeat these measurements for the potentiometer, making sure not to move the dial between measurements.

Resistors are marked with coloured bars to indicate their nominal resistance through a code (there are many calculators for this online). As part of this exercise, you may also compare your results against the nominal resistance the tolerance.

4 Analyzing the data

We will analyze the dependency of the current on the voltage using a linear fitting program. Ohms law for resistors sates

(2)

Thus, the resistance of a linear component can be determined by plotting I vs. V , and measuring the slope.

There are other electrical components that have nonlinear voltage-current relations, like diodes, transistors, and amplifiers. Using a linear model for these would result in a bad fit. In a later exercise, we will perform a nonlinear regression with a lightbulb.

5 Build a linear fitting program

Linear fitting is done via the Levenberg-Marquedt algorithm (an advanced least-squares method), as implemented in the scipy.optimize module. Today, you will use the curve_fit() function (i.e. scipy.optimize.curve_fit). Calculation of the statistics from the ouput of curve_fit() will be done in the next exercise.

Here is the outline of the Python program for Exercise 2a:

  • import the necessary modules and functions (e.g. numpy and curve_fit())
  • Read the data file into an array with the loadtxt()
  • Extract the variables from the array columns.
  • Define the model function f(x) = ax + b.
  • Call curve_fit() with the function, data, and initial guess for the parameters.
  • Output the calculated parameters.
  • Create relevant plots.

Write a program to perform a linear fit for the data you collected in Section 3. It should create a plot of current vs. voltage with errorbars and the line of best fit, and output the calculated value of resistance (the slope of the line of best fit). Run this program using the data for the resistor, and again using the data for the potentiometer.

Q1 In Ohms law, V = IR, the line should pass through I = V = 0. Did your linear fit pass through zero as well? If it didnt, why do you think that happened?

Q2 What result do you find for resistance if you force the line to pass through zero? (i.e. try the model function f(x,a) = ax)

Q3 How does your resistance from using curve_fit() compare to the value measured with the multimeter?

6 Goodness of fit reduced chi-squared

Recall that the 2 distribution gives a measure of how unlikely a measurement is. The more a model deviates from the measurements, the higher the value of 2. But if 2 is too small, its also an indication of a problem: usually that there werent enough samples.

This best (most likely) value of 2 depends on the number of degrees of freedom of the data, = N n, where N is the number of observations and n is the number of parameters. This dependence is removed with the reduced chi-squared distribution,

, (3)

where yi is the dependent data you measured, xi is the independent data, i is the measurement error in the dependent data.

For 2red, the best value is 1:

indicates that the model is a poor fit;

indicates an incomplete fit or underestimated error variance; indicates an over-fit model (not enough data, or the model somehow is fitting the noise).

Q4 Add a function to your program to calculate . What values were computed? How would you interpret your results?

Reviews

There are no reviews yet.

Only logged in customers who have purchased this product may leave a review.

Shopping Cart
[Solved] PHY224 Lab2-Introduction to fitting methods
$25