Creating a triangle function file

2 views (last 30 days)
Brian
Brian on 14 Jun 2013
I have to do the following, no real idea what to do?
" Create a function file “e83.m” that can be called with variable number of input arguments. It could be 1,2 or 3 arguments. They are the sides in a triangle with a 90 degree angle. If one argument is given, then assume it is the hypotenuse and calculate the other sides by assuming they are of equal length. If two arguments are given, assume the longest is the hypothenuse and calculate the remaining one. If three values are given, check if they represent a proper right angled triangle, and output an error if not. “error(‘values not consistent with right-angled triangle’);” Your m-file should of course cope with error inputs by generating proper errors. Possible errors it should detect are: a) Wrong number of input arguments b) Non-numeric input arguments c) Non-scalar input arguments (matrices for example, hint: length(in) == 1) "
Thanks for any help

Answers (1)

Image Analyst
Image Analyst on 14 Jun 2013
Some partial code that you can build upon:
function output = e83(varargin)
% Test code
% test1(42);
% test1(42, 69);
% test1('42', 69, pi);
fprintf('Number of input arguments = %d\n', nargin)
output = 42; % Initialize
if nargin == 1
n1 = varargin{1}
elseif nargin == 2
[n1, n2] = varargin{:}
elseif nargin == 3
[n1, n2, n3] = varargin{:}
if ~isnumeric(n1)
error('n1 is not numeric')
end
end

Categories

Find more on Numeric Types 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!