unable to rotate the plot
8 次查看(过去 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
the wrong figure that i generate1 个评论
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!

