Write a MATLAB script M-file named TriangleType that will load a file of triangl
ID: 3758459 • Letter: W
Question
Write a MATLAB script M-file named TriangleType that will load a file of triangles, and use the function from Part 1 to determine if the triangles are acute or obtuse. A message should be printed to the screen for each triangle.
The triangles will be contained in a text file named Triangles.txt. This file will have three columns, one for each side. Every row in the file will correspond to a triangle. The triangles will be either acute or obtuse—there will be no right triangles, nor any non triangles.
Example:
Given a text file with this data:
The script file should produce the following output to the Command Window:
Code Requirements:
The script file must call (use) the function IsAcuteTriangle to determine the type of triangle. No credit will be given if this is done any other way.
The script file must load and use a text file named Triangle.txt with data formatted as specified above.
Helpful hints
Remember to suppress output in both the function & script files.
Create your own Triangles.txt file test your code.
Explanation / Answer
Please find the code which finds if the triangle is obtuse or acute based on the side length.
So far as I know, to determone if a triangle is acute or obtuse (or
even to see if it is a valid triangle) we had better use the law of
cosine. As, c^2 = a^2 + b^2 - 2 bc cos C.
So cos C = (c^2 - a^2 - b^2) / (2bc)
When c^2 < a^2 + b^2 , angle C is acute.
When c^2 > a^2 + b^2 , angle C is obtuse.
Also, the only possible obtuse angle should be the angle opposite to
the longest side.
For a valid triangle, we should have a+b > c, b+c >a and c+a > b
(1) Let (a,b,c)= (5,8,9).
At first , check 5+8 > 9 ,OK. It is a legal triangle.
(No need to check others as comparing 5+9 and 8, why?)
c= 9 is the longest. check angle C Since 5^2 + 8^2 = 25 + 64 = 89 > 9^2 ,so angle C is acute)
Hence, this is an acute triangle.
(2) I suppose, you typed as:
(a,b,c)= (24,8,35)
Note 24+8 < 35, illegal triangle.
If I change it to (a,b,c)= (24,6,35)
c= 35 is the longest. check angle C
Since 24^2 + 6^2 = 576 + 36 = 612 < 35^2 = 1225 ,so angle C is obtuse)
Hence, this is an obtuse triangle.
Matlab Code:-------------------------
sa=input('Enter a:')
sb=input('Enter b:')
sc=input('Enter c:')
if (sa+sb)<sc
if (sc^2)<((sa^2)+(sb^2))
fprintf('Triangle is Acute')
else
fprintf('Triangle is Obtuse')
end
elseif (sa+sc)<sb
if (sb^2)<((sa^2)+(sc^2))
fprintf('Triangle is Acute')
else
fprintf('Triangle is Obtuse')
end
elseif (sb+sc)<sa
if (sa^2)<((sb^2)+(sc^2))
fprintf('Triangle is Acute')
else
fprintf('Triangle is Obtuse')
end
end
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.