# Homework Help: Matlab: arrays, matrices, and rotating plots

1. Nov 5, 2014

### _N3WTON_

1. The problem statement, all variables and given/known data
Generate a triangle. For this problem, generate a triangle at a grid of points that are finely spaced in the x dimension. The triangle is defined as follows:
-Side 1: y = 0 for x = 0 to 2
-Side 2: x = 0 for y = 0 to 1
-Hypotenuse: y = 1-0.5x for x = 0 to 2
Alternatively, the triangle can be defined as:
-Side 1: y = 0 for x = 0 to 2
-Side 2: x = 0 for y = 0 to 1
-Hypotenuse: x = 2 - 2y for y = 0 to 1
You will need both definitions to successfully complete this assignment.
3.1
Generate a $1 \hspace{1 mm} x \hspace{1 mm} n$ array $x$ containing the values of 0 to 2 in steps of 0.1. Then, using (0.1), generate the corresponding array $y$. This gives you the hypotenuse. Next create a $2 \hspace{1 mm}x \hspace{1 mm} (n+2)$ array ${Triangle}$ that contains the points necessary to plot the triangle, with the x-coordinates in the first row and the y-coordinates in the second row.
3.2
Plot your triangle to show that you have defined things correctly.
3.3
Compute the centroid in the x-direction
3.4
Generate an array $Y$ containing the values of 0 to 1 in steps of 0.05. Then, using (0.2), generate the corresponding $x$ array. This gives you the hypotenuse. Create a $1 \hspace{1 mm} x \hspace{1 mm} (n+2)$ array ${Triangle}$ that contains the points necessary to plot the triangle, with the x-coordinates in the first row and the y-coordinates in the second row.
3.5
Plot the triangle to show that you have defined things correctly and show that the triangle overlaps the triangle given in 3.2
3.6
Compute the centroid in the y-direction
3.7
Using the triangle generated in either of the previous sections, rotate the triangle around a full circle. At each step of the rotation, plot the resultant triangle.
3.8
Translate every point in the triangle by the coordinates $(x_{c}, y_{c})$ prior to rotation. Then rotate the offset triangle in the same manner as in 3.1. Is the rotated triangle the same as or different from the triangle in 3.2. If different, what is the difference?
2. Relevant equations
Mass and first moment equations from basic calculus .

3. The attempt at a solution
Hello all, currently, I am having some trouble getting my first triangle plot correct, I will post my code. The code is running fine, but it is just generating a line rather than a triangle. I am hoping that once I sort out the first part of the problem and find out what I am doing wrong the rest will come easy (wishful thinking perhaps). I was hoping someone could help me debug my code and let me know whats wrong. Thanks and I will keep working to see if I can solve this problem too. :)
Code:
Code (Text):
clc
clear
line = ['r', 'g', 'k', 'b']; %Line colors
Triangle = [0 0.5 1 1.5 2;  %X coordinates
0 0.4 0.6 0.8 1]  %Y coordinates
plot(Triangle(1,*:*, Triangle(2,*:*, 'bs-', 'LineWidth',2);
axis([0 2 0 2]);
axis('square');
axis off
title('Basic Triangle');
figure(1)
Edit: for the "plot" command, I have added ** around my semicolon so that it doesn't appear as an emoticon, the * does not appear in my actual code

Last edited: Nov 5, 2014
2. Nov 5, 2014

### _N3WTON_

Update: I have improved the code somewhat. My plot now resembles a triangle but I am still missing a line segment that connects everything, here is the code:
Code (Text):
clc
clear
line = ['r', 'g', 'k', 'b']; %Line colors
Triangle = [0 2 0; 0 0 1] %Triangle coordinates
plot(Triangle(1,*;*,Triangle(2,*;*,'bs-','LineWidth',2)
axis([0 2 0 2])
axis off
title('Basic Triangle');
figure(1)

Last edited: Nov 5, 2014
3. Nov 5, 2014

### _N3WTON_

Update: I just realized I'm doing the entire problem incorrectly, so back to square one :( lol

4. Nov 5, 2014

### _N3WTON_

Ok, I have finally been able to generate a triangle, but I believe that this is not really the technique that the problem is looking for, if anyone could give me some advice on how to generate the triangle using the above guidelines, I'd greatly appreciate it. Also, I am a little confused about the initial generation the problem is asking for, because an array from 0 to 2 in steps of 0.1 has more elements than an array from 0 to 1 in steps of 0.1, so how are those two arrays compatible?Anyway...here is my updated code:
Code (Text):
clc
clear
line = ['r', 'g', 'k', 'b']; %Line colors
x = linspace(0,2,0.1);
y = linspace(0,1,0.1);
Triangle = [0 2 0 0;0 0 1 0];
plot(Triangle(1,*:, Triangle(2,*:, 'b', 'LineWidth',2)
axis([0 2 0 2])
title('Basic Triangle')
axis('square')
axis off
figure(1)
%Triangle is a planar lamina
f = @(x) 1 - (x/2);
mass = integral(f, 0, 2) %Multiplied by rho, measured in mass per unit area
g = @(x) x.*(1-(x/2));
y_moment = integral(g, 0, 2)
x_centroid = y_moment/mass

Last edited: Nov 5, 2014
5. Nov 6, 2014

### Staff: Mentor

The wording of this problem is confusing to me, possibly because it is incorrect. I believe the intent is that you store the x coordinates in the array named x, and the y coordinates in an array named y. Since the increment along the x-axis is .1, there will be $\frac{2 - 0}{.1} + 1 = 21$ numbers in the x array. The y array should have the same number of points, but this means that the increment is .05, not .1 as it seems to say in the problem description.

The array of points should have 21 x coordinates and 21 y coordinates.

It wouldn't hurt to contact your instructor to get clarification/correction on what the problem is about.

6. Nov 6, 2014

### _N3WTON_

Thank you for the response, I have actually already contacted the professor and he said there is a typo in the problem, or rather, the problem is missing a piece of information. The 0.1 referenced for the y-array is meant to refer to the hypotenuse of the triangle (the hypotenuse was meant to be numbered as equation (0.1) but it was not originally). I hope this is clear. Anyhow, I have been working on this problem all morning and I have most of it complete, I am just struggling with the last part, the translation of the triangle (3.8). Here is my updated code:
Code (Text):
clc
clear
line = ['r', 'g', 'k', 'b']; %Line colors
x = 0:0.1:2;  %X coordinates
y = 1-0.5*x;  %Y coordinates
Triangle = [0 2 0 x 0 0; 0 0 1 y 0 1];  %Triangle x, y coordinates
plot(Triangle(1,:),Triangle(2,:), 'bs-','LineWidth',2) %Plot the triangle
axis([0 2 0 2])
axis('square')
axis off
figure(1)

%Triangle is assumed to be a planar lamina
f = @(x) 1 - (x/2); %Function representing the hypotenuse of the triangle
mass = integral(f, 0, 2); %Multiplied by rho, measured in mass per unit area
g = @(x) x.*(1-(x/2)); %Function to determine the first moment about y
y_moment = integral(g, 0, 2); %Multiplied by rho
x_centroid = y_moment/mass;   %Compute the x coordinate of the centroid
fprintf('The x component of the centroid is %0.2f \n',x_centroid)  %Output centroid

y = 0:0.05:1;   %Y coordinates
x = 2-(2*y);    %X coordinates
Triangle_1 = [0 2 0 x 0 0; 0 0 1 y 0 1];  %Triangle x,y coordinates, same triangle as above
plot(Triangle_1(1,:),Triangle_1(2,:),'bs-','LineWidth',2)  %Plot the triangle
axis([0 2 0 2])
axis('square')
axis off
figure(1)

%Again, triangle is a planar lamina
h = @(x) ((1-(x/2))/2).*(1-(x/2)); %Function to determine first moment about x
x_moment = integral(h,0,2);  %Multiplied by rho
y_centroid = x_moment/mass;  %Compute the y component of the centroid
fprintf('The y component of the centroid is %0.2f \n',y_centroid)  %Output centroid component

%Using the first triangle for rotation
x = 0:0.1:2;  %X coordinates
y = 1-0.5*x;  %Y coordinates
Triangle = [0 2 0 x 0 0; 0 0 1 y 0 1];  %Triangle x, y coordinates
plot(Triangle(1,:),Triangle(2,:), 'bs-','LineWidth',2) %Plot the triangle
axis([0 2 0 2])
axis('square')
axis off
figure(1)
%Setting up the rotation
figure(2) %New window
for k=0:64 %64 different positions
phi = k*(2*pi)/64;  %Angle of rotation, 64 rotations around a full circle (2 pi radians)
A = [cos(phi) -sin(phi); sin(phi) cos(phi)];  %General form of 2-D rotation
TriangleRotated = A*Triangle;   %To rotate the triangle
plot(TriangleRotated(1,:),TriangleRotated(2,:),line(mod(k,4)+1));
%Plot scaling for first iteration of the loop
if k==0
axis([-2 2 -2 2]);
axis('square');
axis off
hold on
end;
figure(2);  %Displays the plot
end %End of the first iteration
hold off;  %Release plot
figure(2); %Display plot
%Translating the triangle

7. Nov 6, 2014

### Staff: Mentor

Translation is pretty straightforward. If xc and yc are the translation amounts, just add xc to each x coordinate in your array, and add yc to each y coordinate in the array. If your array contains 42 numbers, the first 21 of them are x coordinates and the last 21 are y coordinates.

8. Nov 6, 2014

### _N3WTON_

I understand the concept of it, but I'm not entirely sure how to do it in matlab. Would I multiple my x and y arrays by another array?

9. Nov 6, 2014

### _N3WTON_

Nevermind, I figured it out, thanks for the help!

10. Nov 6, 2014

### Staff: Mentor

Hopefully you figured out that you need to add the translation amounts to each half of your array of points. A do loop will work nicely.

11. Nov 6, 2014

### _N3WTON_

Hmm I'm thinking now that maybe I didn't do it correctly, the assignment has been turned in and I'm on campus now but I'll post my code when I get home

12. Nov 6, 2014

### Staff: Mentor

To clarify this a bit, you need to add xc to each element of the first row (the x coordinates)in your Triangle array, and yc to each element in the second row (the y coordinates).

13. Nov 6, 2014

### _N3WTON_

As promised, here is the final bit of code. After reading you last comment I am pretty sure I didn't do the final part correctly. However, it still works pretty well and looks pretty cool (that's really what matters right :P)
Code (Text):
clc
clear
line = ['r', 'g', 'k', 'b']; %Line colors
x = 0:0.1:2;  %X coordinates
y = 1-0.5*x;  %Y coordinates
Triangle = [0 2 0 x 0 0; 0 0 1 y 0 1];  %Triangle x, y coordinates
plot(Triangle(1,:),Triangle(2,:), 'bs-','LineWidth',2) %Plot the triangle
axis([0 2 0 2])
axis('square')
axis off
figure(1)

%Triangle is assumed to be a planar lamina
f = @(x) 1 - (x/2); %Function representing the hypotenuse of the triangle
mass = integral(f, 0, 2); %Multiplied by rho, measured in mass per unit area
g = @(x) x.*(1-(x/2)); %Function to determine the first moment about y
y_moment = integral(g, 0, 2); %Multiplied by rho
x_centroid = y_moment/mass;   %Compute the x coordinate of the centroid
fprintf('The x component of the centroid is %0.2f \n',x_centroid)  %Output centroid

y = 0:0.05:1;   %Y coordinates
x = 2-(2*y);    %X coordinates
Triangle_1 = [0 2 0 x 0 0; 0 0 1 y 0 1];  %Triangle x,y coordinates, same triangle as above
plot(Triangle_1(1,:),Triangle_1(2,:),'bs-','LineWidth',2)  %Plot the triangle
axis([0 2 0 2])
axis('square')
axis off
figure(1)

%Again, triangle is a planar lamina
h = @(x) ((1-(x/2))/2).*(1-(x/2)); %Function to determine first moment about x
x_moment = integral(h,0,2);  %Multiplied by rho
y_centroid = x_moment/mass;  %Compute the y component of the centroid
fprintf('The y component of the centroid is %0.2f \n',y_centroid)  %Output centroid component

%Using the first triangle for rotation
x = 0:0.1:2;  %X coordinates
y = 1-0.5*x;  %Y coordinates
Triangle = [0 2 0 x 0 0; 0 0 1 y 0 1];  %Triangle x, y coordinates
plot(Triangle(1,:),Triangle(2,:), 'bs-','LineWidth',2) %Plot the triangle
axis([0 2 0 2])
axis('square')
axis off
figure(1)
%Setting up the rotation
figure(2) %New window
for k=0:64 %64 different positions
phi = k*(2*pi)/64;  %Angle of rotation, 64 rotations around a full circle (2 pi radians)
A = [cos(phi) -sin(phi); sin(phi) cos(phi)];  %General form of 2-D rotation
TriangleRotated = A*Triangle;   %To rotate the triangle
plot(TriangleRotated(1,:),TriangleRotated(2,:),line(mod(k,4)+1));
%Plot scaling for first iteration of the loop
if k==0
axis([-2 2 -2 2]);
axis('square');
axis off
hold on
end;
figure(2);  %Displays the plot
end %End of the first iteration
hold off;  %Release plot
figure(2); %Display plot
figure(3);
%Translating the triangle by x_c and y_c
x = x + 2;
y = y + 2;
TranslateTriangle = [x 2 2 4 2; y 2 3 2 2]; %Translated triangle
plot(TranslateTriangle(1,:),TranslateTriangle(2,:),'bs-','LineWidth',2)
axis([2 4 2 4])
axis('square')
axis off
figure(3)
%Rotating the translated triangle
figure(4) %New window
for k=0:64 %64 different positions
phi = k*(2*pi)/64;  %Angle of rotation, 64 rotations around a full circle (2 pi radians)
A = [cos(phi) -sin(phi); sin(phi) cos(phi)];  %General form of 2-D rotation
TriangleRotated = A*TranslateTriangle;   %To rotate the triangle
plot(TriangleRotated(1,:),TriangleRotated(2,:),line(mod(k,4)+1));
%Plot scaling for first iteration of the loop
if k==0
axis([-4 4 -4 4]);
axis('square');
axis off
hold on
end;
figure(4);  %Displays the plot
end %End of the first iteration
hold off;  %Release plot
figure(4); %Display plot

14. Nov 6, 2014

### _N3WTON_

As promised, here is the final bit of code. After reading you last comment I am pretty sure I didn't do the final part correctly. However, it still works pretty well and looks pretty cool (that's really what matters right :P)
Code (Text):
clc
clear
line = ['r', 'g', 'k', 'b']; %Line colors
x = 0:0.1:2;  %X coordinates
y = 1-0.5*x;  %Y coordinates
Triangle = [0 2 0 x 0 0; 0 0 1 y 0 1];  %Triangle x, y coordinates
plot(Triangle(1,:),Triangle(2,:), 'bs-','LineWidth',2) %Plot the triangle
axis([0 2 0 2])
axis('square')
axis off
figure(1)

%Triangle is assumed to be a planar lamina
f = @(x) 1 - (x/2); %Function representing the hypotenuse of the triangle
mass = integral(f, 0, 2); %Multiplied by rho, measured in mass per unit area
g = @(x) x.*(1-(x/2)); %Function to determine the first moment about y
y_moment = integral(g, 0, 2); %Multiplied by rho
x_centroid = y_moment/mass;   %Compute the x coordinate of the centroid
fprintf('The x component of the centroid is %0.2f \n',x_centroid)  %Output centroid

y = 0:0.05:1;   %Y coordinates
x = 2-(2*y);    %X coordinates
Triangle_1 = [0 2 0 x 0 0; 0 0 1 y 0 1];  %Triangle x,y coordinates, same triangle as above
plot(Triangle_1(1,:),Triangle_1(2,:),'bs-','LineWidth',2)  %Plot the triangle
axis([0 2 0 2])
axis('square')
axis off
figure(1)

%Again, triangle is a planar lamina
h = @(x) ((1-(x/2))/2).*(1-(x/2)); %Function to determine first moment about x
x_moment = integral(h,0,2);  %Multiplied by rho
y_centroid = x_moment/mass;  %Compute the y component of the centroid
fprintf('The y component of the centroid is %0.2f \n',y_centroid)  %Output centroid component

%Using the first triangle for rotation
x = 0:0.1:2;  %X coordinates
y = 1-0.5*x;  %Y coordinates
Triangle = [0 2 0 x 0 0; 0 0 1 y 0 1];  %Triangle x, y coordinates
plot(Triangle(1,:),Triangle(2,:), 'bs-','LineWidth',2) %Plot the triangle
axis([0 2 0 2])
axis('square')
axis off
figure(1)
%Setting up the rotation
figure(2) %New window
for k=0:64 %64 different positions
phi = k*(2*pi)/64;  %Angle of rotation, 64 rotations around a full circle (2 pi radians)
A = [cos(phi) -sin(phi); sin(phi) cos(phi)];  %General form of 2-D rotation
TriangleRotated = A*Triangle;   %To rotate the triangle
plot(TriangleRotated(1,:),TriangleRotated(2,:),line(mod(k,4)+1));
%Plot scaling for first iteration of the loop
if k==0
axis([-2 2 -2 2]);
axis('square');
axis off
hold on
end;
figure(2);  %Displays the plot
end %End of the first iteration
hold off;  %Release plot
figure(2); %Display plot
figure(3);
%Translating the triangle by x_c and y_c
x = x + 2;
y = y + 2;
TranslateTriangle = [x 2 2 4 2; y 2 3 2 2]; %Translated triangle
plot(TranslateTriangle(1,:),TranslateTriangle(2,:),'bs-','LineWidth',2)
axis([2 4 2 4])
axis('square')
axis off
figure(3)
%Rotating the translated triangle
figure(4) %New window
for k=0:64 %64 different positions
phi = k*(2*pi)/64;  %Angle of rotation, 64 rotations around a full circle (2 pi radians)
A = [cos(phi) -sin(phi); sin(phi) cos(phi)];  %General form of 2-D rotation
TriangleRotated = A*TranslateTriangle;   %To rotate the triangle
plot(TriangleRotated(1,:),TriangleRotated(2,:),line(mod(k,4)+1));
%Plot scaling for first iteration of the loop
if k==0
axis([-4 4 -4 4]);
axis('square');
axis off
hold on
end;
figure(4);  %Displays the plot
end %End of the first iteration
hold off;  %Release plot
figure(4); %Display plot

Share this great discussion with others via Reddit, Google+, Twitter, or Facebook

Have something to add?
Draft saved Draft deleted