Вы находитесь на странице: 1из 9

Advance Engineering Mathematics assignment

Curve Fitting to Data:


Estimate the model from a given data set. The data for this problem is titled data.mat. The first row of
the data is the input to the system while the second row is the corresponding system output with some
noise.
1. Plot the data.

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) Estimate the optimal values of the coefficients a, b, c of the model.

A=[x.^2 x ones(length(x),1)];
c=inv(A'*A)*A'*y; %optimal values of the coefficients of the model

c =[0.0938;0.0031;2.7823] 0.0938* x2 +0.0031*x+2.7823

(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

SSresid=305.3813 = sum of square of error

SStotal = (length(y)-1) * var(y);


rsq = 1 - SSresid/SStotal %coefficient of determination

coefficient of determination = rsq =0.8400

This demonstrates that the quadratic equation 0.0938* x 2 +0.0031*x+2.7823 predicts 84% of the
variance in the variable y.

Fitting a linear equation y = ax + b to the observed data:

(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

SSresid= 1.9090e+03 = sum of square of error

SStotal = (length(y)-1) * var(y);


rsq = 1 - SSresid/SStotal %coefficient of determination

Coefficient of determination = rsq = 3.4751e-05

This demonstrates that the linear equation 0.0031*x+5.9404 predicts 3.4751e-03% of the variance in the
variable y.

2. Which model do you think is more suitable? Give your reasoning.


The results indicates that a quadratic least-squares fit of the data is a more suitable model because the
sum of square of error is minimized when a quadratic equation is fitted to the observed data.
Linear Prediction:
Predict future values of a time series using linear prediction. The data for this problem is titled
data2.mat. It contains a time-series data recorded for 20 seconds. The first row of the data is the time
data while the second row is the corresponding value of some physical phenomenon.
1. Plot the data
load data2.mat;
time=data2(1,:)';
x=data2(2,:)';
t=time/2;
plot(t,x,'.');
xlabel('Time (Seconds)');
ylabel('System output');
title('Linear prediction')
Using a second order linear predictor x(t) = c1x(t−1) + c2x(t−2):

(a) Estimate the optimal values of the coefficients ci of the model.


k=2;
y=zeros(length(x)-k,1);
for r=1:length(x)-k
y(r)=x(r+k);
end
A=zeros(length(x)-k,k);
for n=1:k
for m=1:length(x)-k
A(m,n)=x(k+(m-n));
end
end
c=inv(A'*A)*A'*y; %optimal values of the coefficients of the model

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:

(a) Estimate the optimal values of the coefficients ci of the model.


k=input('Enter Order of linear predictor K='); %k=5
y=zeros(length(x)-k,1);
for r=1:length(x)-k
y(r)=x(r+k);
end
A=zeros(length(x)-k,k);
for n=1:k
for m=1:length(x)-k
A(m,n)=x(k+(m-n));
end
end
c=pinv(A)*y; %optimal values of the coefficients of the model

c =[ 3.1290; -2.6920; -0.7906; 2.1384; -0.7856 ]


(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)+c(3)*y1(r-3)+c(4)*y1(r-4)+c(5)*y1(r-5);
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')

2. Which model do you think is more suitable? Give your reasoning.


The result indicates that a fifth order linear predictor of the data is a more suitable model.

Вам также может понравиться