Creating a triangle function file
조회 수: 4 (최근 30일)
이전 댓글 표시
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
댓글 수: 0
답변 (1개)
Image Analyst
2013년 6월 14일
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
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Operators and Elementary Operations에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!