unable to rotate the plot
2 views (last 30 days)
Show older comments
% 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 Comment
DGM
on 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.
Answers (1)
Diego Caro
on 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')
0 Comments
See Also
Categories
Find more on Logical in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!