Академический Документы
Профессиональный Документы
Культура Документы
load data.mat;
x=data(1,:)';
y=data(2,:)';
figure
plot(x,y,'o');
xlabel('Input to the system');
ylabel('System output');
title('Data')
Fitting a quadratic equation y = ax2 + bx + c to the observed data:
A=[x.^2 x ones(length(x),1)];
c=inv(A'*A)*A'*y; %optimal values of the coefficients of the model
(b) Plot the estimated model together with the observed data.
yestimated=zeros(length(y),1);
for k=1:length(x)
yestimated(k)= c(1)*x(k)^2+c(2)*x(k)+c(3); % Fitting a quadratic equation
end
figure
plot(x,y,'o');
hold on
plot(x,yestimated)
hold off
xlabel('Input to the system');
ylabel('System output');
title('Data')
legend('data','quadratic fit')
(c) Calculate the error when the quadratic equation is used to model the observed data.
yresid=y-yestimated;
SSresid=yresid'*yresid; %sum(yresid.^2) sum of square of error
This demonstrates that the quadratic equation 0.0938* x 2 +0.0031*x+2.7823 predicts 84% of the
variance in the variable y.
(a) Estimate the optimal values of the coefficients a,b of the model.
A=[x ones(length(x),1)];
c=inv(A'*A)*A'*y; %optimal values of the coefficients of the model
c =[0.0031;5.9404] 0.0031*x+5.9404
(b) Plot the estimated model together with the observed data.
yestimated=zeros(length(y),1);
for k=1:length(x)
yestimated(k)= c(1)*x(k)+c(2); % Fitting a linear equation
end
figure
plot(x,y,'o');
hold on
plot(x,yestimated)
hold off
xlabel('Input to the system');
ylabel('System output');
title('Data')
legend('data','linear fit')
(c) Calculate the error when the linear equation is used to model the observed data.
yresid=y-yestimated;
SSresid=yresid'*yresid; %sum(yresid.^2) sum of square of error
This demonstrates that the linear equation 0.0031*x+5.9404 predicts 3.4751e-03% of the variance in the
variable y.
c =[ 1.9508; -0.9757 ]
(b) Predict the time series data for the next 5seconds and plot the predicted data together with the
observed data.
t1=[t;(20.05:0.05:25)'];
x1=[x;zeros((length(t1)-length(t)),1)];
y1=zeros(length(x1),1);
for r=1:length(x)
y1(r)=x1(r);
end
for r=length(x)+1:length(x1)
y1(r)=c(1)*y1(r-1)+c(2)*y1(r-2);
end
figure
plot(t,x,'r');
hold on
plot(t1,y1,'.');
hold off
xlabel('Time (Seconds)');
ylabel('System output');
title(['K= ',num2str(k),' Order Linear predictor'])
legend('measured','forecasted')
Using a 5th order linear predictor: