MATLAB: Taylor series for sin(x) doesn’t give the right answer above 34 radians

sinxtaylor series

I have the following code:
x=35;
N=90;
si=0;
for k = 0:N
si = si +((((-1)^(k))*((x^(2*k+1))))/(factorial((2*k+1))));
end
fprintf(['actual value from the code ' num2str(si) '\n'])
fprintf(['actual value from sin (' num2str(x) ')= ' num2str(sin(x))])
I did examples from x=.1 to x=34, they give you almost approximation to the correct one or sometimes the exact value of sin(x) (note that I increase the N as we x becomes bigger, i.e. x=10, then N could be 15, but for x=20, then N could be at least 30 to find the exact value). But, when I try for x=35 or above and I increase N to reach 70 till 90, then it doesnt change. It looks like matlab give the same result for N=70 to N=90. If you choose N to be 120 or above, then matlab won't work, it gives NaN.
My question: If I want to perform a code like this one to find for x=35 and above, then how can I modify the code to do it? Or what other ways to do it.
Thanks

Best Answer

  • The taylor series is only valid in radians
    x=35*pi/180; % convert to radians. Or x = deg2rad(35).
    N=90;
    si=0;
    for k = 0:N
    si = si +((((-1)^(k))*((x^(2*k+1))))/(factorial((2*k+1))));
    end
    fprintf('actual value from the code %f\n', si)
    fprintf('actual value from sin (%f)= %f\n', x, sin(x))