#include<stdio.h>#include<stdlib.h>#include<math.h>#definePI3.14159265358979323846264338327950288419716939937510582doublefactorial(int _x);doublesinTaylor(double _x);doublesindTaylor(double _x);doublepower(double _x,int N);doublesinTaylor2(double _x);intmain(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");return0;}// factorial functiondoublefactorial(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])doublesinTaylor(double _x){ int N_max =10;double S_N =0; for (int k =0; k < N_max; k++) // [TODO] add your algorithm herereturn S_N;}// Taylor series approximation for sin(x) using pre-defined functions (input unit: [deg])doublesindTaylor(double _x){ // [TODO] add your algorithm here}// power fuctiondoublepower(double _x,int N){ // [TODO] add your algorithm here}// Taylor series approximation for sin(x) using pre-defined functions (input unit: [rad])doublesinTaylor2(double _x){ // [TODO] add your algorithm here}
/*----------------------------------------------------------------\
@ C-Tutorial by Young-Keun Kim - Handong Global University
Author : YOUR NAME
Created : 09-01-2022
Modified : 09-01-2022
Language/ver : C in MSVS2022
Description : C_taylorSeries_exercise_solution.c
----------------------------------------------------------------*/
#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 = sinTaylor2(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++)
S_N = S_N + pow(-1, k) * pow(_x, 2 * k + 1) / factorial(2 * k + 1);
return S_N;
}
// Taylor series approximation for sin(x) using pre-defined functions (input unit: [deg])
double sindTaylor(double _x)
{
return sinTaylor(_x * PI / 180);
}
// power fuction
double power(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) without using pre-defined functions (input unit: [rad])
double sinTaylor2(double _x)
{
int N_max = 10;
double S_N = 0;
for (int k = 0; k < N_max; k++)
S_N = S_N + power(-1, k) * power(_x, 2 * k + 1) / factorial(2 * k + 1);
return S_N;
}
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;
}
/*----------------------------------------------------------------\@ 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);// 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);// Taylor series approximation for sin(x) without using pre-defined functions (input unit: [rad])externdoublesinTaylor2(double _x);// Function that reduced the computation cost of sinTaylor2 (input unit: [rad])externdoublesinTaylor3(double _x);#endif
/*----------------------------------------------------------------\@ Numerical Methods by Young-Keun Kim - Handong Global UniversityAuthor : SSS LabCreated : 05-03-2021Modified : 05-03-2021Language/ver : C in MSVS2019Description : myNP_tutorial.c/----------------------------------------------------------------*/#include"myNP_tutorial.h"// factorial functiondoublefactorial(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])doublesinTaylor(double _x){int N_max =10;double S_N =0;for (int k =0; k < N_max; k++) S_N = S_N +pow(-1, k) *pow(_x,2* k +1) /factorial(2* k +1);return S_N;}// Taylor series approximation for sin(x) using pre-defined functions (input unit: [deg])doublesindTaylor(double _x){returnsinTaylor(_x * PI /180);}// power fuctiondoublepower(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) without using pre-defined functions (input unit: [rad])doublesinTaylor2(double _x){int N_max =10;double S_N =0;for (int k =0; k < N_max; k++) S_N = S_N +power(-1, k) *power(_x,2* k +1) /factorial(2* k +1);return S_N;}