problem in matlab programs

1 view (last 30 days)
dab483
dab483 on 6 Jul 2012
Hi, I'm having problem to plot my figure when i try to run the program several times to get an average value of rmse. I know there is something wrong the way i save the data for plotting, but i dont know how to solve it.
if i set the no_of_runs more than 1,let say 2 the previous data will still be there and making the data save in the xArray much bigger (1x201), hence the vector of t (1x101 double) and xArray and others will not be the same any more and fail to plot.
how do i make the array will still be (1x101 double) everytime when no_of_runs more than 1? thank you.
tf = 100; % simulation length
no_of_runs=2
x=1; %%initialize
xhatukf1Array = 1;
Pukf1= ..
Pukf1array = diag(Pukf1);
%%start program
for k=1: no_of_runs
for t=1:tf
.....equations...+++
x= ...
z=....
Kukf1 = Pxy1 /(Py1);
xhatukf1 = xhatukf1 + Kukf1 * (z - zhat1);
Pukf1 = Pukf1 - Kukf1 * Py1 * Kukf1';
% Save data for plotting.
xArray = [xArray x];
xhatukf1Array = [xhatukf1Array xhatukf1];
Pukf1array = [Pukf1array diag(Pukf1)];
end %%(end t loop)
t = 0 : tf;
figure;
plot(t, xArray(1,:),'k-',t,xhatukf1Array(1,:),'m');
xlabel('Seconds');
legend('True Position','ukf2n+1');
rmsError_ukf2N(k) = sqrt(inv(tf)*sum((xArray-xhatukf1Array).^(2)));
end %%(end k loop)

Accepted Answer

Jan
Jan on 6 Jul 2012
for k=1: no_of_runs
xArray = [];
...
end
A pre-allocation would be more efficient:
for k=1: no_of_runs
xArray = zeros(<LENGTH_OF_x>, tf); % I do not know "size(x)"
for t=1:tf
xArray(:, t) = x;
end
end
  1 Comment
dab483
dab483 on 6 Jul 2012
thank you so much.. now i can plot when no_of_runs more than 1. :)

Sign in to comment.

More Answers (0)

Categories

Find more on Graphics Objects in Help Center and File Exchange

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!