I really need help with inputting the code for these exercises in MATAB. CONSIDE
ID: 2981645 • Letter: I
Question
I really need help with inputting the code for these exercises in MATAB.
CONSIDER A TRIANGLE WHOSE VERTICES ARE (-0.5, -1), (0, 1), (0.5, -1) and (-0.5, -1)
Instructions: For each of the following exercise, create an M-le to store the MATLAB commands.
Copy and paste the M-le into a text document. For problems 1 and 2, include in the text document
the pictures produced by MATLAB. Resize and crop the pictures so that they do not take up too much
space. If the question requires written answers, include them in the text le in the appropriate location.
Make sure you clearly label and separate all the Exercises. For problems 3 and 4 you do not need to
include the picture.
1. Consider the original triangle T. Reect the triangle about the line at 45? and plot the reection
together with the original triangle. Add a grid, a legend and title.
2. Consider the original triangle T. First rotate the triangle 45?
counterclockwise and then reect the
triangle about the line at 45?
. Plot the resulting triangle
EXAMPLE 5
The goal of this example is to draw the triangle in its original size, then cause it to disappear, and nally
to redraw it as it looks after it is dilated by a factor of 1:25. If this operation is repeated ten times in
succession, the triangle will appear to be expanding or moving forward.
To do this we need to rst plot the triangle and store its handle in p:
p = plot(T(1,:),T(2,:))
After we transform the matrix by setting T = D*T we can erase the original gure and draw the new
gure with the command
set(p,'xdata',T(1,:),'ydata',T(2,:));
The following are the commands that produce the eects. Enter them in MATLAB (you need not enter
the comments that follow the % signs). The pause(0.1) command is used to adjust the speed of the
plots so that we can see the graphs before they are erase. You might need to change the 0:1 to suit the
speed of your machine.
clf % clear all settings for the plot
T = [-0.5,0,0.5,-0.5;-1,1,-1,-1];
D = 1.25*eye(2);
p = plot(T(1,:),T(2,:)); % plot the triangle
axis([-10,10,-10,10]) % set size of the graph
axis square % make the display square
figure(gcf) % display graphics window
% Adjust the windows on your screen so that both the command window
% and the graphics window show
hold on % hold the current graph
for i = 1:10
T = D*T; % transform the figure
set(p,'xdata',T(1,:),'ydata',T(2,:)); % erase original figure and plot
% the transformed figure
pause(0.1) % adjust this pause rate to suit your computer.
end
hold off
3. Adapt the procedure developed in Example 5 to rotate the triangle couunterclockwise by increments
of =20 about the origin. Stop when the triangle is in its original location and then rotate it in
the clockwise direction until the triangle is in its original location again. If the pause command is
set properly, it should appear that the triangle is moving around a circle. You may want to rescale
the axis.
4. Adapt the procedure developed in Example 5 to show the triangle rotating in a counterclockwise
direction about the origin by increments of =20 (for a total angle of =2) and expanding at the
same time by a factor of 1:25, then stopping and rotating in the clockwise direction as it shrinks
to its original size (with a contraction factor of :8). At the end of the program, the gure should
have returned to its original size and original location.
Explanation / Answer
QUESTION 1)
T = [-0.5, 0, 0.5, -0.5; -1, 1, -1, -1];
plot(T(1,:),T(2,:),'linewidth',2)
hold on
R = [0, 1; 1, 0];
RT = R*T; %Reflect Triangle
plot(RT(1,:),RT(2,:),'-r','linewidth',2)
title('Reflect Triangle')
legend('Original triangle','Reflect triangle')
grid on
axis equal
hold off
QUESTION 2)
>> T = [-0.5, 0, 0.5, -0.5; -1, 1, -1, -1];
>> plot(T(1,:),T(2,:),'linewidth',2)
>> hold on
>> R = [0, 1; 1, 0];
>> Q = [cos(pi/4), -sin(pi/4); sin(pi/4), cos(pi/4)];
>> RQT = R*Q*T;
>> plot(RQT(1,:),RQT(2,:),'-r','linewidth',2)
>> hold off
QUESTION 3)
clf % clear all settings for the plot
T = [-0.5,0,0.5,-0.5;-1,1,-1,-1];
Q = [cos(pi/20), -sin(pi/20); sin(pi/20), cos(pi/20)];
QT = Q*T;
p = plot(QT(1,:),QT(2,:)); % plot the triangle
axis([-2,2,-2,2]) % set size of the graph
axis square % make the display square
figure(gcf) % display graphics window
% Adjust the windows on your screen so that both the command window
% and the graphics window show
hold on % hold the current graph
for i = 1:40
T = Q*T; % transform the figure
set(p,'xdata',T(1,:),'ydata',T(2,:)); % erase original figure and plot
pause(0.1)
end
for i = 1:40
T = Q'*T; % transform the figure
set(p,'xdata',T(1,:),'ydata',T(2,:));
pause(0.1) % adjust this pause rate to suit your computer.
end
QUESTION 4)
clf % clear all settings for the plot
T = [-0.5,0,0.5,-0.5;-1,1,-1,-1];
D = 1.25*eye(2);
D2 = 0.8*eye(2);
Q = [cos(pi/20), -sin(pi/20); sin(pi/20), cos(pi/20)];
QT = Q*T;
p = plot(QT(1,:),QT(2,:)); % plot the triangle
axis([-10,10,-10,10]) % set size of the graph
axis square % make the display square
figure(gcf) % display graphics window
% Adjust the windows on your screen so that both the command window
% and the graphics window show
hold on % hold the current graph
for i = 1:10
T = D*Q*T; % transform the figure
set(p,'xdata',T(1,:),'ydata',T(2,:)); % erase original figure and plot
% the transformed figure
pause(0.5) % adjust this pause rate to suit your computer.
end
for i = 1:10
T = D2*Q'*T; % transform the figure
set(p,'xdata',T(1,:),'ydata',T(2,:));
pause(0.5) % adjust this pause rate to suit your computer.
end
hold off