Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
217 views
in Technique[技术] by (71.8m points)

How do I animate the rotation of a set of vectors in MATLAB?

I am trying to visualize the rotation of a coordinate system from a fixed world frame to a rotated body frame. I rotated the frame using a DCM and now I am trying to animate the movement of the new frame (which is 3 orthogonal vectors) from the initial frame to its rotated orientation.

I want to use quiver3 but I cannot get the previous quiver to delete itself from the plot before the next one is drawn. The result is a procession of arrows slowly changing size and turning until they reach their final orientation.

I suspect it is an issue with hold on/ hold off though I do not know.

If anyone could help me create a smooth animation of the vectors rotating in space from their initial starting position to their final orientation I would be appreciative.

Thanks!

clear all;
k = [0; 0; 1;];
j = [0; 1; 0;];
i = [1; 0; 0;];
starts = zeros(3,3);
ends = [i j k];
t = linspace(0,1,100);
yaw = pi/4;
pitch = pi/4;
roll = pi/4;
quiver3(starts(1,:), starts(2,:), starts(3,:), ends(1,:), ends(2,:), ends(3,:))
hold on;
for k = 1:length(t)
    hold on;
    yawDCM = [cos(yaw*t(k)) sin(yaw*t(k)) 0; -sin(yaw*t(k)) cos(yaw*t(k)) 0; 0 0 1*t(k)];
    pitchDCM = [cos(pitch*t(k)) 0 -sin(pitch*t(k)); 0 1*t(k) 0; sin(pitch*t(k)) 0 cos(pitch*t(k));];
    rollDCM = [1*t(k) 0 0; 0 cos(roll*t(k)) sin(roll*t(k)); 0 -sin(roll*t(k)) cos(roll*t(k));];
    Q = yawDCM*pitchDCM*rollDCM;
    ends2 = Q*ends;
%     hold on;
%     plot3(ends2(1,1), ends2(1,2), ends2(1,3), '--');
%     plot3(ends2(2,1), ends2(2,2), ends2(2,3), '--');
%     plot3(ends2(3,1), ends2(3,2), ends2(3,3), '--');
%     pause(.01);
%     hold off;
    quiver3(starts(:,1), starts(:,2), starts(:,3), ends2(:,1), ends2(:,2), ends2(:,3), 0);
    pause(.1);
    hold off
end
axis equal
question from:https://stackoverflow.com/questions/66068914/how-do-i-animate-the-rotation-of-a-set-of-vectors-in-matlab

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Actually you don't have to use the hold on/off command. Just define the axes and your view so you can watch the rotation in a comfortable way. I used your code, actually you don't need the initialisation quiver but can directly start with the loop:

figure(1)
xlim([-0.5 1]); ylim([-0.5 1]); zlim([-0.5 1]);
view([45 45 45])
quiver3(starts(1,:), starts(2,:), starts(3,:), ends(1,:), ends(2,:), ends(3,:))
for k = 1:length(t)
    yawDCM = [cos(yaw*t(k)) sin(yaw*t(k)) 0; -sin(yaw*t(k)) cos(yaw*t(k)) 0; 0 0 1*t(k)];
    pitchDCM = [cos(pitch*t(k)) 0 -sin(pitch*t(k)); 0 1*t(k) 0; sin(pitch*t(k)) 0 cos(pitch*t(k));];
    rollDCM = [1*t(k) 0 0; 0 cos(roll*t(k)) sin(roll*t(k)); 0 -sin(roll*t(k)) cos(roll*t(k));];
    Q = yawDCM*pitchDCM*rollDCM;
    ends2 = Q*ends;
    quiver3(starts(:,1), starts(:,2), starts(:,3), ends2(:,1), ends2(:,2), ends2(:,3), 0);
    xlim([-0.5 1]); ylim([-0.5 1]); zlim([-0.5 1]);
    view([45 45 45])
    pause(.1);    
end

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...