Matlab: arrays, matrices, and rotating plots

In summary: I am assuming that the x-coordinate will increase from 0 to 1 and then decrease back to 0, but in actuality, the x-coordinate will increase from 0 to 2 and then decrease back to 0. So, my code is not actually generating a triangle. Can someone please help me out?
  • #1
_N3WTON_
351
3

Homework Statement


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 [itex] 1 \hspace{1 mm} x \hspace{1 mm} n[/itex] array [itex] x [/itex] containing the values of 0 to 2 in steps of 0.1. Then, using (0.1), generate the corresponding array [itex] y [/itex]. This gives you the hypotenuse. Next create a [itex] 2 \hspace{1 mm}x \hspace{1 mm} (n+2) [/itex] array [itex] {Triangle} [/itex] 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 [itex] Y [/itex] containing the values of 0 to 1 in steps of 0.05. Then, using (0.2), generate the corresponding [itex] x [/itex] array. This gives you the hypotenuse. Create a [itex] 1 \hspace{1 mm} x \hspace{1 mm} (n+2) [/itex] array [itex] {Triangle} [/itex] 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 [itex] (x_{c}, y_{c}) [/itex] 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?

Homework Equations


Mass and first moment equations from basic calculus .

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 what's wrong. Thanks and I will keep working to see if I can solve this problem too. :)
Code:
Code:
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:
Physics news on Phys.org
  • #2
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:
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:
  • #3
Update: I just realized I'm doing the entire problem incorrectly, so back to square one :( lol
 
  • #4
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:
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:
  • #5
_N3WTON_ said:
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?
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
Mark44 said:
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.
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:
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
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
Mark44 said:
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.
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
Nevermind, I figured it out, thanks for the help!
 
  • #10
_N3WTON_ said:
Nevermind, I figured it out, thanks for the help!
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
Mark44 said:
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.
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
Mark44 said:
Hopefully you figured out that you need to add the translation amounts to each half of your array of points.
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
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:
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
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:
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
 

Related to Matlab: arrays, matrices, and rotating plots

1. What is the difference between an array and a matrix in Matlab?

Arrays and matrices are both ways of organizing and storing data in Matlab. The main difference is that arrays can have any number of dimensions, while matrices are limited to two dimensions. Arrays are also more flexible, as they can contain different types of data, while matrices can only contain numerical data.

2. How can I create an array or matrix in Matlab?

To create an array, you can use the "array" function and specify the dimensions and data types. To create a matrix, you can use the "matrix" function and specify the number of rows and columns. You can also use the bracket notation to manually create an array or matrix by entering the data elements separated by commas or semicolons.

3. Can I rotate a plot in Matlab?

Yes, you can rotate a plot in Matlab by using the "view" function. This function allows you to change the angle and direction of the plot, giving you a different perspective of the data. You can also use the "rotate" function to interactively rotate a plot.

4. How do I access and manipulate elements in an array or matrix?

You can access and manipulate elements in an array or matrix by using their indices. Indices start at 1, and you can use a combination of numbers and colons to specify ranges of elements. You can then use various functions like "reshape" or "transpose" to manipulate the data in the array or matrix.

5. Can I perform calculations on arrays and matrices in Matlab?

Yes, Matlab has built-in functions for performing various mathematical operations on arrays and matrices. These include element-wise operations like addition, subtraction, multiplication, and division, as well as matrix operations like matrix multiplication and inversion. You can also use loops and conditional statements to perform more complex calculations on arrays and matrices.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
1
Views
932
  • Engineering and Comp Sci Homework Help
Replies
6
Views
916
  • Engineering and Comp Sci Homework Help
Replies
1
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
870
  • Engineering and Comp Sci Homework Help
Replies
10
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
17
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
994
  • Engineering and Comp Sci Homework Help
Replies
4
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
927
Back
Top