We can see how well it works. Your code is below.
Now, let me test it out, in a symbolic form.
Is that a correct Taylor expansion for cos(X)?
Yes. In fact, it is.
Does it correctly extimate cos(X), for reasonably small X?
It seems to predict reasonably well.
My guess is you are hoping this will predict correctly for large values of X. And that is of course not true.
Yes, that is garbage. Perhaps you need to do some reading about convergence of infinite series. Even if this series will converge in theory, after a vast number of terms, this does not mean you will expect it to converge in practice, using computation with a finite number of decimal digits.
function Pn = taylor_cos(a,n)