Write a MATLAB program that determines cos (x) using the Taylor series expansion.
Show older comments
The Taylor series expansion for cos(x) is: cos(x)=1-x^2/2!+x^4/4!-x^6/6!+... =
∞
∑ (-1)^n/(2n)n!(x^2n)
n=0
where x is in radians. Write a MATLAB program that determines cos (x) using
the Taylor series expansion. The program asks the user to type a value for an
angle in degrees. Then the program uses a loop for adding the terms of the
Taylor series. If an is the nth term in the series, then the sum Sn of the n terms
is sn = sn-1 + an . In each pass calculate the estimated error E given by
E = abs(Sn-Sn-1/Sn-1)· Stop adding terms when E<=0.000001. The program displays the value of cos(x). Use the program for calculating:
(a) cos(35°) (b) sin(125°)
Compare the values with those obtained by using a calculator.
so far i have the following but it is not working.
A = input('Enter the value for an angle in degrees = ');
n=1; an = 1; Sn = Sn-1+an;
while E <= 0.000001
an = cos(35)/factorial(n);
Sn = Sn-1 + an
n = n+1;
end
if n <= 0.000001
disp('stop')
fprintf('Sn - Sn-1/Sn(%f) = %f', cos,Sn)
end
1 Comment
James Tursa
on 8 Apr 2015
Please make an attempt and post your code, then we can comment on it and make suggestions.
Accepted Answer
More Answers (0)
Categories
Find more on Logical in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!