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

2 views (last 30 days)
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)

Roger Stafford
Roger Stafford on 8 Apr 2013
Edited: Roger Stafford on 8 Apr 2013
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
Lemmiwinks
Lemmiwinks on 8 Apr 2013
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)
Roger Stafford
Roger Stafford on 8 Apr 2013
Edited: Roger Stafford on 8 Apr 2013
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.


sanjeeb
sanjeeb on 30 Dec 2019
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

Community Treasure Hunt

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

Start Hunting!