Help generating a triangle using arrays, rotating triangle
Show older comments
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.
Answers (4)
Orion
on 6 Nov 2014
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 Comments
Colin
on 6 Nov 2014
Orion
on 6 Nov 2014
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
Youssef Khmou
on 6 Nov 2014
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 Comments
Colin
on 6 Nov 2014
Youssef Khmou
on 6 Nov 2014
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])
Categories
Find more on Interactive Control and Callbacks 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!