Download as pdf or txt
Download as pdf or txt
You are on page 1of 6

Table of Contents

........................................................................................................................................ 1
Linear LSA ........................................................................................................................ 1
General LSA ...................................................................................................................... 1
Global Test (alpha = 0.05) .................................................................................................... 3

clc;
clear all;

Linear LSA
xyz = dlmread('data1.txt','',0,1);
x = xyz(:,1);
y = xyz(:,2);
z = xyz(:,3);

for i = 1:16
B(i,1:4) = [-1 -x(i) -y(i) -x(i)*y(i)];
f(i,1) = -z(i);
end
W = eye(16);

Delta = (inv(B'*W*B))*(B'*W*f);
residuals = f - (B*Delta);
fprintf('\n\nInitial Parameter Approximations:\na0 = %f; a1 = %f; a2 =
%f; a3 = %f\n',Delta);

General LSA
n = 48; n0 = 36; r = 12; u = 4; c = 16;
fprintf('GLS Counting:\nn = %d; n0 = %d; r = %d; u = %d; c = %d
\n',n,n0,r,u,c);

A = zeros(c,n);
B = zeros(c,u);

% Original observations
l = [];
for i = 1:c
l = [l; x(i); y(i); z(i)];
end

% Initial approximations
l0 = l;
x0 = Delta;
sigma0 = 0.1;
W = eye(n);
niter = 0;

1
while (abs(Delta) >= 1e-8)
a0 = x0(1); a1 = x0(2); a2 = x0(3); a3 = x0(4);
x = l0(1:3:3*c-2);
y = l0(2:3:3*c-1);
z = l0(3:3:3*c);

% Create A, B anf f matrices


for i = 1:c
F(i,1) = z(i) - (a0 + a1*x(i) + a2*y(i) +a3*x(i)*y(i));
dF_dx = -(a1 + a3*y(i));
dF_dy = -(a2 + a3*x(i));
dF_dz = 1;
dF_da0 = -1;
dF_da1 = -x(i);
dF_da2 = -y(i);
dF_da3 = -x(i)*y(i);

A(i,3*i-2:3*i) = [dF_dx dF_dy dF_dz];


B(i,:) = [dF_da0 dF_da1 dF_da2 dF_da3];
end
f = -F-A*(l-l0);

% GLS Adjustment
Q = inv(W);
Qe = A*Q*A';
We = inv(Qe);
Delta = inv(B'*We*B)*B'*We*f;
K = We*(f-B*Delta);
v = Q*A'*K;

if (niter == 0)
fprintf('\n\n"A" Matrix (Block Diagonal Elements of each row)
for 1st iteration:\n');
for row = 1:c
fprintf(' %9.4f %9.4f %9.4f
\n',A(row,3*row-2:3*row));
end
fprintf('\n\n"B" Matrix for 1st iteration:\n');
fprintf(' %d %9.4f %9.4f %9.4f\n',B');
end

% Updation of observations and parameters


x0 = x0+Delta;
l0 = l+v;
niter = niter+1;
fprintf('\n\n\nIteration No. %d\n', niter);
fprintf('Delta: \n');
fprintf('%10.9f\n',Delta);
end

fprintf('\n\nTotal GLS iterations = %d\n',niter);


fprintf('Final parameter values:\na0 = %f; a1 = %f; a2 = %f; a3 = %f
\n',x0);

2
fprintf('\n\nFinal residuals:\n-----|v_x|--------|v_y|--------|
v_z|-----\n');
fprintf(' %9.6f %9.6f %9.6f\n',v);
fprintf('\n\n\n\n\nAdjusted observations:\n-----|x|----------|
y|----------|z|-----\n');
fprintf(' %9.6f %9.6f %9.6f\n',l0);

Global Test (alpha = 0.05)


Find test statistic

ts = v'*W*v/(sigma0^2);

% Compute critical values


alpha = 0.05;
cv1 = icdf('chi2',alpha/2,r);
cv2 = icdf('chi2',1-(alpha/2),r);

% Check if hypothesis is accepted or rejected


if(ts>cv1 && ts<cv2)
fprintf('\n\nHypothesis is accepted!!\n');
fprintf('\n\n----|cv1|-------|ts|-------|cv2|----\n');
fprintf(' %7.4f %7.4f %7.4f\n',cv1,ts,cv2);
variance = sigma0;
fprintf('\n\nVariance (prior) = %f\n',variance);
else
fprintf('Hypothesis is rejected!!');
fprintf('\n\nVariance (a-posteriori) = %f\n',variance);
variance = (v'*W*v)/r;
end

Initial Parameter Approximations:


a0 = 5.271546; a1 = 0.148669; a2 = 0.226399; a3 = 0.024593
GLS Counting:
n = 48; n0 = 36; r = 12; u = 4; c = 16

"A" Matrix (Block Diagonal Elements of each row) for 1st iteration:
-0.1979 -0.2776 1.0000
-0.2023 -0.3002 1.0000
-0.2030 -0.3226 1.0000
-0.1939 -0.3506 1.0000
-0.2161 -0.2724 1.0000
-0.2195 -0.2972 1.0000
-0.2239 -0.3235 1.0000
-0.2239 -0.3496 1.0000
-0.2434 -0.2734 1.0000
-0.2488 -0.2970 1.0000
-0.2434 -0.3255 1.0000
-0.2522 -0.3494 1.0000
-0.2714 -0.2803 1.0000
-0.2702 -0.3046 1.0000

3
-0.2724 -0.3282 1.0000
-0.2699 -0.3501 1.0000

"B" Matrix for 1st iteration:


-1 -2.0800 -2.0000 -4.1600
-1 -3.0000 -2.1800 -6.5400
-1 -3.9100 -2.2100 -8.6411
-1 -5.0500 -1.8400 -9.2920
-1 -1.8700 -2.7400 -5.1238
-1 -2.8800 -2.8800 -8.2944
-1 -3.9500 -3.0600 -12.0870
-1 -5.0100 -3.0600 -15.3306
-1 -1.9100 -3.8500 -7.3535
-1 -2.8700 -4.0700 -11.6809
-1 -4.0300 -3.8500 -15.5155
-1 -5.0000 -4.2100 -21.0500
-1 -2.1900 -4.9900 -10.9281
-1 -3.1800 -4.9400 -15.7092
-1 -4.1400 -5.0300 -20.8242
-1 -5.0300 -4.9300 -24.7979

Iteration No. 1
Delta:
0.000910487
-0.000315134
0.000125921
-0.000021365

Iteration No. 2
Delta:
0.003263626
-0.002058383
-0.001770360
0.000822017

Iteration No. 3
Delta:
-0.000023684
0.000004783
0.000015712
-0.000003901

Iteration No. 4
Delta:
0.000055583

4
-0.000020586
-0.000018913
0.000006702

Iteration No. 5
Delta:
-0.000000193
0.000000041
0.000000150
-0.000000039

Iteration No. 6
Delta:
0.000000444
-0.000000152
-0.000000140
0.000000047

Iteration No. 7
Delta:
-0.000000001
0.000000000
0.000000001
-0.000000000

Total GLS iterations = 7


Final parameter values:
a0 = 5.275753; a1 = 0.146279; a2 = 0.224752; a3 = 0.025396

Final residuals:
-----|v_x|--------|v_y|--------|v_z|-----
0.006158 0.008669 -0.031214
-0.000114 -0.000170 0.000565
-0.021790 -0.034979 0.108129
0.014333 0.026151 -0.074007
0.004772 0.006017 -0.022092
-0.004815 -0.006540 0.021963
-0.018990 -0.027604 0.085044
0.000823 0.001293 -0.003672
0.007065 0.007909 -0.028924
-0.001510 -0.001800 0.006049
0.028425 0.038031 -0.116011
-0.005952 -0.008271 0.023525
0.005887 0.006046 -0.021552
-0.004688 -0.005272 0.017261
-0.032470 -0.039135 0.118926

5
0.022865 0.029655 -0.083991

Adjusted observations:
-----|x|----------|y|----------|z|-----
2.086158 2.008669 6.138786
2.999886 2.179830 6.370565
3.888210 2.175021 6.548129
5.064333 1.866151 6.675993
1.874772 2.746017 6.297908
2.875185 2.873460 6.551963
3.931010 3.032396 6.835044
5.010823 3.061293 7.086328
1.917065 3.857909 6.611076
2.868490 4.068200 6.906049
4.058425 3.888031 7.143989
4.994048 4.201729 7.483525
2.195887 4.996046 6.998448
3.175312 4.934728 7.247261
4.107530 4.990865 7.518926
5.052865 4.959655 7.766009

Hypothesis is accepted!!

----|cv1|-------|ts|-------|cv2|----
4.4038 7.3741 23.3367

Variance (prior) = 0.100000

Published with MATLAB R2015a

You might also like