MATLAB: How to use a while loop to perform an infinite series approximation

alternating serieserrorinfinite seriesloopsinxtaylor serieswhile

So I have to write a program using a while loop that uses the alternating series approximation for sin(x) using a while loop and compare it to the matlab sin(x) using an error function. so far I have:
clc
clear
x=input('Enter the value of x to compute sin(x): ');
error=1;
n=0;
while error >= 1*(10^-5);
terms = ((-1)^n)*(((x^(n+1)))/factorial(n+1));
SINx=sum(terms);
n=n+1;
error=abs((sin(x)-SINx)/sin(x))*100;
end
And my loop does nothing. Any ideas?

Best Answer

Here's a shabby way of doing it...
x=pi/2; %test: sin(pi/2)=1;
error=1;
n=1;
count=0;
while error >= 1*(10^-3);
%x -x^3/3! +x^5/5! ...
count=count+1;
terms(count)=(-1)^(count+1)*(x^n)/factorial(n);
SINx=sum(terms);
n=n+2;
error=abs((sin(x)-SINx)/sin(x))*100;
end
disp(SINx)