0

Hello, im trying to create a cos function in MATLAB, but it is wrong. :/

well from Taylor serie we have

`cosx = 1 - x^2/2! + x^4/4! - x^6/6! +... + x^n/n!`

or for noobs ->

`cosx = x^1/1! - x^2/2! + x^4/4! - x^6/6! +... + x^n/n!`

this is my code in MATLAB

```
function y = cosine(x,n)
sum = x;
term = x;
for i = 1:2:n %with step 2
temp = -temp * (x^i/i);
sum = sum + temp;
end
y = sum;
```

I try this also

```
function y = cosine(x,n)
sum = x;
term = x;
for i = 1:2:n %with step 2
temp = -temp * x *x /(i*(i+1));
sum = sum + temp;
end
y = sum;
```

Any help how i can fix term?