How to plot standard dev. from a closed-loop trajectory

5 vues (au cours des 30 derniers jours)
Luis FigueroaFernandez
Luis FigueroaFernandez le 1 Fév 2022
Commenté : William Rose le 1 Fév 2022
Hello, I am trying to plott the standard deviation of the X and Y points of a trajectory (T) obtained as a mean of different trajectories. So far I have tried the the easy-way-out that was adding and subtracting the sd to the mean of T and plotting those:
T = plot(x, mean_y, 'Color', 'k', 'LineWidth', 2);
sd = plot(x, [mean_y - 2*std_y; mean_y + 2*std_y], 'Color', 'r');
However, this doesn't work when you have a closed loop.
My second idea was to get the tangent of each point in my plot through the differential of my plot, and obtain each point on the tangent of this derivetive at a distance of sd. However I am not 100% sure of how to implement this. So far I have:
der_x = diff(R_avg_z)./diff(R_avg_x) %tangent of the curve
I would trully appreciate some help with this as this is not my field of expertiese.
Thank you
Alonso

Réponse acceptée

William Rose
William Rose le 1 Fév 2022
Modifié(e) : William Rose le 1 Fév 2022
[edited: added comments to the code]
[edited again to correct a mistake in my code, in the first plot() call]
If all your trajectories have the same number of points:
clear;
M=20; %number of trajectories
N=101; %points per trajectory
t=(0:N-1)/(N-1);
sigma=0.1; %noise amplitude
%Next: Create M closed loop trajectories.
%Each row of array x has the x values for one trajectory.
x=repmat(cos(2*pi*t),M,1)+sigma*randn(M,N);
%Each row of array y has the y values for one trajectory.
y=repmat(sin(2*pi*t),M,1)+sigma*randn(M,N);
%estimate the mean and SD in x- and y-directions at each point
mnX=mean(x); mnY=mean(y);
sdX=std(x); sdY=std(y);
%plot all the trajectories and the mean trajectory
figure
subplot(121)
for i=1:M
plot(x(i,:),y(i,:),'-b.');
hold on
end
xlabel('X'); ylabel('Y'); axis equal; grid on
plot(mnX,mnY,'-r','LineWidth',2)
%plot the mean trajectory and +-1 standard deviation
subplot(122)
for i=1:N
pos = [mnX(i)-sdX(i),mnY(i)-sdY(i),2*sdX(i),2*sdY(i)];
%uncomment next line, and comment out line above, if you want +-2 SD
%pos = [mnX(i)-2*sdX(i),mnY(i)-2*sdY(i),4*sdX(i),4*sdY(i)];
rectangle('Position',pos,'Curvature',[1 1],'FaceColor',[1,.7,.7],'EdgeColor','none')
hold on;
end
xlabel('X'); ylabel('Y'); axis equal; grid on
plot(mnX,mnY,'-r','LineWidth',2)
The left hand plot shows all the trajectories and the mean trajectory. The rifht hand plot shows the mean trajectory and +-1 standard deviation.
  3 commentaires
Luis FigueroaFernandez
Luis FigueroaFernandez le 1 Fév 2022
Oh Wow!
That is is a super smart way to go through the problem! thank you so much. The results are better than expected.
Thank you so much!
William Rose
William Rose le 1 Fév 2022
You're welcome. Your figure looks cool. Like looking into a cave.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Line Plots dans Help Center et File Exchange

Produits


Version

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by