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

조회 수: 6 (최근 30일)
Luis FigueroaFernandez
Luis FigueroaFernandez 2022년 2월 1일
댓글: William Rose 2022년 2월 1일
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

채택된 답변

William Rose
William Rose 2022년 2월 1일
편집: William Rose 2022년 2월 1일
[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
Luis FigueroaFernandez
Luis FigueroaFernandez 2022년 2월 1일
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 2022년 2월 1일
You're welcome. Your figure looks cool. Like looking into a cave.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Line Plots에 대해 자세히 알아보기

제품


릴리스

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by