Experiment 1 Data Fitting, Method of Least Squares ( Linear Regression )
Experiment No.1 - Linear Data Fitting using Method of Least Squares Using scilab
CONCEPT -
(a) (Linear Regression)
The theoretical model proposes a linear relationship between the dependent y and the independent
x variable-
y = a0 + a1x
where a0 is the intercept and a1 is the slope.
(b) (Goodness of Fit)
Goodness of Fit (g) is indicated by the value of the sum of squares of the deviation (νi) of the dependent
variable i.e., difference between the experimental value yi and the expected value y(xi)
g =X νi^2 where X=sigma
= X [y(xi) − yi]^2
and the standard deviation σ
σ =sqrt(g/(n − 2))
METHOD -
(a) (Linear Regression for Given Experimental Data)
Given the data set (xi, yi) for i = 1, ..., n; the slope and intercept of the least square line is given
by the formula
a1 = N1/D & a0 =N0/D
where N1 = n{X (xiyi) −X (xi)*X (yi)} , N0 = X (yi)* X (xi^2) − X (xi)* X (xiyi)
and D = n{X (xi)^2 −X(xi) *X (xi)}
(b) (1 point) (Standard Error)
The standard deviation in the value of slope annd intercept would be given by
σa1 =n*σ^2/D
& σa0 ={σ^2 *X(xi)^2}/D
APPLICATIONS
(a) (Spring Constant - Static)
The extension l − l0 and the mass m bears the relation k (l − l0) = m × g.
Estimate the value of k and error ∆k, from an experiment that produced the following data-
Mass (g) 0.0 100.0 200.0 300.0 400.0 500.0 600.0 700.0 800.0 900.0
Extension (cm) 0.0 0.2 0.4 0.7 0.9 1.1 1.3 1.6 1.8 2.0
(b) (Ohm’s Law)
The current I and the voltage V bears the relation IR = V .
Estimate the value of R and error ∆R, from an experiment that produced the following data for
voltage vs current
Voltage (V ) 0.0 1.0 2.0 3.0 4.0 5.0 6.0 7.0 8.0 9.0
Current (mA) 0.0 0.8 1.5 2.1 2.6 3.0 3.3 3.6 3.9 4.0
(c) (2 points) (Thermal Expansion)
The length l and the temperature change T − T0 bears the relation l = l0 + α (T − T0).
Estimate the value of α and error ∆α, from an experiment that produced the following data-
Temperature (C) 0.0 10.0 20.0 30.0 40.0 50.0 60.0 70.0 80.0 90.0
Length (mm) 0.0 0.2 0.4 0.7 0.9 1.1 1.3 1.6 1.8 2.0
Scilab Program For the Given Problem -
#scilabcode
//The extension l − l0 and the mass m bears the relation k (l − l0) = m × g.
//Estimate the value of k and error ∆k, from an experiment that produced
the following data
clc
clf
n=10; //numbers of points
x=0:100:900;
y=[0.0,0.2,0.4,0.7,0.9,1.1,1.3,1.6,1.8,2.0]
a1=(n*sum(x.*y)-sum(x)*sum(y)) //formula refer to theory
a0=(sum(y)*sum(x.^2)-sum(x)*sum(x.*y))
d=(n*sum(x.^2)-(sum(x))^2)
n1=a1/d;
n0=a0/d;
disp(x) //to display the x values
disp(y)
disp("the slope of the least square line ")
disp(n1)
disp("of the least square line")
disp(n0)
y1=n1*x+n0; //predicted values of y
disp("the value of y1: ")
disp(y1)
//deviation in y
v1=y1-y
disp("deviation in y : ")
disp(v1);
g=sum(v1.*v1);//Goodness of Fit
sd=sqrt(g/(n-2));
disp("the standard deviation σ ")
disp(sd);//standard deviation
sd1=sqrt(n*sd^2/d);//The standard deviation in the value of slope
sd0=sqrt(sum(x.^2)*sd^2/d)//The standard deviation in the value of
intercept
yp=(n1+sd1)*x+((n0+sd0))
yn=(n1-sd1)*x+((n0-sd0))
k=980/n1; //spring constant and value of g in cgs
deltak=980*sd1/a1; //error in k
disp(string(k)+"+-"+string(deltak)+" Dyne/cm","spring constant =", sd1
,"standard Deviation in slope= ",sd0, "standard deviation in intercept= ")
plot(x,y,".",x,y1,x,yp,x,yn);
legend("Observed","calculated","y+","y-")
xtitle("spring constant","mass(gm)","extension(cm)")
//spring constant = 434677.42+-0.0000017 Dyne/cm
Please make some adjustment according to Problem.
PLOT
2
//(Ohm’s Law)
//The current I and the voltage V bears the relation IR = V .
//Estimate the value of R and error ∆R, from an experiment that produced
the following data for
//voltage vs current
clc
clf
n=10;
x=0:1:9;
y=[0.0,0.8,1.5,2.1,2.6,3.0,3.3,3.6,3.9,4.0]
a1=(n*sum(x.*y)-sum(x)*sum(y))
a0=(sum(y)*sum(x.^2)-sum(x)*sum(x.*y))
d=(n*sum(x.^2)-(sum(x))^2)
n1=a1/d;
n0=a0/d;
disp(x)
disp(y)
disp("the slope of the least square line ")
disp(n1)
disp("of the least square line")
disp(n0)
y1=n1*x+n0;
disp("the value of y1: ")
disp(y1)
v1=y1-y ////deviation in y
disp("deviation in y : ")
disp(v1);
g=sum(v1.*v1)////Goodness of Fit
sd=sqrt(g/(n-2));
disp("the standard deviation σ ")
disp(sd);//standard deviation
sd1=sqrt(n*sd^2/d);//The standard deviation in the value of slope
sd0=sqrt(sum(x.^2)*sd^2/d)//The standard deviation in the value of
intercept
yp=(n1+sd1)*x+((n0+sd0))
yn=(n1-sd1)*x+((n0-sd0))
R=1/n1
delta_R=sd1/n1
disp(string(R)+"+-"+string(delta_R)+" Ohm","Resistance is= ", sd1 ,"standard
Deviation in slope= ",sd0, "standard deviation in intercept= ")
plot(x,y,".",x,y1,x,yp,x,yn);
legend("Observed","calculated","y+","y-")
xtitle("resistance","voltage","current(mA)")
//Resistance is = 2.2853186+-0.0805292 Ohm
3
//(Thermal Expansion)
//The length l and the temperature change T − T0 bears the relation l = l0 +
α (T − T0)
//Estimate the value of α and error ∆α, from an experiment that produced
the following data
clc
n=10;
x=0:10:90;
y=[0.0,0.2,0.4,0.7,0.9,1.1,1.3,1.6,1.8,2.0]
a1=(n*sum(x.*y)-sum(x)*sum(y))
a0=(sum(y)*sum(x.^2)-sum(x)*sum(x.*y))
d=(n*sum(x.^2)-(sum(x))^2)
n1=a1/d;
n0=a0/d;
disp(x)
disp(y)
disp("the slope of the least square line ")
disp(n1)
disp("of the least square line")
disp(n0)
y1=n1*x+n0;
disp("the value of y1: ")
disp(y1)
v1=y1-y ////deviation in y
disp("deviation in y : ")
disp(v1);
g=sum(v1.*v1);//goodness of Fit
sd=sqrt(g/(n-2));
disp("the standard deviation σ ")
disp(sd);//standard deviation
sd1=sqrt(n*sd^2/d);//The standard deviation in the value of slope
sd0=sqrt(sum(x.^2)*sd^2/d)//The standard deviation in the value of
intercept
yp=(n1+sd1)*x+((n0+sd0))
yn=(n1-sd1)*x+((n0-sd0))
alpha=n1
delta_a=sd1
disp(string(alpha)+"+-"+string(delta_a)+" mm/degre ecelcius","te", sd1
,"standard Deviation in slope= ",sd0, "standard deviation in intercept= ")
plot(x,y,".",x,y1,x,yp,x,yn);
legend("Observed","calculated","y+","y-")
xtitle("temp.coff.","temp (c)","length (mm)")
//Thermal Expension = 0.0225455+-0.0003149 mm/degre ecelcius
// University of Delhi /Department of Physics
// Specially Created for Bsc Physics 2nd Year Students Of Physics Hons
Note - If you have any Problem Regarding this Kindly Comment or Reach us out at scilabprogram@gmail.com
Can u also give the same in for python
ReplyDelete