unable to rotate the plot
조회 수: 2 (최근 30일)
이전 댓글 표시
% Rotate the axes
theta = 3*pi/4; % Set theta.
fprintf('Rotate the axes...\n');
[x_mat_rotated, y_mat_rotated] = rotation(x_mat, y_mat, theta);
plot_function(x_mat_rotated, y_mat_rotated);
fprintf('Finished.');
The above is from my hw mfile, which i should add extra code in another mfile to run it,
% Write the code here
% Hint: you can use matrix product to solve the new coordinate for every
% point.
R = [cos(theta), sin(theta); -sin(theta), cos(theta)];
B = [x_mat ; y_mat]
A = R*B
x_mat_new = A(1,:)
y_mat_new = A(2,:)
end
%-------------------------------------------------------
I write the above code, and it results in a wrong graph, how can i fix it? The figure should be a rotated parabola
the wrong figure that i generate

댓글 수: 1
DGM
2025년 3월 10일
It's hard to troubleshoot code that's not there, but this much works.
% some fake data
x_mat = linspace(-2,2,100);
y_mat = x_mat.^2;
% transform the data
theta = pi/4; % Set theta.
[x_mat_rotated, y_mat_rotated] = rotation(x_mat, y_mat, theta);
% plot both
plot(x_mat,y_mat,':'); hold on; grid on
plot(x_mat_rotated, y_mat_rotated);
axis equal
function [x_mat_new, y_mat_new] = rotation(x_mat,y_mat,theta)
R = [cos(theta), sin(theta); -sin(theta), cos(theta)];
B = [x_mat; y_mat];
A = R*B;
x_mat_new = A(1,:);
y_mat_new = A(2,:);
end
Depending on your frame of reference, you can swap the signs on the sin() terms to change the rotation direction.
답변 (1개)
Diego Caro
2025년 3월 10일
Are you sure you're plotting the right thing? Look at this other example using your code.
y = 0:0.1:10;
x = y;
theta = pi/2; % Changed theta to visualize better rotation
R = [cos(theta), sin(theta); -sin(theta), cos(theta)];
B = [x; y];
A = R*B;
figure
hold on
plot(x,y)
plot(A(1,:),A(2,:))
legend('Original line','Rotated line')
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Logical에 대해 자세히 알아보기
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!