MATLAB: What is wrong with this command line

cos_maclaurin

function y = cos_maclaurin(x,n)
%%This function will iterate through the values in the array x and for each
%%x-value it will perform a series summation for the cosine function using
%%n-terms in the series. The function will then output corresponding
%%y-values for each x-value.
y = 0;
for h = 0:n
a = (-1)^(h)*((x^(2*h))/(factorial(2*h)));
y = a + y;
end
line 3 = ???Undefined function or variable h

Best Answer

Assuming x and n are defined before hand, there is nothing wrong with that command line. What makes you think something is wrong?
.
.
EDIT
How are you calling that function? Because when I call it I get no error.
>> cos_maclaurin(1.5,6)
ans =
0.0707372049851851
Also, if you really want to return the results for an array input, you should put your code in a loop (at the least - other improvements could be made):
function Y = cos_maclaurin(x,n)
% help goes here....
% n = n(1); % And other input checking goes here. Suggestions:
% isnumeric(x), isempty(x), same for n, etc.
Y = zeros(size(x));
for ii = 1:numel(x)
y = 0;
for h = 0:n
a = (-1)^(h)*((x(ii)^(2*h))/(factorial(2*h)));
y = a + y;
end
Y(ii) = y;
end
With the above code you can do:
>> cos_maclaurin([1.5 3],60)
ans =
0.0707372016677029 -0.989992496600446
Further enhancements might include the ability to enter a single n or an array n the same size as x, as well as efficiency improvements. Good luck!