Angles from different triangles calculated simultaneously - Matrix Dimension Problem and Storage of values
2 views (last 30 days)
Show older comments
Hello, I am trying to obtain 100 different triangles and calculated their respective angles, as seen the variable "x" contains the range. However, I am having troubles due to matrix dimensions and storage of the angles calculate.
clear
clc
x = -0.06:0.001:-0.012;
%x = -0.012;
Counter = 0:1:100;
while true
Counter = Counter + 1;
% Choose any 3 points of the rectangle
p1 = [-0.06541 0.2458];
p2 = [x 0];
p3 = [0 0];
% Make vectors along each edge of the triangle
v12 = p2-p1;
v13 = p3-p1;
v23 = p3-p2;
% Normalise those vectors
u12 = v12/norm(v12);
u13 = v13/norm(v13);
u23 = v23/norm(v23);
% Compute angles as the acos of their dot products
angle1 = acos(dot(u12, u13, 2));
angle2 = acos(dot(u23, -u12, 2));
angle3 = pi - angle1 - angle2;
% Transforming radians to Degrees
angle_d1 = angle1*180./pi;
angle_d2 = angle2*180./pi;
angle_d3 = angle3*180./pi;
% Display
%figure, plot(p1(1),p1(2),'b.',p2(1),p2(2),'g.',p3(1),p3(2),'m.'), axis equal, hold on
% text(p1(1),p1(2),sprintf('Pt1 (%d deg)',round(angle1/pi*180)))
% text(p2(1),p2(2),sprintf('Pt2 (%d deg)',round(angle2/pi*180)))
%text(p3(1),p3(2),sprintf('Pt3 (%d deg)',round(angle3/pi*180)))
break
end
4 Comments
Jan
on 14 Apr 2021
I still do not understand, what you want to achieve. (By the way, no need for apologies: This forum is thought for Matlab questions and of course it is the nature of problems that some details are not clear yet :-) )
What does this mean: "obtain 100 different triangles and calculated their respective angles"?
I suggest to start explaining, what your inputs are. Then mention, how you want to calculate which output. A small working example can help. The code you have tried is useful also.
Instead of "it appears an error of Matrix Dimensions" or "it was a problem of not being possible to use vectors", post a copy of the complete error message, because this can contain important details.
What is the purpose of this code:
Angles(i) = acos((diff(data1) * diff(data2).') / ...
(sqrt(sum(diff(data1).^2, 2)) * sqrt(sum(diff(data2).^2, 2))))
diff(data1) * diff(data2).' is a matrix. Then do you really mean the matrix division / or the elementwise division ./ ? In both cases, the result is not a scalar, so you cannot assign it to Angles(i). But what is correct? Do you expect the output to be a scalar or do you want to store a list of arrays in Angles?
The code of the question contains:
Counter = 0:1:100;
while true
Counter = Counter + 1;
...
break
end
Because the loop is stopped in the firt iteration, you can omit the WHILE/END. The variable Counter is not used anywhere, so what is its purpose? Do you really want to create a vector and increment all of its elements by 1?
Accepted Answer
Jan
on 15 Apr 2021
What about moving the code to display one triangle into a function?
%First Triangle Second Triangle
ShowTri([-0.06541 0.2458; ...
-0.06 0; ...
0, 0]);
ShowTri([-0.06541 0.2458; ...
-0.012 0; ...
0, 0]);
function ShowTri(Tri)
% Making vectors along each edge of the triangle
v = Tri([2,3,3], :) - Tri([1,1,2], :);
% Vector Normalization
u = v ./ vecnorm(v, 2);
% Computing angles as the acos of their dot products
angleD(1) = acosd(dot(u(1, :), u(2, :), 2));
angleD(2) = acosd(dot(u(3, :), -u(1, :), 2));
angleD(3) = 180 - angleD(1) - angleD(2);
%Display
FigH = figure();
AxesH = axes(FigH, 'NextPlot', 'add'); % As: hold('on')
Style = {'b.', 'g.', 'm.'};
for k = 1:3
plot(Tri(k, 1), Tri(k, 2), Style{k});
text(Tri(k, 1), Tri(k, 2), sprintf('Pt%d (%d deg)', k, round(angleD(k))));
end
axis equal
end
More Answers (1)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!