How do I read these arrays into the function to input more than just one answer?
2 views (last 30 days)
Show older comments
a=500
alpha=[54.80,54.06,53.34]
beta=[65.59,64.59,63.62]
x=a*(tan(beta)/(tan(beta)-tan(alpha)))
It only outputs the last answer with the last set of angles and I need it at all three angles.
0 Comments
Accepted Answer
John BG
on 13 Feb 2017
Edited: John BG
on 13 Feb 2017
Sean
a start point could be
function x=f1(a,alpha,beta)
x=a*(tand(beta)./(tand(beta)-tand(alpha)))
end
call example
a=500
alpha=[54.80,54.06,53.34]
beta=[65.59,64.59,63.62]
x=f1(a,alpha,beta)
tan is for rad angles, tand for degree angles.
any function, to be robust, has to be able to block non coherent inputs, like chars instead of numbers, or alpha and beta with different lengths, or if you only want it to work on reals, then remove the imaginary parts.
many functions also usually include a few comment lines right below the function header, that show up when keying in 'help function_name' in MATLAB Command Window.
there are commands like
nargin
nargout
arginchk
argoutchk
varargin
varagout
to help building input output checks
the function, including a few input checks would be
function [x,err_code]x=f1(a,alpha,beta)
arginchk(3,3); % only 3 inputs
argoutchk(1,1) % only 1 output
err_list={'err message 1';'err_message 2'}
err_code=0;
if numel(a)>1
err_code=1;
disp('err_list{1}')
break;
end
% stretch angles to avoid possible size mismatch
alpha=alpha(:);beta=beta(:);
if numel(alpha)!=numel(beta)
err_code=2;
disp(err_code_list{err_code})
break;
end
x=a*(tand(beta)./(tand(beta)-tand(alpha)))
end
if you find this answer useful would you please be so kind to mark my answer as Accepted Answer?
To any other reader, please if you find this answer of any help solving your question,
please click on the thumbs-up vote link,
thanks in advance
John BG
1 Comment
John BG
on 13 Feb 2017
Edited: John BG
on 13 Feb 2017
Perhaps you may want to consider the alternative expression to
x=tand(beta)./(tand(beta)-tand(alpha))
being
x=1-.5*sind(alpha+beta)./sin(alpha-beta)
because tangent gets to Inf and -Inf periodically, MATLAB may throw less warnings for possible inaccurate calculations due to too close to Inf
More Answers (1)
Image Analyst
on 13 Feb 2017
You used slash for divide, when you should have used dot slash
x = a*(tan(beta) ./ (tan(beta)-tan(alpha)))
to do an element-by-element divide for every element in the arrays.
0 Comments
See Also
Categories
Find more on Creating and Concatenating Matrices 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!