- #1

StaloyT

- 1

- 0

## Homework Statement

Write a user-defined function that determines cos(x) using Taylor Series expansion

Stop adding terms when estimated error, E<=.000001

## Homework Equations

sum S

_{n }= S

_{n-1}+ a

_{n}

E = | (S

_{n}- S

_{n-1})/S

_{n-1}|

## The Attempt at a Solution

function y = cosTaylor(x)

Sn=1;

Snm1=0;

n=2;

xr=x*pi/180;

E=1;

while E >= .000001

an=(-1)^n*xr^(2*n)/(factorial(2*n));

Snm1=Sn;

Sn=Snm1+an;

n=n+1;

E=abs((Sn-Snm1)/Snm1);

end

This gives values too large compared to what they should be.

I really don't understand why this doesn't work.

Any help is appreciated.

Thanks in advance!