unable to rotate the plot

5 vues (au cours des 30 derniers jours)
Man Yuk
Man Yuk le 10 Mar 2025
Commenté : DGM le 10 Mar 2025
% 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 parabolathe wrong figure that i generate
  1 commentaire
DGM
DGM le 10 Mar 2025
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.

Connectez-vous pour commenter.

Réponses (1)

Diego Caro
Diego Caro le 10 Mar 2025
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')

Catégories

En savoir plus sur Logical dans Help Center et File Exchange

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by