matlab code to find least square and weighted least square

12
3.8.3 MATLAB Code x=[2 2.5 3 5 9]'; y=[-4.2 -5 2 1 24.3]'; %%% best least square fitting%%%%%%%%%%%%%%%%%%%%%% A=[x ones(length(x),1)]; c_LU = inv((A'*A)) * A' * y; y_LU=c_LU(1).* x + c_LU(2) .* ones(length(x),1); %%%%% weighted Least square fitting %%%%%%%%%%% W = diag([10 1 1 1 10]); c_WLU = inv((A'*W*A)) * A' *W * y; y_WLU=c_WLU(1).* x + c_WLU(2) .* ones(length(x),1); %plotting%%%%%%%%%%%%%% plot(x,y,'o',x,y_LU,'--',x,y_WLU); legend('data plot','best LS','Weighted LS'); Best least square line coefficients: a= 3.9930 b= -13.5498 Weighted matrix , W = diag{10 , 1 , 1 , 1 , 10} Weighted least square line coefficients: a= 4.0895 b= -12.7732

Upload: bikalpa-khatiwada

Post on 03-Oct-2015

238 views

Category:

Documents


1 download

DESCRIPTION

Matlab code to find least square and weighted least square

TRANSCRIPT

3.8.3 MATLAB Code x=[2 2.5 3 5 9]';y=[-4.2 -5 2 1 24.3]';%%% best least square fitting%%%%%%%%%%%%%%%%%%%%%%A=[x ones(length(x),1)];c_LU = inv((A'*A)) * A' * y;y_LU=c_LU(1).* x + c_LU(2) .* ones(length(x),1);%%%%% weighted Least square fitting %%%%%%%%%%%W = diag([10 1 1 1 10]);c_WLU = inv((A'*W*A)) * A' *W * y;y_WLU=c_WLU(1).* x + c_WLU(2) .* ones(length(x),1);%plotting%%%%%%%%%%%%%%plot(x,y,'o',x,y_LU,'--',x,y_WLU);legend('data plot','best LS','Weighted LS');

Best least square line coefficients: a= 3.9930 b= -13.5498Weighted matrix , W = diag{10 , 1 , 1 , 1 , 10} Weighted least square line coefficients: a= 4.0895 b= -12.7732

5) MATLAB Code for forward predictorclear all;nstd=sqrt(0.1);v=nstd.*randn(14,1);x = zeros(14,1); for i = 1:14 if (i == 1) x(i) = v(i); elseif (i==2) x(i)=-0.3957 * x(i-1) + v(i); elseif (i==3) x(i)=-0.3957* x(i-1) + 0.2417 * x(i-2) + v(i); elseif(i==4) x(i)=-0.3957 * x(i-1) + 0.2417 * x(i-2) - 0.0454 * x(i-3)+ v(i); else x(i)=-0.3957*x(i-1) + 0.2417 *x(i-2)-0.0454 *x(i-3)-0.3136*x(i-4)+v(i); end end%%%% forming data matrix A %%%%%%%%%%%%%%%%%%A= [];for i = 1:10 d= [x(i+3) x(i+2) x(i+1) x(i)]; A= [A;d];end R = A' *A;Y=[];for i=1:10 Y=[Y;x(i+4)];end p=A'*Y;c= inv(R)*p;x_hat = zeros(14,1);for i = 1:14 if (i == 1) x_hat(i) = 0; elseif (i==2) x_hat(i)=c(1) * x(i-1) ; elseif (i==3) x_hat(i)=c(1) * x(i-1) + c(2)* x(i-2) ; elseif(i==4) x_hat(i)=c(1) * x(i-1) + c(2) * x(i-2) +c(3) * x(i-3); else x_hat(i)=c(1) * x(i-1) + c(2) * x(i-2)+ c(3)* x(i-3) + c(4) * x(i-4) ; end end%%%%%%%%% plotting of x(t)%%%%%%%%%%%%%%%%%%%% plot(x);hold on ;plot(x_hat,'-r'); w=-pi:0.01:pi;for i= 1:length(w)S_true(i) = (abs(0.1./(1+0.3957.*exp(-j.*w(i))-0.2416.*exp(-j*2.*w(i)) + 0.0454.*exp(-j*3.*w(i)) + 0.3135 .* exp(-j*4.*w(i))))).^2; endhold off;figure(2);plot(w,S_true);hold on;S_est = (abs(0.1./(1+c(1).*exp(-j.*w)+ c(2).*exp(-j*2.*w) + c(3).*exp(-j*3.*w) + c(4) .* exp(-j*4.*w)))).^2;plot(w,S_est,'--r');

MATLAB Code For Forward/Backward Predictor%%%%%%%% prediction using forward /backward predictor %%%%%%%%%%5clear allnstd=sqrt(0.1);v=nstd.*randn(14,1);x = zeros(14,1);for i = 1:14 if (i == 1) x(i) = v(i); elseif (i==2) x(i)=-0.3957 * x(i-1) + v(i); elseif (i==3) x(i)=-0.3957 * x(i-1) + 0.2417 * x(i-2) + v(i); elseif(i==4) x(i)=-0.3957 * x(i-1) + 0.2417 * x(i-2) - 0.0454 * x(i-3)+ v(i); else x(i)=-0.3957 * x(i-1) + 0.2417 * x(i-2) - 0.0454 * x(i-3) -0.3136 * x(i-4) + v(i); end end%%%% forming data matrix A %%%%%%%%%%%%%%%%%%A= [];for i = 1:16 if (i> c c = -0.1200 0.2707 0.0066 -0.2556 Using forward/Backward predictor the coefficient is obtained as:>> cc = -0.3412 0.3855 -0.0244 -0.3774

Plots Comparison of x[t] and x_hat[t]:i) Using Forward predictor

ii) Using Forward /Backward Predictorc) i) Using Forward predictor

ii) Using Forward/Backward predictor 6) MATLAB Codeclear all;nstd=sqrt(0.5);v=nstd.*randn(14,1);x = zeros(14,1);for i = 1:14 x(i)=0.1*cos(0.21*i)+0.6*cos(0.23*i) + v(i); end %%%% forming data matrix A %%%%%%%%%%%%%%%%%%A= [];for i = 1:10 d= [x(i+3) x(i+2) x(i+1) x(i)]; A= [A;d];end R = A' *A;Y=[];for i=1:10 Y=[Y;x(i+4)];end p=A'*Y;c= inv(R)*p;x_hat = zeros(14,1);for i = 1:14 if (i == 1) x_hat(i) = 0; elseif (i==2) x_hat(i)=c(1) * x(i-1) ; elseif (i==3) x_hat(i)=c(1) * x(i-1) + c(2)* x(i-2) ; elseif(i==4) x_hat(i)=c(1) * x(i-1) + c(2) * x(i-2) +c(3) * x(i-3); else x_hat(i)=c(1) * x(i-1) + c(2) * x(i-2)+ c(3)* x(i-3) + c(4) * x(i-4) ; end end%%%%%%%%% plotting of x(t)%%%%%%%%%%%%%%%%%%%% plot(x);hold on ;plot(x_hat,'-r'); hold off;figure(2)w=-pi:0.01:pi;S_est = (abs(0.5./(1+c(1).*exp(-j.*w)+ c(2).*exp(-j*2.*w) + c(3).*exp(-j*3.*w) + c(4) .* exp(-j*4.*w)))).^2;plot(w,S_est,'-r');i) When using frequency as 0.2

ii) When using frequency as 0.21

7b )

c)d)