Particle filter error solution
Show older comments
Hi, I am still new to malab. Try to run a simple particle filter but error keep appear saying too many input arguments and error in
(line 77 ) [xh(:,k), pf] = particle_filter(xk, Bond, y(:,k), pf,'systematic_resampling'); saying unrecognized y function. Please help me.
%% clear memory, screen, and close all figures
clear, clc, close all;
%% initial setting
T = 4; %step number
Ns = 10; %number of ensemble
MB = 60; %number of bond to 100% cured
%% Predictive ensemble formation
nx = 3; %numberofstate (k, Bond, vk)
%nx = size(xk1);
xk = zeros(233,5800,67); %taktahubetulke tak
for j = 1:Ns
end
Bond = xk(3,:,:);
Fbond = zeros(Ns,1);
for j = 1:Ns
Fbond(j,:) = Bond(j);
end
%% Observed value matrix
ny = 1; %number of observations
Y = '(0 ; 16.7)';
%yk = @(k) Y(ny,k);
yk = Y(ny,2);
%yek = zeros(ny,Ns,m);
%yek = zeros(ny,Ns);
%yek(ny,1:Ns) = yk;
%obs = @(k, xk, vk) (1-(xk(3,1:Ns)/MB)) + vk;
obs = @(k, Bond, vk) ((Bond/MB)*100) + vk;
%% process noise and noise generator function
nu = 1; % size of the vector of process noise
sigma_u = sqrt(10);
p_sys_noise = @(u) normpdf(u, 0, sigma_u);
gen_sys_noise = @(u) normrnd(0, sigma_u); % sample from p_sys_noise (returns column vector)
%% observation noise and noise generator function
nv = 1; % size of the vector of observation noise
sigma_v = sqrt(1);
p_obs_noise = @(v) normpdf(v, 0, sigma_v);
gen_obs_noise = @(v) normrnd(0, sigma_v); % sample from p_obs_noise (returns column vector)
%% Likelihood p(y[k] | x[k])
% (under the suposition of additive process noise)
%p_yk_given_xk = @(k, yk, xk) p_obs_noise(yk - obs(k, xk, 0));
R = cov(Fbond);
test = (yk-obs(2,Bond,0));
p_yk_given_xk = @(k, yk, Bond) (1/((2*pi)*det(R))^(1/2))*exp((-1/2)*(yk-obs(k,Bond,0))*(R^(-1))*(yk-obs(k,Bond,0)));
%p_yk_given_xk = @(k, yk, Bond) (1/((2*pi)*det(R))^(1/2))*exp((-1/2)*(test)'*(R^(-1))*(test));
YX = (1/((2*pi)*det(R))^(1/2))*exp((-1/2)*(yk-obs(2,Bond,0)).*(R^(-1)).*(yk-obs(2,Bond,0)));
%% %% Separate memory space
x = zeros(nx,T); y = zeros(ny,T);
u = zeros(nu,T); v = zeros(nv,T);
%% Separate memory
xh0 = 0;
xh = zeros(nx, T); xh(:,1) = xh0;
yh = zeros(Ns, T); yh(:,1) = obs(1, xh0, 0);
gen_x0 = @(x) normrnd(0, sqrt(10));
pf.k = 1; % initial iteration number
pf.Ns = 10; % number of particles
pf.w = zeros(pf.Ns, T); % weights
pf.particles = zeros(nx, pf.Ns, T); % particles
pf.gen_x0 = gen_x0; % function for sampling from initial pdf p_x0
pf.p_yk_given_xk = p_yk_given_xk; % function of the observation likelihood PDF p(y[k] | x[k])
pf.gen_sys_noise = gen_sys_noise; % function for generating system noise
%pf.p_x0 = p_x0; % initial prior PDF p(x[0])
%pf.p_xk_given_ xkm1 = p_xk_given_xkm1; % transition prior PDF p(x[k] | x[k-1])
%% Estimate state
for k = 2:T
fprintf('Iteration = %d/%d\n',k,T);
% state estimation
pf.k = k;
%[xh(:,k), pf] = particle_filter(sys, y(:,k), pf, 'multinomial_resampling');
[xh(:,k), pf] = particle_filter(xk, Bond, y(:,k), pf,'systematic_resampling');
% filtered observation
yh(:,k) = obs(k, Bond(:,k), 0);
end
%% plot of the state vs estimated state by the particle filter vs particle paths
figure
hold on;
h1 = plot(1:T,squeeze(pf.particles),'y');
h2 = plot(1:T,x,'b','LineWidth',4);
h3 = plot(1:T,xh,'r','LineWidth',4);
h4 = plot(1:T,xhmode,'g','LineWidth',4);
legend([h2 h3 h4 h1(1)],'state','mean of estimated state','mode of estimated state','particle paths');
title('State vs estimated state by the particle filter vs particle paths','FontSize',14);
Answers (1)
Walter Roberson
on 4 Nov 2022
1 vote
My guess is that you are using the particle_filter function from https://www.mathworks.com/matlabcentral/fileexchange/35468-particle-filter-tutorial . That function expects sys, yk, pf, resampling_strategy where sys is an aonymous function, pf is a struct, and that is the correct spelling of the resampling_strategy option.
The function does not support passing in a pair of numeric arrays.
Look at the commented out line directly before it to see what it expects.
It is possible, of course, that you are expecting to use a modified version of that code... if so then you need to have that modified version on your path (before the original version if the original version is still present.)
4 Comments
シティヌルシュハダ モハマド ナシル
on 4 Nov 2022
Walter Roberson
on 4 Nov 2022
xk = zeros(233,5800,67); %taktahubetulke tak
You initialize xk to all zero, which is possibly a good thing to do.
for j = 1:Ns
end
You have a loop there, but you do not do anything inside the loop. It is not clear why you have that loop.
Bond = xk(3,:,:);
xk has not changed since it was initialized, so xk is still all zero. It is not clear why you would pick out the third row specifically out of the all-zero array.
Fbond = zeros(Ns,1);
for j = 1:Ns
Fbond(j,:) = Bond(j);
end
Bond is all zero because it was taken from the all-zero xk, so your loop is overwriting the Fbond = zeros with values that are all zero. It is not clear why you would do that.
for k = 2:T
fprintf('Iteration = %d/%d\n',k,T);
% state estimation
pf.k = k;
%[xh(:,k), pf] = particle_filter(sys, y(:,k), pf, 'multinomial_resampling');
[xh(:,k), pf] = particle_filter(xk, Bond, y(:,k), pf,'systematic_resampling');
% filtered observation
yh(:,k) = obs(k, Bond(:,k), 0);
end
You have not changed xk since you assigned it all zero, and you do not change it inside the loop. You have not changed Bond since you assigned it all zero, and you do not change it inside the loop. It is not clear why you would want to pass xk or Bond in to particle_filter() instead of the sys that it was designed to work with ?
The particle_filter is designed for the situation in which particle locations evolve according to a function handle, with the function handle named sys for this code. If you want the particle locations to stay constant 0, then instead of editing the particle filter code to try to force it to work with constant arrays instead of with a function handle, then I would suggest you should instead just pass in
sys = @(k, xkm1, uk) zeros(size(xkm1,1),1)
or something like that.
I do not know why you changed the interface to particle_filter so it is difficult to make recommendations.
シティヌルシュハダ モハマド ナシル
on 7 Nov 2022
シティヌルシュハダ モハマド ナシル
on 7 Nov 2022
Edited: Walter Roberson
on 7 Nov 2022
Categories
Find more on Online State Estimation in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!