Experiment 2:- Data Fitting Method of Least Squares ( Quadratic Regression )
Scilab Program For Quadratic Regression
CONCEPT :-
(a) (Quadratic Regression) The theoretical model proposes a quadratic relationship the dependent y and the independent x variable
y(x) = a0 + a1x + a2x 2
where a0, a1 and a2 are constants
(b) (Goodness of Fit)
Goodness of Fit g of the above line to a given data set of n number of data points and the standard deviation σ are given by
g = X(vi)^2 = X[y(xi) − yi ]^ 2 & where X is Sigma
σ = sqrt{ g/ (n − m − 1) }
METHOD:-
(a) (Parabolic Fit for Given Experimental Data)
Given the data set (xi , yi) for i = 1, ..., N; the parameters a0, a1 & a2 of the least square parabola is given by the matrix formula.
(b) (Error in Fitting Parameters)
The standard deviation in the value of the fitting parameters would be given by
σ^2= a0 /M11 = σ^ 2= a1/ M22 = σ ^2= a2/ M33 = σ ^2 /∆
where ∆ is the determinant of A and Mii are the minor of elements aii of matrix A for all i = 1, 2 & 3. Remember that a“minor” Mij is the determinant of the square matrix formed by deleting ith row and jth column from some larger square matrix A.
APPLICATIONS:-
(a) (3 points) (Constant Acceleration) The displacement x and the time t bears the relation x = x0 + v0t + (1/2)at2 . Estimate the value of initial speed v0 and acceleration a from an experiment that produced the following data
time (s) 0.0 0.1 0.3 0.5 0.7 0.9 1.1 1.3 1.5
displacement (m) 2.567 2.649 2.903 3.215 3.631 4.112 4.762 5.300 6.013
SCILAB PROGRAM FOR GIVEN PROBLEM :-
//DATA FITTING
clc
clf
//quadretic regressiOn
x=[0.0,0.1,0.3,0.5,0.7,0.9,1.1,1.3,1.5]//time
y=[2.567,2.649,2.903,3.215,3.631,4.112,4.762,5.300,6.013]//displacement
n=length(x)
m=2
x1=0,x2=0,x3=0,x4=0,y1=0,xy=0,x2y=0
for i=1:n
x1=x1+x(i)
x2=x2+x(i)^2
x3=x3+x(i)^3
x4=x4+x(i)^4
y1=y1+y(i)
xy=xy+x(i)*y(i)
x2y=x2y+(x(i)^2)*y(i)
end
disp(x1,x2,x3,x4,y1,xy,x2y)
A=[n,x1,x2;x1,x2,x3;x2,x3,x4]//matrixA
C=[y1;xy;x2y]//matrixC
B=inv(A)*C
disp(B,"values of Required constants x0,v0,1/2a")
ypre=B(1)+B(2)*x+B(3)*x^2;//predicted values of y
disp(ypre,"Predicted values of ypre")
g=0
for i=1:n
g=g+(ypre(i)-y(i)).^2;
end
disp(g,"the value of g")
sigma=sqrt(g/(n-m-1))
disp(sigma,"The value of Sigma is")
X=[x2,x3;x3,x4]
Y=[n,x2;x2,x4]
Z=[n,x1;x1,x2]
M11=det(X)
M22=det(Y)
M33=det(Z)
W=det(A)
disp(W,"determinent of a")
siga0=sigma*sqrt(M11/W)
siga1=sigma*sqrt(M22/W)
siga2=sigma*sqrt(M33/W)
disp(siga0,"The value of sigma A0")
disp(siga1,"The value of sigma A1")
disp(siga2,"The value of sigma A2")
disp(B(2),"the value of initial speed in m/s")
acc=2*B(3)
disp(acc,"the value of acceleration in m/s^2")
yp=(B(1)+siga0)+(B(2)+siga1)*x+(B(3)+siga2)*x.^2
yn=(B(1)-siga0)+(B(2)-siga1)*x+(B(3)-siga2)*x.^2
//plot(x,y,".",x,ypre,".",x,yp,".",x,yn,)
plot2d(x,y,-2)
plot2d(x,ypre,2)
plot2d(x,yp,3)
plot2d(x,yn,5)
xlabel("time (seconds")
ylabel("displacement (m)")
xtitle("linear motion under constant acc-")
legend("actual","calculated","ypositive","ynegative")
mprintf("\n SNO\t Xi \t Yi \t Xi.^2 \t Xi.Yi \t Xi^2.Yi \t Xi^3 \t Xi.^4\n");
for i=1:n;
mprintf("%d \t %.2f \t %.2f \t %.2f \t %.2f\t %.2f\t\t %.2f \t %.2f\n",i,x(i),y(i),x(i).*x(i),x(i).*y(i),x(i).*x(i).*y1(i),x(i).^3,x(i).^4);
end
mprintf("sum")
mprintf("\n")
mprintf("%d \t %.2f \t %.2f \t %.2f \t %.2f\t %.2f\t\t %.2f \t %.2f\n",1,x1,y1,x2,xy,x2y,x3,x4);
disp(x1,x2,x3,x4,y1,xy,x2y)
//the value of initial speed in m/s
// 0.899292
//the value of acceleration in m/s^2
// 1.8916731
Comments
Post a Comment