unable to rotate the plot
2 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
% 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 Kommentar
DGM
am 10 Mär. 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.
Antworten (1)
Diego Caro
am 10 Mär. 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 Kommentare
Siehe auch
Kategorien
Mehr zu Logical finden Sie in Help Center und File Exchange
Produkte
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!