% test_data_SVD.m

% function [b,iflag] = test_data_SVD();

%

% This MATLAB function performs an example

% calculation of the use of SVD to perform

% linear least squares calculations.

%

% K. Beers

% MIT ChE

% 7/10/2002

 

 

function [b,iflag] = test_data_SVD();

 

iflag = 0;

 

cutoff = 10*eps;

 

% First, set the true model parameter values

beta_0 = 1;

beta_1 = 2;

beta_2 = 3;

beta = [beta_0; beta_1; beta_2];

 

% Next, set the design matrix for the system

num_exp = 6;

X = zeros(num_exp,3);

X(:,1) = ones(num_exp,1);

X(1,2:3) = [1 2];

X(2,2:3) = [1 2];

X(3,2:3) = [1 3];

X(4,2:3) = [2 3];

X(5,2:3) = [3 3];

X(6,2:3) = [3 2];

 

% Next, generate data

y_true = X*beta;

y = y_true + 0.1*randn(num_exp,1);

 

% Peform SVD of X matrix

[W,S,V] = svd(X);

 

% Get "inverse" of S.

S_inv = S';

for k=1:length(beta)

    if(S_inv(k,k) > cutoff)

        S_inv(k,k) = 1/S(k,k);

    else

        S_inv(k,k) = 0;

    end

end

 

% Get SVD estimate of the parameter vector.

b = V*S_inv*W'*y;

 

iflag = 1;

 

return;