Info

This question is closed. Reopen it to edit or answer.

Why can't I get an output when I am trying to find Cosine of 180 radians using Taylor series expansion.

2 views (last 30 days)
sum= double(1);
sign=1;
for n=2:2:1000
sign = -sign;
form = ((Rad).^n)/factorial(n);
sum = sum + (sign*(form));
if form <= 0.000000001
break;
end
end
CosFormula = sum
  3 Comments
Torsten
Torsten on 9 Feb 2016
"Rad" is undefined.
Furthermore, you should not build x^n/n! anew in each step because you will arrive at Infinity/Infinity quite soon for Rad>1. Note that form_new = form_old*x^2/((2*n-1)*2*n).
What are you trying to calculate - cos(180) or cos(pi) ? Note that the argument in the Taylor series expansion is in radians, not in degrees.
Best wishes
Torsten.
Guillaume
Guillaume on 9 Feb 2016
Do not use sum or sign as variable names. They both are the names of frequently used matlab functions and having a variable with the same name means you can't use the functions anymore.

Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!