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
Create a new project “ TU_TaylorSeries” with Visual Studio
Create the new source file and name it as “TU_taylorSeries_exercise.cpp”
Copy the source code
C_taylorSeries_exercise.cpp
#include<stdio.h>#include<stdlib.h>#include<math.h>#definePI3.14159265358979323846264338327950288419716939937510582doublefactorial(int _x);doublepower(double _x,int N);doublesinTaylor(double _x);doublesindTaylor(double _x);intmain(int argc,char* argv[]){double xdeg =60; double x = PI /3;double sinN =0;double sinDeg =0; /*===== Select the function to call =====*/ sinN =sinTaylor(x); // in [rad]printf("\n\n");printf("=======================================\n");printf(" sin( %f[rad] ) Calculation \n", x);printf("=======================================\n"); printf(" - My result = %3.12f \n", sinN);printf(" - Math.h result = %3.12f \n", sin(x));printf(" - absolute err. = %3.12f \n", sinN - sin(x));printf("=======================================\n"); sinDeg =sindTaylor(xdeg); // in [deg]printf("\n\n");printf("=======================================\n");printf(" sin( %f[deg] ) Calculation \n", xdeg);printf("=======================================\n"); printf(" - My result = %3.12f \n", sinDeg);printf("=======================================\n");system("pause");return0;}// factorial functiondoublefactorial(int N){int y =1;for (int k =2; k <= N; k++) y = y * k;return y;}// power functiondoublepower(double _x,int N){double y =1;for (int k =1; k <= N; k++) y = y * _x;return y;}// Taylor series approximation for sin(x) (input unit: [rad])doublesinTaylor(double _x){ int N_max =10;double sinN =0; for (int k =0; k < N_max; k++)// [TODO] add your algorithm herereturn sinN;}// Taylor series approximation for sin(x) (input unit: [deg])doublesindTaylor(double _x){double sinDeg=0;// [TODO] add your algorithm herereturn sinDeg;}
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
Define your sinTaylor(x) in the NP library header file
Procedure
Create a new project “ TU_TaylorSeries_Part2” with Visual Studio
Create the new source file and name it as “TU_taylorSeries_exercise_part2.cpp”
Copy the source code
C_taylorSeries_exercise_part2.cpp
#include<stdio.h>#include<stdlib.h>#include<math.h>//#define PI 3.14159265358979323846264338327950288419716939937510582#include"../../include/myNP_tutorial.h"intmain(int argc,char* argv[]){double xdeg =60; double x = PI /3;double sinN =0;double sinDeg =0; /*===== Select the function to call =====*/ sinN =sinTaylor(x); // in [rad]printf("\n\n");printf("=======================================\n");printf(" sin( %f[rad] ) Calculation \n", x);printf("=======================================\n"); printf(" - My result = %3.12f \n", sinN);printf(" - Math.h result = %3.12f \n", sin(x));printf(" - absolute err. = %3.12f \n", sinN - sin(x));printf("=======================================\n"); sinDeg =sindTaylor(xdeg); // in [deg]printf("\n\n");printf("=======================================\n");printf(" sin( %f[deg] ) Calculation \n", xdeg);printf("=======================================\n"); printf(" - My result = %3.12f \n", sinDeg);printf("=======================================\n");system("pause");return0;}
Update the existing library header files named as myNP_tutorial.h and myNP_tutorial.c
These files should be saved in “ \include\” folder.
Your sinTaylor(rad) of Exercise 1 should be declared and defined in the header file.
/*----------------------------------------------------------------\@ Numerical Methods by Young-Keun Kim - Handong Global UniversityAuthor : SSS LabCreated : 05-03-2021Modified : 05-03-2021Language/ver : C in MSVS2019Description : myNP_tutorial.h/----------------------------------------------------------------*/#ifndef_MY_NM_H // use either (#pragma once) or (#ifndef ...#endif)#define_MY_NM_H#definePI3.14159265358979323846264338327950288419716939937510582#include<stdio.h>#include<stdlib.h>#include<math.h>// Factorial functionexterndoublefactorial(double _x);externdoublepower(double _x,int N);// Taylor series approximation for sin(x) using pre-defined functions (input unit: [rad])externdoublesinTaylor(double _x);// Taylor series approximation for sin(x) using pre-defined functions (input unit: [deg])externdoublesindTaylor(double _x);#endif
/*----------------------------------------------------------------\@ C-Tutorial by Young-Keun Kim - Handong Global UniversityAuthor : SSS LABCreated : 05-03-2021Modified : 08-19-2022Language/ver : C++ in MSVS2022Description : myNP_tutorial.c/----------------------------------------------------------------*/#include"myNP_tutorial.h"// factorial functiondoublefactorial(int N){// [TODO] add your algorithm herereturn0;}// power functiondoublepower(double _x,int N){// [TODO] add your algorithm herereturn0;}// Taylor series approximation for sin(x) using pre-defined functions (input unit: [rad])doublesinTaylor(double _x){ int N_max =10;double sinN =0; //for (int k = 0; k < N_max; k++)// [TODO] add your algorithm herereturn sinN;}// Taylor series approximation for sin(x) using pre-defined functions (input unit: [deg])doublesindTaylor(double _x){double sinDeg =0;// [TODO] add your algorithm herereturn sinDeg;}