You are on page 1of 4

Week 4: Fitting Data to Models

Shyam Shankar H R
EE15B127
Electrical Dept.
February 8, 2017

1 Introduction
This report contains the solutions of the week-4 assignment of EE2307. The answers to the questions,
the python code, and the plot obtained are entered below.

1.1 Abstract
Here, we study the process of fitting experimentally obtained data to theoretical models. In any
experiment, there will be a certain degree of error in data, and we also study how to account for the
error (or noise) while fitting the data to the model.

1.2 The python code


from pylab import *
from scipy.special import *
import numpy
def calcnu(x,x0,str,eps,model):
J1 = jv(1,x)
v = zeros(36)
#v stores the value of nu obtained from various subvectors x_i of x
#corresponding to the value of x_0
noise = eps*randn(36) #noise is added to account for experimental errors
j = 0 #counter variable
for k in x0: #Setting up matrix A
xi = [i for i in x if i>=k]
#xi stores numbers from x_0 to 20.
n = len(xi)
#n is the number of samples (rows to be considered)
M = zeros((n,2))
if model == b:
M[:,0] = cos(xi)
M[:,1] = sin(xi)
else:
M[:,0] = cos(xi)/sqrt(xi)
M[:,1] = sin(xi)/sqrt(xi)
#Matrix equation: of the form Ac = b.. where b is the vector of
#Bessel Functions, and c is the vector (A,B)
b = J1[-n:]
c = lstsq(M,b)[0]

1
A = c[0]
B = c[1]
mag = sqrt(A**2+B**2)
phi = numpy.arccos((float)(A)/mag)
v[j] = (phi-pi/4.0)/(pi/2.0)+noise[j]
j+=1
return v
x = linspace(0,20,41)
x0 = arange(0.5,18.5,0.5)
fig1 = figure()
plot(x0,calcnu(x,x0,bo,0,b),bo,label=e=0,model(b)) #noise = 0, model: b
plot(x0,calcnu(x,x0,go,0,c),go,label=e=0,model(c)) #noise = 0, model:c
plot(x0,calcnu(x,x0,ro,0.01,c),ro,label=e=1.0e-02,model(c))
#noise = 0.01, model:c
xlabel(r"$x_{o}$",fontsize=16)
ylabel(r"$v$",fontsize=16)
legend(loc=lower left)
savefig("fig.pdf")
show()

2 Theory:
Bessel function, which is an useful tool in cylindrical geometry, comes in the form:

Figure 1:
Bessel function of first type
0.6

0.4

0.2
Jxa

0.0

0.2

0.4
0 5 10 15 20
x

For large values of x, we can approximate the function as


s
2
Jv (x) = cos(x )
x 2 4
2
Using least squares method of data fitting, we estimate the value of , using either of the two models.

2.1 Model 1:
We construct a matrix corresponding to

Acos(xi ) + Bsin(xi ) J1 (xi )

We get a matrix equation of the form: A c = b, where A is the matrix of functions cos(xi ) and sin(xi ),
b is the matrix to be evaluated (a vector: (A,B)), and c is the vector of Bessel fuctions corresponding
to xi .

2.2 Model 2:
We construct a matrix corresponding to

cos(xi ) sin(xi )
A +B J1 (xi )
xi xi

We get a matrix equation of the form: A c = b, where A is the matrix of functions cos(x ) sin(x )
i and i ,
xi xi
b is the matrix to be evaluated (a vector: (A,B)), and c is the vector of Bessel fuctions corresponding
to xi .

3 Plots of

Figure 2: Value of obtained v/s x0

1.05

1.00

0.95
v

0.90

0.85 e=0,model(b)
e=0,model(c)
e=1.0e-02,model(c)
0.80
0 2 4 6 8 10 12 14 16 18
R
xo

3
Figure 3: After increasing the number of observations
Plots obtained by increasing the sampling rate x 5 times
1.02

1.00

0.98

0.96

0.94
v

0.92

0.90
e=0,model(b)
0.88 e=0,model(c)
e=1.0e-02,model(c)
0.86
0 5 10 15 20
xo

4 Results and Discussions


Theoretically, the value of should be 1. From the plots obtained, we find that the green plot better
fits this description.
That is, the model (c) [or model 2] is a better approximation to the function. When the number of
observations was increased, the plots (expecially, the green one, corresponding to model (c)) approached
the theoretical value of 1, even better.
Now, every experiment will have a certain degree of error associated with the measurements, and we
simulate this experimental error using the noise factor in the models. When we add random numbers
scaled to 0.01 (arbitrarily chosen) to the experimantal data, we obtain the red plot as shown. From
the plots, we observe that noisy measurement gets off the track as x0 increases. At these large values
of x0 , the noiseless data fits the theory better. But for moderate to low values of x0 , the data with
appended noise gives a sense of the practical data obtained as compared to the theoretical model, and
helps us to verify the theoretical model, without having to take too many measurements.

You might also like