Why doesn't this code rotate and transform the spiral?

15 views (last 30 days)
clear
clc
clf
t = linspace( 0,4*pi,1000)
r=@(t) sqrt(t)
x= r(t).*cos(t)
y= r(t).*sin(t)
plot(x,y)
%turns x and y into a 3xn matrix
pts=[x;y;ones(1,length(x))]
%create matrix
mTrans=eye(3,3);
%declare translation variables
dx=5;
dy=1.5;
%apply to matrix
mTrans(1,3)=dx;
mTrans(2,3)=dy;
%create matrix
mRot=eye(3,3);
%define rotation angle
theta=pi/6;
%change matrix
mRot(1,1)=cos(theta);
mRot(1,2)=sin(theta);
mRot(2,1)=-sin(theta);
mRot(2,2)=cos(theta);
%new points after tranformation and rotation
ptsNew=mTrans*mRot*pts
hold on
plot(pts)

Accepted Answer

Stephan
Stephan on 3 Jun 2019
Edited: Stephan on 3 Jun 2019
No for loop is needed:
t = linspace( 0,4*pi,1000);
r=@(t) sqrt(t);
x = r(t).*cos(t);
y = r(t).*sin(t);
plot(x,y)
%turns x and y into a 3xn matrix
pts=[x;y;ones(1,length(x))];
%create matrix
mTrans=eye(3);
%declare translation variables
dx=5;
dy=1.5;
%apply to matrix
mTrans(1,3)=dx;
mTrans(2,3)=dy;
%define rotation angle
theta=pi;
%rotation matrix
mRot = [cos(theta), -sin(theta), 0; sin(theta), cos(theta), 0; 0, 0, 1];
%new points after tranformation and rotation
ptsNew=mTrans*mRot*pts;
hold on
plot(ptsNew(1,:),ptsNew(2,:))
hold off

More Answers (1)

darova
darova on 3 Jun 2019
Because you have to mulptiply each set of point separately
ptsNew = zeros(size(pts));
for i = 1:1000
ptsNew(:,i) = mTrans*mRot*pts(:,i);
end
hold on
plot(ptsNew(1,:),ptsNew(2,:))
hold off

Categories

Find more on General Physics in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!