I have algorithm I have estimated the data pretty well , but I 'm not able to forecast it .
3 次查看(过去 30 天)
显示 更早的评论
I have estimated my data through a fucnction that contains Kalmen filter algorthim and it estimated the data well and I have tried to use forecast function and i found those errors
Error: File: forecast.m Line: 23 Column: 33
Character vector is not terminated properly.
my data
function h = GetSonar()
%
%
persistent sonarAlt % SonartAlt.mat
persistent k firstRun
if isempty(firstRun)
load SonarAlt
k = 1;
firstRun = 1;
end
h = sonarAlt(k);
k = k + 1;
the full code is for kalman
function [pos vel Px] = DvKalman(z)
%
%
persistent A H Q R
persistent x P
persistent firstRun
if isempty(firstRun)
firstRun = 1;
dt = 1; % sec, for sonar
A = [ 1 dt ;
0 1 ];
H = [1 0];
Q = [ 1 0 ;
0 3 ];
R = 10;
x = [ 0 20 ]';
P = 5*eye(2);
end
% Kalman filter algorithm
xp = A*x;
Pp = A*P*A' + Q;
K = Pp*H'*inv(H*Pp*H' + R);
x = xp + K*(z - H*xp);
P = Pp - K*H*Pp;
pos = x(1);
vel = x(2);
Px = P ;
clear all
Nsamples = 1500;
Xsaved = zeros(Nsamples, 2);
Zsaved = zeros(Nsamples, 1);
Psaved = zeros(Nsamples, 2);
for k=1:Nsamples
z = GetSonar();
[pos vel P] = DvKalman1(z);
Xsaved(k, :) = [pos vel];
Zsaved(k) = z;
Psaved(k,1:2) = [P(1,1) P(2,2)];
end
dt = 0.02;
t = 0:dt:Nsamples*dt-dt;
fig = figure ;
left_color = [0 0 0]; % black
right_color= [0 0 1]; % blue
set(fig,'defaultAxesColorOrder',[left_color; right_color]);
hold on
yyaxis left
plot(t, Zsaved(:), 'r.','markersize',20)
plot(t, Xsaved(:, 1),'k-','linewidth',4)
ylabel('position (m)')
yyaxis right
plot(t, Xsaved(:, 2),'b-','linewidth',4)
xlabel('time (s)'); ylabel('velocity (m/s)')
title('Position and Velocity from Noisy Sonar Measurements')
legend('Position, Raw Measurements','Position from Kalman Filter',...
'Velocity from Kalman Filter')
set(gca,'fontsize',18); grid on
set(gcf, 'Position', [10, 100, 1200, 500])
%% Extra things to plot
figure
plot(t,Psaved(:,1),'k')
figure
plot(t,Psaved(:,2),'b')
figure
t=0:dt:(Nsamples-1)*dt-dt;
vel_est = diff(Zsaved)/dt;
plot(t,vel_est,'r.','markersize',20)
hold on
vel_est2 = diff(Xsaved(:,1))/dt;
plot(t,vel_est2,'k-','linewidth',2)
plot(t,Xsaved(1:(end-1),2),'b','linewidth',4)
grid
xlabel('time (s)'); ylabel('velocity (m/s)')
legend('Finite Difference of Measurement',...
'Finite Difference of Estimated Position',...
'Kalman Filter Velocity Estimate');
set(gca,'fontsize',18); grid on
and forecast fuction
function YP = forecast(model,data,K, Init)
%FORECAST Forecast a time series K steps into the future
%
% YF = FORECAST(MODEL,DATA,K)
%
% DATA: Existing data up to time N, an IDDATA object.
% MODEL: The model as any IDMODEL object, IDPOLY, IDSS, IDARX or IDGREY.
% K: The time horizon of forecasting, a positive integer with the number of
samples
% YF: The forecasted output after time N, an IDDATA object with output
% only, covering the time span N+1:N+K.
%
% YF = FORECAST(MODEL,DATA,K, INIT)
% wehere INIT is 'z' or 'e' allows specification of initial conditions (at
% time = Data.SamplingInstants(1)).
%
% See also idmodel/predict, which computes a fixed horizon prediction
% along the existing data record.
[N, ny] = size(data); % assume data is iddata
Mss = idss(model);
ord = size(pvget(Mss,'A'),1);
if ord>N
error('Forecast:TooFewSamples','The data should contain at least %d
samples.',ord)
end
if nargin<4, Init = 'e'; end
yp = zeros(K,ny);
mp = getPredictor(Mss);
[Ap,Bp,Cp] = ssdata(mp);
if Init=='z'
xt = ltitr(Ap, Bp, data.y); % use zero init
x0 = xt(end,:);
else % Init == 'e'
[A1,B1,C1,D1,K1] = ssdata(Mss);
x00 =
x0est(data.y,A1,B1,C1,D1,K1,size(C1,1),size(B1,2),250e3,eye(size(C1,1)));
x0 = ltitr(Ap,Bp,data.y,x00); x0 = x0(end,:);
end
u = [data.y(end,:); zeros(1,ny)];
for ct = 1:K
xt = ltitr(Ap, Bp, u, x0);
x0 = xt(end,:);
yp(ct,:) = (Cp*x0.').';
u = [yp(ct,:); zeros(1,ny)];
end
YP = data; YP.y = yp; YP.u = []; YP.Name = '';
YP.UserData = []; YP.Notes = '';
YP.Tstart = data.Ts*(N+1);
%------local function -----------------------------------
function mp = getPredictor(sysd)
[A,B,C,D,K] = ssdata(sysd);
Ts = sysd.Ts;
Ny = size(D,1);
mp = idss(A-K*C, [K B-K*D], C, [zeros(Ny), D], zeros(size(A,1),Ny), 'Ts',
Ts);
mp.InputDelay = [zeros(Ny,1); sysd.InputDelay];
0 个评论
回答(1 个)
Rik
2024-1-26
As the error message is telling you, the problem is here;
error('Forecast:TooFewSamples','The data should contain at least %d
samples.',ord)
There is a new line, but that results in an improper termination of the char vector. You should use the edit below.
error('Forecast:TooFewSamples','The data should contain at least %d samples.',ord)
4 个评论
Rik
2024-1-26
Why are you not formatting your code as code? This makes your post hard to read, and makes it harder to run in the forum interface.
What have you tried so far to solve this problem? I don't have experience with the functions you're using, so I would have to do the same: start by reading the documentation and google for solutions. What did you find? Why didn't those results solve your problem?
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Subspace Methods 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!