Not all graphs appear at the same time
3 views (last 30 days)
Show older comments
I'm graphing the results of a code. I need four graphs to appear in the same plane and I'm using the "hold on/hold off" instruction but I only see three... I've tried to modify the order, remove one and put another, etc. But I never see all four at the same time. I attach all the code and appreciate any help in this regard.
%%%%%Condiciones iniciales
Phi=0.95;
n=50;
P0=1;
x0=sqrt(P0)*randn(1,1);
Q0=0.1;
w0=sqrt(Q0)*randn(1,1);
x(1)=Phi*x0+w0;
%%%%Estado%%%%%%
for k=1:n
Q(k)=0.1;
w(k)=sqrt(Q(k))*randn(1,1);
x(k+1)=Phi*x(k)+w(k);
end
%%%%%Observaciones%%%%%
R0=0.5;
v0=sqrt(R0)*randn(1,1);
z0=x0+v0;
for k=1:n
R(k)=0.5;
v(k)=sqrt(R(k))*randn
z(k)=x(k)+v(k)
end
%%%%Filtrado%%%
Cpred0=P0;
K0=Cpred0*[P0+R0]^-1;
fil0=K0*z0;
Cfil0=[1-K0]*Cpred0;
pred(1)=Phi*fil0;
Cpred(1)=Phi^2*Cfil0+Q0;
for k=1:n
K(k)=Cpred(k)*[Cpred(k)+R(k)]^-1;
fil(k)=pred(k)+K(k)*[z(k)-pred(k)];
Cfil(k)=[1-K(k)]*Cpred(k);
pred(k+1)=Phi*fil(k);
Cpred(k+1)=Phi^2*Cfil(k)+Q(k);
end
%%%%%Suavizamiento de punto fijo N=2%%%%%
for k=1:n
L(k,k)=Cfil(k);
pf(k,k)=fil(k);
Cpf(k,k)=Cfil(k);
end
for k=1:n-2
for i=1:2
kpf(k,k+i)=L(k,k+i-1)*Phi*(Cpred(k+i))^-1;
L(k,k+i)=L(k,k+i-1)*Phi*(1-K(k+i-1));
pf(k,k+i)=pf(k,k+i-1)+kpf(k,k+i)*(z(k+i)-pred(k+i));
Cpf(k,k+i)=Cpf(k,k+i-1)-kpf(k,k+i)*(pred(k+i)+R(k))*kpf(k,k+i);
end
end
for k=1:n
pf1(k)=pf(k,k);
Cpf1(k)=Cpf(k,k);
end
hold on
plot(x)
plot(z,'--')
plot(fil)
plot(pf1)
hold off
title('Estimación de estados con filtro de Kalman y suavizamiento punto fijo')
xlabel('Tiempos k')
legend('Estados simulados','Observaciones','Filtrado','Suavizado punto fijo')
0 Comments
Answers (1)
Steven Lord
on 1 Aug 2022
%%%%%Condiciones iniciales
Phi=0.95;
n=50;
P0=1;
x0=sqrt(P0)*randn(1,1);
Q0=0.1;
w0=sqrt(Q0)*randn(1,1);
x(1)=Phi*x0+w0;
%%%%Estado%%%%%%
for k=1:n
Q(k)=0.1;
w(k)=sqrt(Q(k))*randn(1,1);
x(k+1)=Phi*x(k)+w(k);
end
%%%%%Observaciones%%%%%
R0=0.5;
v0=sqrt(R0)*randn(1,1);
z0=x0+v0;
for k=1:n
R(k)=0.5;
v(k)=sqrt(R(k))*randn; % Adding some semicolons
z(k)=x(k)+v(k);
end
%%%%Filtrado%%%
Cpred0=P0;
K0=Cpred0*[P0+R0]^-1;
fil0=K0*z0;
Cfil0=[1-K0]*Cpred0;
pred(1)=Phi*fil0;
Cpred(1)=Phi^2*Cfil0+Q0;
for k=1:n
K(k)=Cpred(k)*[Cpred(k)+R(k)]^-1;
fil(k)=pred(k)+K(k)*[z(k)-pred(k)];
Cfil(k)=[1-K(k)]*Cpred(k);
pred(k+1)=Phi*fil(k);
Cpred(k+1)=Phi^2*Cfil(k)+Q(k);
end
%%%%%Suavizamiento de punto fijo N=2%%%%%
for k=1:n
L(k,k)=Cfil(k);
pf(k,k)=fil(k);
Cpf(k,k)=Cfil(k);
end
for k=1:n-2
for i=1:2
kpf(k,k+i)=L(k,k+i-1)*Phi*(Cpred(k+i))^-1;
L(k,k+i)=L(k,k+i-1)*Phi*(1-K(k+i-1));
pf(k,k+i)=pf(k,k+i-1)+kpf(k,k+i)*(z(k+i)-pred(k+i));
Cpf(k,k+i)=Cpf(k,k+i-1)-kpf(k,k+i)*(pred(k+i)+R(k))*kpf(k,k+i);
end
end
for k=1:n
pf1(k)=pf(k,k);
Cpf1(k)=Cpf(k,k);
end
hold on
plot(x)
plot(z,'--')
plot(fil)
plot(pf1)
hold off
title('Estimación de estados con filtro de Kalman y suavizamiento punto fijo')
xlabel('Tiempos k')
legend('Estados simulados','Observaciones','Filtrado','Suavizado punto fijo')
Let's check to see if one of your lines is being plotted outside the Y limits of your axes.
[minval, maxval] = bounds(x)
[minval, maxval] = bounds(z)
[minval, maxval] = bounds(fil)
[minval, maxval] = bounds(pf1)
So no, all four are being plotted within the bounds of the Y axis limits. But from the fact that the bounds of fil and pf1 are displayed the same, I'm suspicious if they have the exact same values.
norm(fil-pf1)
Yes, they do. So all four of your lines are being plotted, it's just that one of them is being plotted exactly on top of another.
0 Comments
See Also
Categories
Find more on Graphics Performance 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!