Plotting several graphs in one plot
显示 更早的评论
So I've been on this problem for quite some time now, and I've looked everywhere online but I cant find it anywhere. So hopefully somebody could help me with this.
So basically, I am trying to plot particle filters through 50 time steps. I get a graph for that plot. Although, my deliemma is when I try to loop the result so that I get the graph 20 times, on the same plot.
I want to have a single plot with 20 different graphs of the same function. I dont know if thats really clear. but heres my code in very short term.
for test = 1:20
hold on
for k = 1 : 50
% True Values of State & Measurement
x = 0.5 * x + 25 * x / (1 + x^2) + 8 * cos(1.2*(k-1)) + sqrt(Q) * randn;
y = x^2 / 20 + sqrt(R) * randn;
% Particle filter
for i = 1 : 200
Xminus(i) = 0.5 * X(i) + 25 * X(i) / (1 + X(i)^2) + 8 * cos(1.2*(k-1)) + sqrt(Q) * randn;
Y = Xminus(i)^2 / 20;
Likelihood = y - Y;
q(i) = (1 / sqrt(R) / sqrt(2*pi)) * exp(-Likelihood^2 / 2 / R);
end
.
.
.
end %for the k=1:50
t = 0 : 50;
figure;
subplot (3,1,1)
plot(t, x0, 'b.', t, xPFArr, 'k-');
xlabel('Time Step');
ylabel('State');
subplot (3,1,2)
plot(t, x0, 'b.', t, xAuxArr, 'g:');
xlabel('Time Step');
ylabel('State');
subplot (3,1,3)
plot(t, x0, 'b.', t, xRegArr, 'r:');
xlabel('Time Step');
ylabel('State');
end %for the test
So each of the subplots are diffrent particle filters. I tried doing hold on before the k loop but for some reason it displays the first loop so time step to 50 of one graph. And then when it goes for the 2nd iteration my data has 100 steps instead of 50.
DOes anybody know why? And how I could fix this problem? That would be great,
fabio
采纳的回答
更多回答(1 个)
Paulo Silva
2011-8-26
Do it like this:
s1=subplot(311);hold(s1) %create each subplot and hold it
s2=subplot(312);hold(s2)
s3=subplot(313);hold(s3)
for n=1:10
axes(s1) %select the current axes
%you can avoid selecting the current axes
%plot(s1,n,1,'*') %alternative way
plot(n,1,'*')
axes(s2)
plot(n,1,'r*')
axes(s3)
plot(n,1,'g*')
end
6 个评论
Oleg Komarov
2011-8-26
Did subplot always support the syntax subplot(mnp)?
Paulo Silva
2011-8-26
no idea, I've been using it since MATLAB 2007b
Oleg Komarov
2011-8-26
Thanks.
Fangjun Jiang
2011-8-26
@Paulo, you are weird! Did you check every source m file before using it? I never saw subplot(mnp) before but apparently it works.
Paulo Silva
2011-8-26
No, that trick and others you learn from experienced MATLAB users (here, blogs, video tutorials, books, etc) :) and yes I'm weird :D
Walter Roberson
2011-8-26
That aspect of subplot is documented. Look at the very first of the "syntax" lines in the reference documentation:
h = subplot(m,n,p) or subplot(mnp)
类别
在 帮助中心 和 File Exchange 中查找有关 2-D and 3-D Plots 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!