Write a C function that approximates sin function.

Write your question here.
Write a C function that approximates sin
function so that you can answer the following question during the lab

a) For a value of x = 0.1, how many terms do you need in the power series so that the result from the
series equals the result from the sin(x) library function up to the 6th decimal place.

Hint: You will need power and factorial functions. For power function, you can use power function that
has a prototype defined as;

double pow(double x, double y)

pow returns the result of x to the power y as a double type. You will need to

include math.h to use this
function (#include <math.h>).

Note that you don’t need put prototype.

Prototype of that power functions is already located in math.h file.

Simply, you need to put include statement math.h in your source code .

The factorial function can be implemented as the following;
double factorial (double n)
{
if(n==0)
return 1;
int factorial = 1;
for (int i = n; i >=1; i--)
factorial *= i;
http://www.wolframalpha.com/input/?i=taylor+series+sin+x
http://blogs.ubc.ca/infiniteseriesmodule/units/unit-3-power-series/taylor-series/maclaurin-expansion-of-sinx/

Although you may implement it without using power or factorial function.
for example ,
for the ith term = (i-1)th term *-1*x*x/(2*i*(2*i+1))
Last edited on
You could modify the exp(x) in:
http://www.cplusplus.com/forum/beginner/114198/#msg626319
to sin(x).
Modify lines 14, 18, 22.
First term is x, and -(x*x)/(2n(1+2n)) is the multiplier for each successive term as explained above.
Last edited on
Topic archived. No new replies allowed.