SSS Triangle functions, need output help

2 ビュー (過去 30 日間)
Kyle
Kyle 2011 年 4 月 22 日
回答済み: Aqsa Saleem 2021 年 6 月 1 日
My function is outputting ang1, ang2, and ang3 in addition to my fprintf statements, why? and how to i fix it? Question asks for my to calculate it the triangle is valid by checking the area.
function [ang1 ang2 ang3 ] = triangle(s1,s2,s3)
%TRIANGLE calculates the degrees of all angles given the length of all
%sides.;
s1=input('Please input side 1: ');
s2=input('Please input side 2: ');
s3=input('Please input side 3: ');
s=(s1+s2+s3)/(2);
Area=sqrt(s*(s-s1)*(s-s2)*(s-s3));
if Area > 0
ang1=acosd((s2^2 + s3^2 - s1^2)/(2*s2*s3));
ang2=acosd((s3^2 + s1^2 - s2^2)/(2*s3*s1));
ang3=acosd((s1^2 + s2^2 - s3^2)/(2*s1*s2));
fprintf('The angle opposite side 1 is %.2f deg\n',ang1);
fprintf('The angle opposite side 2 is %.2f deg\n',ang2);
fprintf('The angle opposite side 3 is %.2f deg\n',ang3);
elseif Area >= 0
fprintf('The 3 sides do NOT form an open triangle!\n');
end
end
I'm also having a problem with triangle inputs (1,2,3), it says the correct thing but outputs an error with it
Error in ==> triangle at 5
s1=input('Please input side 1: ');
??? Output argument "ang1" (and maybe others) not
assigned during call to "P:\triangle.m>triangle".
do the input statements need to be outside the function?
FINAL SOLUTION:
function [ang1 ang2 ang3]=triangle(s1,s2,s3)
%TRIANGLE calculates the degrees of all angles given the length of all
%sides.;
ang1=acosd((s2^2 + s3^2 - s1^2)/(2*s2*s3));
ang2=acosd((s3^2 + s1^2 - s2^2)/(2*s3*s1));
ang3=acosd((s1^2 + s2^2 - s3^2)/(2*s1*s2));
end
%Test_triangle
s1=input('Please input side 1: ');
s2=input('Please input side 2: ');
s3=input('Please input side 3: ');
s=((s1+s2+s3)/(2));
Area=sqrt(s*(s-s1)*(s-s2)*(s-s3));
if Area > 0
[ang1 ang2 ang3]=triangle(s1,s2,s3);
fprintf('The angle opposite side 1 is %.2f deg.\n',ang1);
fprintf('The angle opposite side 2 is %.2f deg.\n',ang2);
fprintf('The angle opposite side 3 is %.2f deg.\n',ang3);
elseif Area <= 0
fprintf('The 3 sides do NOT form an open triangle!\n');
end

採用された回答

Chirag Gupta
Chirag Gupta 2011 年 4 月 22 日
This is because your function defines them as output arguments
function [ang1 ang2 ang3 ] = triangle(s1,s2,s3)
MATLAB expects 3 outputs as defined by ang1,ang2 and ang3. Thats why you get the error.
If you don't want the outputs, just define the function as:
function triangle(s1,s2,s3)
  2 件のコメント
Chirag Gupta
Chirag Gupta 2011 年 4 月 22 日
similarly if you don't want to supply inputs to the function you don't need to pass them as input args
function triangle()
% ......
s1 = input
etc
Kyle
Kyle 2011 年 4 月 23 日
I found out what was wrong, i miss read the question. I needed to create the function and then a test program to run it. My mistake. Thanks for the help.

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

その他の回答 (2 件)

Walter Roberson
Walter Roberson 2011 年 4 月 22 日
You only define ang1, ang2, and ang3 if Area > 0. What happens if the Area is not greater than 0 ?
  1 件のコメント
Kyle
Kyle 2011 年 4 月 22 日
Oh,typo.
Area <=0
fprintf('The 3 sides do NOT form an open triangle!\n');
But i dont understand how i can define the equations for ang1 ang 2 and ang3 without causing it to output them separate from the fprintf statements.

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


Aqsa Saleem
Aqsa Saleem 2021 年 6 月 1 日
given triangle with sides a,b,c write a program to find whether it is an isosceles triangle in MATLAB programing

カテゴリ

Help Center および File ExchangeCreating and Concatenating Matrices についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by