Tutorial - Sine Taylor

Tutorial - Programming sin(x) (In Class Activity)

Preparation

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].

circle-info

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

Procedure

  • Create a new empty project in Visual Studio Community

    • Name the project as: TU_TaylorSeries

  • It should be saved under \tutorial directory

    • i.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

chevron-rightC_taylorSeries_exercise.cpphashtag
  • 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

image

Pseudocode for Programming Sine with Taylor series

image

Pseudocode for Programming power()

image

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 directory

    • i.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

chevron-rightC_taylorSeries_exercise_part2.cpphashtag

(Library File Preparation)

These files should be saved in “ \include\” folder.

image

(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

  • Run and check the answer

Video for Problem 2

See here for the TA Tutorial Video


Exercise

  1. Create double cosTaylor(double rad)

  2. Create double expTaylor(double x)


After you have completed all the exercises, you can check sample solutions here

Exercise solutionsarrow-up-right


Last updated