Please some body get me out from this
cosx=1-x^2/2+x^4/4-x^6/6! ........The series should end when the last term is less than 10^(-6).
The code I have done my self is
function[y]=tall(x)
x=0;
n=0;
err=0;
tol=10.^(-6);
y=0;
while(err<TOL)< font>
n=n+1;
y=sum((-1).^n/factorial(2*n))*x.^(2*n);
err=((-1).^n/factorial(2*n))*x.^(2*n);
end
return
end
but I am not getting result please help me on that
0
Comments
1) Your function file takes an input (x) but you overwrite whatever the input is with 0 in the line x=0; This line should be omitted.
2) You set err=0 but your while loop condition appears to be err. Since the while loop only executes when its condition evaluates to true your loop will never execute.
3) You do not need to use 'return' in this instance. It should be omitted.
Logic:
You don't need to be concerned with error, you just want to evaluate the terms of the sequence, add them to find the series result, and stop evaluating when the terms are smaller than 10^(-6). There are several ways to do this with the most obvious being a while loop.
Also, since the Taylor series approximation for cosine only uses even numbers for n it makes more sense for n to start with 0 or 2 (depending on how you decide to implement this) and increment by 2 with each iteration. This is easier to follow as well as computationally faster than multiplying n by 2 every time you want to use it in your series.
-drasnor
function[y]=tall(x)
x=0;
n=0;
err=0;
tol=10.^(-6);
y=0;
while(err<tol)
n=n+1;
y=sum((-1).^n/factorial(2*n))*x.^(2*n);
err=((-1).^n/factorial(2*n))*x.^(2*n);
end
return
end
while(err<tol)
n=n+1
.
.
.
n=n+1;
.
.
.
-drasnor