Tutorial - Sine Taylor
Tutorial - Programming sin(x) (In Class Activity)
Preparation
1) PPT Download: Download Supplementary PPT
2) You must follow the Tutorial: Tutorial: NP Library Header Files
3) You must do: Assignment 0
Problem 1
Introduction
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].
Procedure
Create a new empty project in Visual Studio Community
Name the project as:
TU_TaylorSeries
It should be saved under
\tutorial
directoryi.e.:
C:\Users\yourID\source\repos\NP\tutorial\TU_TaylorSeries
Create a new C/C++ source file for main()
Name the source file as
TU_taylorSeries_exercise.cpp
Copy the source code from
Fill in the definition of sinTaylor(rad) in the main source.
Compare your answer and calculate the absolute error
sin(π/3)= 0.86602540378
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()

Video for Problem 1
See here for the TA Tutorial Video
Problem 2
Introduction
Define your sinTaylor(x) in the NP library header file
Procedure
Create a new empty project in Visual Studio Community
Name the project as:
TU_TaylorSeries_Part2
It should be saved under
\tutorial
directoryi.e.:
C:\Users\yourID\source\repos\NP\tutorial\TU_TaylorSeries_Part2
Create a new C/C++ source file for main()
Name the source file as
TU_taylorSeries_exercise_part2.cpp
Copy the source code
(Library File Preparation)
Under the directory of
\include
, prepare header filesFiles:
myNP_tutorial.cpp
andmyNP_tutorial.h
.C:\Users\yourID\source\repos\NP\include
This is the same header file as in Tutorial: NP Library Header Files Step 3. Create library header files
These files should be saved in “ \include\” folder.

(Library File Update)
Update the header files
Your sinTaylor(rad) of Problem 1 should be declared and defined in the header file.
See below as example
/*----------------------------------------------------------------\
@ Numerical Methods by Young-Keun Kim - Handong Global University
Author : SSS Lab
Created : 05-03-2021
Modified : 05-03-2021
Language/ver : C in MSVS2019
Description : myNP_tutorial.h
/----------------------------------------------------------------*/
#ifndef _MY_NM_H // use either (#pragma once) or (#ifndef ...#endif)
#define _MY_NM_H
#define PI 3.14159265358979323846264338327950288419716939937510582
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
// Factorial function
extern double factorial(double _x);
extern double power(double _x, int N);
// Taylor series approximation for sin(x) using pre-defined functions (input unit: [rad])
extern double sinTaylor(double _x);
// Taylor series approximation for sin(x) using pre-defined functions (input unit: [deg])
extern double sindTaylor(double _x);
#endif
Run and check the answer
Video for Problem 2
See here for the TA Tutorial Video
Exercise
Create
double cosTaylor(double rad)
Create
double expTaylor(double x)
After you have completed all the exercises, you can check sample solutions here
Last updated
Was this helpful?