Friday 6 January 2012

Q&A: help with programming sin x series and its error approximation in c?

I have to program sin x using c program. The user will input the value of x in degree and also value of r for error approximation using a given formula for error approximation. I am not allowed to use functions, only basic if, if else statements and loops such as for, while, do while, etc. I am also not allowed to use math library. I am not sure how to go about doing this. Any help would be appreciated. Thanks
Yes I am using taylor series of sin x. I am not sure how to do factorial without using math library.

Answer by another_nick_2006
use Taylor series

http://en.wikipedia.org/wiki/Sine


Series definition
The sine function (blue) is closely approximated by its Taylor polynomial of degree 7 (pink) for a full cycle centered on the origin.Using only geometry and properties of limits, it can be shown that the derivative of sine is cosine and the derivative of cosine is the negative of sine.

Using the reflection from the calculated geometric derivation of the sinus is with the 4n + k-th derivative at the point 0:

This gives the following Taylor series expansion at x = 0. One can then use the theory of Taylor series to show that the following identities hold for all real numbers x:[1]

If x were expressed in degrees then the series would contain messy factors involving powers of π/180: if x is the number of degrees, the number of radians is y = πx /180, so

Mathematically important relationships between the sine and cosine functions and the exponential function (see, for example, Euler’s formula) are, again, elegant when the functions’ arguments are in radians and messy otherwise. In most branches of mathematics beyond practical geometry, angles are generally measured in radians.

A similar series is Gregory’s series for arctan, which is obtained by omitting the factorials in the denominator.

source

http://www.dreamincode.net/code/snippet5207.htm

http://www.google.de/#hl=de&source=hp&q=Taylor+series+sin+program+source&aq=f&aqi=&aql=&oq=&fp=9d8b700e84f864a3

Answer by Poonam
——————————————
#include
#include
using namespace std;

double factorial(double n)
{
double fact=1;
for(int i=1;i<=n;i++)
fact *= i;
return fact;
}

double power(double x,double n)
{
double pow=1;
for(int i=0;i pow*=x;
return pow;
}

double taylorSin(double angle,double pre)
{
double answer=0;
for(int i=0;i answer+=(-1)*power(angle,2*i+1)/factorial(2*i+1);
return answer;
}

void main()
{
double x,pre=5;
cout<<"nEnter x: ";
cin>>x;
cout<<"nsin("< getch();
}

error on page facebookDetermine the degree of the Maclaurin polynomial required for the error in the approximation of the function sin(0.4) to be less than 0.001.

Answer by kb
Note that sin x = Σ(n=0 to ∞) (-1)^n x^(2n+1)/(2n+1)!
==> sin(0.4) = Σ(n=0 to ∞) (-1)^n (0.4)^(2n+1)/(2n+1)!.

Since this is an alternating series, the error after n terms is bounded above by the (n+1)-th term.
==> We need |Error| = (0.4)^(2n+1)/(2n+1)! < 0.0001
==> (2.5)^(2n+1) (2n+1)! > 1000, by inverting both sides.

By trial and error, this is true for n ≥ 2.
(So, use a quadratic approximation.)

I hope this helps!

Understanding the properties of the remainder or error function for an Nth degree Taylor approximation of a function

Video Rating: 4 / 5

No comments:

Post a Comment