Advertisement
The_General

Untitled

Oct 27th, 2013
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function [ fcos ] = cosine( x, n )
  2. %Cosine Provides the cosine (cos x) of the input
  3. %   cosine(x) takes a matrices of values (in radians) and outputs a
  4. %   matrices containing an approximate cosine of each value in the matrix.
  5. %
  6. %   cosine(x) takes a matrices 'x' and returns the cosines. Tbe default
  7. %   number of values in the series is 10.
  8. %   cosine(x) takes a matrices 'x' and 'n', the number of values in the
  9. %   series, and returns a matrices of the cosines of 'x'.
  10. if (nargin < 2)  ||  isempty(n)
  11.         n = 10;
  12. end
  13. validateattributes(x, {'numeric'}, {'finite', 'real', 'finite'});
  14. validateattributes(n, {'numeric'}, {'integer', 'scalar', 'finite', 'positive'});
  15. [columns, rows] = size(x);
  16. fcos = zeros(columns,rows);
  17. for i = 1:numel(x)
  18.     fcos(i) = (sum(((-1).^(0:n)).*(x(i).^(2*(0:n)))./(factorial(2*(0:n)))));
  19. end
  20. if numel(fcos)~=numel(x)
  21.     error('cosine:inputoutputmismatch','The output matrix is smaller than the input matrix')
  22. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement