Help generating a triangle using arrays, rotating triangle

23 次查看(过去 30 天)
The assignment asks me to generate a triangle: Triangle dimensions:
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
generate a 1 x 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. I am then asked to generate a 2 x (n + 2) array 'Triangle' that contains the points necessary to plot the triangle, with x-coordinates in the first row and y-coordinates in the second row. There is more to the problem, but I am having trouble with this part and I believe once I get this down I will be able to complete the rest of the assignment (also I can't go on unless I have the first part completed). However, I have never had linear algebra before so I am a bit rusty when it comes to matrices and arrays, this is the code I have thus far:
clc
clear
line = ['r', 'g', 'k', 'b']; %Line colors
x = linspace(0,2,0.1);
y = linspace(0,1,0.1);
Triangle = [x; y]
plot(Triangle(1,:), Triangle(2,:), 'bs-', 'LineWidth',2)
axis([0 2 0 2])
axis('square')
axis off
figure(1)
end
However, when I run the code it says I have an empty matrix, 2 by 0. If someone could give me some advice on what to do here and why my code isn't working I would greatly appreciate it. Thanks.

回答(4 个)

Orion
Orion 2014-11-6
You misused linspace.
x = linspace(0,2,0.1);
will return an empty matrix, same for y. and then you concatenate two empty matrix., so you create a 2-by-0 Empty matrix
see the doc of linspace :
y = linspace(a,b,n) generates a row vector y of n points linearly spaced between and including a and b. For n = 1, linspace returns b.
in your code you want to create a vector starting at 0, ending at 2 and incrementing of 0.1 :
x = 0:0.1:2;
and for y
y = 1-0.5*x;
so your code should be more like :
clc
clear
line = ['r', 'g', 'k', 'b']; %Line colors
x = 0:0.1:2;
y = 1-0.5*x;
plot(x,y, 'bs-', 'LineWidth',2)
axis([0 2 0 2])
axis('square')
axis off
  3 个评论
Orion
Orion 2014-11-6
maybe this
clc
clear
line = ['r', 'g', 'k', 'b']; %Line colors
x = 0:0.1:2;
y = 1-0.5*x;
plot(x,y, 'bs-', 'LineWidth',2)
axis([0 2 0 2])
axis('square')
axis off;
hold on;
% plot the x axis.
plot(x,zeros(size(x)), 'bs-', 'LineWidth',2);
% plot the y axis
plot(zeros(size(x)),y, 'bs-', 'LineWidth',2);
In this two extra plots, i used the same number of elements as in x.
Then, it's up to you to draw the number of point you need/want
Colin
Colin 2014-11-6
编辑:Colin 2014-11-6
Thanks again, I have one last question: the code you just posted seems to me like it may be hard to rotate, since ultimately this assignment requires me to rotate the triangle. Therefore, I was thinking it may be easier just to do something like:
if
clc
clear
line = ['r', 'g', 'k', 'b']; %Line colors
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)
end
end
Would you agree?

请先登录,再进行评论。


Youssef  Khmou
Youssef Khmou 2014-11-6
Colin, you can try using the function line as the following :
x1=[0 2];
y1=[0 0];
x2=[0 0];
y2=[0 1];
x3=[0 2];
y3=[1 0];
figure;
line(x1,y1); hold on
line(x2,y2);
line(x3,y3);
hold off
axis([-2 2 -2 2])
  3 个评论
Youssef  Khmou
Youssef Khmou 2014-11-6
you mean you need to get a couple (x,y) that gives an array? it is possible to concatenate the answer above :
X=[0 2 0 0 0 2];
Y=[0 0 0 1 1 0];
figure; plot(X,Y)
axis([-2 2 -2 2])
Colin
Colin 2014-11-6
编辑:Colin 2014-11-6
I am supposed to generate a 1xn array 'x' containing the values of x=0 to 2 in steps of 0.1. Then I am to generate another array 'y' using the equation for the hypotenuse. They say that these two arrays will give me the hypotenuse of my triangle. Then, I am to generate a 2x(n+2) array called 'Triangle' that contains the other points necessary to plot the triangle. After I do all of that I am to find the centroid (which won't be a problem) and rotate the triangle in a full circle. Ultimately the output should look something like what this guy has done: http://www.youtube.com/watch?v=OAMU0kT7fBc

请先登录,再进行评论。


Colin
Colin 2014-11-6
编辑:Colin 2014-11-6
This is my updated code:
clc
clear
line = ['r', 'g', 'k', 'b']; %Line colors
x = 0:0.1:2;
y = 1-0.5*x;
Triangle = [0 2 0 x 0 0; 0 0 1 y 0 1]
plot(Triangle(1,:),Triangle(2,:), 'bs-','LineWidth',2)
axis([0 2 0 2])
axis('square')
axis off
figure(1)
i
figure(2)
%Triangle is assumed to be 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
end
end

Orion
Orion 2014-11-6
I haven't done things like that for a long time. always fun.
see the attached file and adapt it.
  3 个评论
Colin
Colin 2014-11-6
Awesome, here is my code (I got the full rotation down)
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)
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
end

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 2-D and 3-D Plots 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by