Matlab area of triangle question

30 ビュー (過去 30 日間)
bob saget
bob saget 2016 年 11 月 27 日
コメント済み: Image Analyst 2025 年 12 月 10 日 3:49
I'm a newbie to Matlab and have been struggling on this question.
Write a user-defined MATLAB function that determines the area of a triangle when the lengths of the sides are given. For the function name and arguments use [area] = triangle (a, b, c). Of triangle with the following sides:
  • a. a = 10, b = 15, c = 7.
  • b. a = 6, b = 8, c = 10.
  • c. a = 200, b = 75, c = 250.
What I have written but is probably entirely incorrect.
a = [10 6 200];
b = [15 8 75];
c = [7 10 250];
[area] = triangle(a, b, c)
s = (a+b+c)/2;
area = sqrt(s*(s-a)*(s-b)*(s-c));
disp(area);
A line to check if the value's will work in the Heron's formula is not necessary.

採用された回答

David Goodmanson
David Goodmanson 2016 年 11 月 27 日
編集済み: David Goodmanson 2016 年 11 月 27 日
Hello Bob, The good thing is that you are doing this with vectors of values rather than using for loops. Vectorized code is generally better and easier. Suppose you temporarily don't make a function yet, and put a % in front of the [area] = .... line so as to comment it out. The problem right now is that the multiplications you want to do are term-by-term, i.e. the first entry of s times the first entry of (s-a), etc. For that you use .* :
area = sqrt(s.*(s-a).*(s-b).*(s-c));
and the lines of code run as is. For the function, create and save an mfile like
function [area] = triangle(a,b,c)
% do stuff
end
After that you can define a,b,c and use
x = triangle(a,b,c)
where x is any convenient variable name.
  1 件のコメント
bob saget
bob saget 2016 年 11 月 27 日
Thank you! It worked. Posting my corrected answer for anybody coming from Google or anywhere else.
a = [10 6 200];
b = [15 8 75];
c = [7 10 250];
%[area] = triangle(a, b, c);
s = (a+b+c)./2;
area = sqrt(s.*(s-a).*(s-b).*(s-c));
fprintf('The areas in order are %.2f\n', area);

サインインしてコメントする。

その他の回答 (3 件)

Image Analyst
Image Analyst 2016 年 11 月 27 日
編集済み: Image Analyst 2016 年 11 月 27 日
I think you're getting all those a, b, and c's confused. It helps to rename them to something descriptive:
function test_triangle_area_function
clc; % Clear the command window.
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
aSides = [10 6 200];
bSides = [15 8 75];
cSides = [7 10 250];
for tri = 1 : length(aSides)
area = triangle(aSides(tri), bSides(tri), cSides(tri));
fprintf('Triangle %d: a = %d, b = %d, c = %d, area = %f\n', ...
tri, aSides(tri), bSides(tri), cSides(tri), area);
end
function area = triangle(a, b, c)
s = (a+b+c)/2;
area = sqrt(s*(s-a)*(s-b)*(s-c));

Tefo Sheila
Tefo Sheila 2024 年 3 月 26 日
The area of a triangle with sides having the lengths a, b and c is given by the formula A=√(s(s-a)(s-b)(s-a)) where s=(a+b+c)/2. Use MATLAB to a script program called AreaTriScript that calculate the area of triangle having the lengths a,b and c.The program allows user to enter tye values a,b and c through the keyboard using input ( ) command.include the title and comments in your program
Use the program to calculate the area of triangle with sides of lengths (i) a=5cm,b=12cm,c=13cm (ii) a=8cm,b=3cm,c=9cm.Compare your answer with that obtained using the formula A=½ x base x height.
  1 件のコメント
Image Analyst
Image Analyst 2024 年 3 月 26 日
@Tefo Sheila this does not answer @bob saget's question. This looks like your homework question that uses slightly different side length values. I gave a full solution in my answer above.
Since this looks like your homework problem, if you have any questions after reviewing my Answer then ask your instructor or read the link below to get started:
and start your own question with your attempt at code.

サインインしてコメントする。


Khalid
Khalid 2025 年 12 月 9 日 21:23

Write a program that takes two sides of a stationary triangle and shows the area and shape of this triangle

  1 件のコメント
Image Analyst
Image Analyst 2025 年 12 月 10 日 3:49
Try patch and fill and inputdlg

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeStartup and Shutdown についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by