Documente Academic
Documente Profesional
Documente Cultură
1 Introduction
In this assignment we attempt to study the least squares method in greater
detail by tting a model for the bessel functiion. We t three models:
1. f (x) = Acos(x) + B sin(x)
2. f (x) = A cos(x)
x
+ B sin(x)
x
2 Code
Listing 1: Code for least squares tting
import numpy as np
import m a t p l o t l i b . pyplot as p l t
import s c i p y . s p e c i a l as sp
maxlim= 40
numpts= 101
def J ( x , v ) :
return sp . j v ( 1 , x )
x , spac= np . l i n s p a c e ( 0 , maxlim , numpts , endpoint=True , r e t s t e p=True )#uses hard coded
x= x . reshape ( numpts , 1 )
def matfeats1 ( x ) :
return np . hstack ( ( ( np . cos ( x ) ) , np . s i n ( x ) ) )
def l e a s t s q (A, f v a l s ) :
return np . l i n a l g . inv (A.T. dot (A) ) . dot (A.T. dot ( f v a l s ) )
def matfeats2 ( x ) :
return np . hstack ( ( np . cos ( x )/ x 0 . 5 , np . s i n ( x )/ x 0 . 5 ) )
def n u c a l c ( x , x0 , col , eps , model ) :
vec = ( x [ ( x>=x0 ) ] )
vs= vec . shape [ 0 ]
vec= vec . reshape ( vs , 1 )
1
a= l e a s t s q ( model ( vec ) , J ( vec ,1)+ eps np . random . randn ( vec . shape [ 0 ] , 1 ) )
return ( 2 ( np . a r c c o s ( a [ 0 ] / ( a [0] 2+ a [ 1 ] 2 ) 0 . 5 ) np . p i /4)/ np . p i )
3 Observations
On running the code above fro various values of numpts(i.e, the number of
measurements), we see that that the t for models 1 and 2 do not change signif-
2
icantly. However, due to the added noise model 3 shows deviations particularly
for higher values of x0 . This deviation(measured in the code using mean squared
error) becomes less prevalent in the lower values of x0 as the number of measure-
ments increases(see gures). The eect of noise is best seen from the variance
plots(shown for numpts=41, 81, and 300). We then procced to dene a best t
by dening a cost function, which is minimum when it gets an optimal1 sum of
variance and estimated value.
graphs
3
Figure 2: variance for numpts= 81
4
Figure 3: variance for numpts=41
model ts:
5
Figure 4: value for numpts= 41
6
Figure 5: for numpts= 81
7
Figure 6: for numpts=300
8
A higher number of points leads to a sharper increase in variance, further
this rise is seen primarliy in the higher(highest 20%) values of points. The
variance stays almost the same for lower values of x0 .
These points clearly indicate that a large number of equations can mitigate
noisy signals, thus better ts need large sets of equations.
We also see that the lower values of x0 do not accurate produce values of .
This is because the bessel function approximates to our selected model only for
higher values of x.