Taylor series with sin(x).... I need help setting up my while loop and error bound

This is what I have so far....it is not giving me anything, and I know there are things wrong with it, but I am not sure what: ---------------------
a picture of my script file is here: http://i.imgur.com/RFoX9y8.png?1
---------------------- Thank you!

Answers (2)

I see three mistakes here.
1. You reference sin_approx(count-1) but you aren't storing sin_approx in an array so there is no such entity. You need to either create an array or else create a temporary "previous" value.
2. You ask for the angle in degrees but your series expansion is only good for radian measure.
3. The inequality in the while loop is in the wrong direction. You need
while abs(E)>=0.000001

5 Comments

Ok, so if, prior to my loop commands, I define sin_approx=[-2*pi:0.001:2*pi], change the E<= to E>=, and somewhere in my commands convert the inputted angle to radians (the specifications require an angle input in degrees) and then reconvert the result back to degrees, it should be fixable?
There is a sind() function that takes arguments in degrees, so sind(45) is the same value as sin(45*pi/180).
Do I need to use the sin MATLAB function in my script though? I thought the program is approximating that.. Also, after everything has looped and stopped, how can I output the result for the approximated sin(x)? I tried this way but I get a long string of numbers:
fprintf('sin(x) = %5.4f',sin_approx)
Yes, you do need to convert to radians because that is what the Taylor series assumes. It would be invalid for angles in degrees. The Taylor series for 'sind' is altogether different.

Sign in to comment.

I writen above mention like below
clc
x=input('Enter the value of x in degree: ');
xr=x*pi/180;
n=0;
s=0;
while err>=0.000001
a=(-1)^n*(xr)^(2*n+1)/factorial(2*n+1);
s=s+a;
err=abs(a/s);
n=n+1;
end
fprintf('The value of taylor series sinx %f for specific value of x %f',s,x)
fprintf('The value of MATLAB function sindx %f for specific value of x %f',sind(x))
but it shown error as per below
Undefined function or variable 'err'.
Error in Practice2 (line 7)
while err>=0.000001
Please correct it

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Asked:

on 7 Apr 2013

Answered:

on 30 Dec 2019

Community Treasure Hunt

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

Start Hunting!