Tutorial - Sine Taylor

Tutorial - Sine Taylor

Tutorial - Programming Sin(x) with Taylor Series

Download Supplementary PPT

Part 1

Q1 a) Create sinTaylor(x) that returns the output of sine x, where x is in [rad].

b) Create sindTaylor(x) that returns the output of sine x, where x in in [deg].

You must use your own function of power() and factorial() from Assignment 0

Procedure

  1. Create a new project “ TU_TaylorSeries” with Visual Studio

  2. Create the new source file and name it as “C_taylorSeries_exercise.cpp”

  3. Copy the source code from this link: C_taylorSeries_exercise.c

  4. Fill in the definition of sinTaylor(rad) in the main source.

  5. Compare your answer and calculate the absolute error

  • sin(π/3)= 0.86602540378

  1. Create sindTaylor(deg) for degree unit input and output.

  • Hint: re-use sinTaylor(rad) definition

TIP

Approximation of Sine with Taylor series

Pseudocode for Programming Sine with Taylor series

Pseudocode for Programming power()

See here for the TA Tutorial Video


Part 2

Q. Define your sinTaylor(x) in a header file

Procedure

  1. Create a new project “ TU_TaylorSeries_Part2” with Visual Studio

  2. Create the new source file and name it as “C_taylorSeries_exercise_part2.cpp”

  3. Copy the source code from this link: C_taylorSeries_exercise_part2.c

  4. Create a new header file named as myNP_tutorial.h and myNP_tutorial.c

5. Your sinTaylor(rad) of Exercise 1 should be declared and defined in the header file.

6. Run and check the answer

See here for the TA Tutorial Video


Extra Work

  1. Create double cosTaylor(double rad)

  2. Create double expTaylor(double x)


Exercises and solution

Part 1

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define		PI		3.14159265358979323846264338327950288419716939937510582


double factorial(int _x);
double sinTaylor(double _x);
double sindTaylor(double _x);

double power(double _x, int N);
double sinTaylor2(double _x);

int main(int argc, char* argv[])
{

	double x = PI / 3;
	//double x = 60;

	double S_N = 0;

	/*===== Select the function to call =====*/
	S_N = sinTaylor(x);
	//S_N = sindTaylor(x);
	
	printf("\n\n");
	printf("=======================================\n");
	printf("    sin( %f[rad] ) Calculation   \n", x);
	printf("=======================================\n");	
	printf("   -  My     result = %3.12f    \n", S_N);
	printf("   -  Math.h result = %3.12f    \n", sin(x));
	printf("   -  absolute err. = %3.12f    \n", S_N - sin(x));
	printf("=======================================\n");
	
	system("pause");
	return 0;
}


// factorial function
double factorial(int N)
{
	int y = 1;
	for (int k = 2; k <= N; k++)
		y = y * k;

	return y;
}


//  Taylor series approximation for sin(x) using pre-defined functions (input unit: [rad])
double sinTaylor(double _x)
{	
	int N_max = 10;
	double S_N = 0;			

	for (int k = 0; k < N_max; k++)
		// [TODO] add your algorithm here
	
	return S_N;
}


// Taylor series approximation for sin(x) using pre-defined functions (input unit: [deg])
double sindTaylor(double _x)
{
	// [TODO] add your algorithm here
}

// power fuction
double power(double _x, int N)
{
	// [TODO] add your algorithm here
}

//  Taylor series approximation for sin(x) using pre-defined functions (input unit: [rad])
double sinTaylor2(double _x)
{
	// [TODO] add your algorithm here
}

Part 2

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
//#define		PI		3.14159265358979323846264338327950288419716939937510582

#include "../../../include/myNP_tutorial.h"

int main(int argc, char* argv[])
{

	double x = PI / 3;
	//double x = 60;

	double S_N = 0;

	/*===== Select the function to call =====*/
	S_N = sinTaylor(x);
	//S_N = sindTaylor(x);

	printf("\n\n");
	printf("=======================================\n");
	printf("    sin( %f[rad] ) Calculation   \n", x);
	printf("=======================================\n");
	printf("   -  My     result = %3.12f    \n", S_N);
	printf("   -  Math.h result = %3.12f    \n", sin(x));
	printf("   -  absolute err. = %3.12f    \n", S_N - sin(x));
	printf("=======================================\n");

	system("pause");
	return 0;
}

Last updated