💾
EC
  • Introduction
  • EC Course
    • Syllabus
    • Preparation for EC
    • Tutorial
      • Tutorial: arduino-stm32
        • Tutorial: arduino-stm32 Installation
        • Tutorial: arduino-stm32 Part1
        • Tutorial: arduino-stm32 Part2
      • Tutorial: MDK uVision
        • Tutorial: Installing MDK uVision
        • Tutorial: Create a Project with uVision
        • Tutorial: Adding library header in uVision
        • Tutorial: Re-using Project Configuration
        • Debugging in uVision
      • Tutorial: PlatformIO in CLion
      • Tutorial: PlatformIO in VSCode
      • Tutorial: Repository Management
      • Tutorial: Managing library header files
      • Tutorial: PinName Configuration
      • Tutorial: Bitwise Macro
      • Tutorial: Custom initialization
      • Tutorial: Documentation
      • Tutorial: Creating Application API
      • Tutorial: 7-Segment Display
      • Tutorial: DC motor driver connection
      • Tutorial: USART with TeraTerm
      • Tutorial: Finite State Machine programming
      • Tutorial: Bluetooth
      • Tutorial: Zigbee with Nucleo board
    • LAB
      • LAB Report Template
      • LAB: Smart mini-fan with STM32-duino
      • LAB: Portable Fan with mbed
      • LAB: GPIO Digital InOut
      • LAB: GPIO Digital InOut 7-segment
      • LAB: EXTI & SysTick
      • LAB: Timer & PWM
      • LAB: Stepper Motor
      • LAB: Input Capture - Ultrasonic
      • LAB: USART - LED, Bluetooth
      • LAB: ADC - IR reflective sensor
      • LAB: Line Tracing RC Car
    • Sample code
      • Code Templates
    • Hardware
      • Nucleo-F411RE
      • LAB Hardware
        • Electronic Chips
        • HUINS Embedded Kit
    • Projects
      • Line Tracing Car Project
      • Design Project
        • Past Projects
      • Project Grading Criteria
    • Study Resource for MCU
      • Hexa-Decimal Table
      • Bitwise Op for Register
      • System Clock
      • Timer
      • USART
      • ADC
  • STM32 M4 Programming
    • Documentation
      • C++ API Docs
    • Firmware Library
      • PinName Configuration
      • GPIO Digital
      • RCC
      • EXTI_SysTick
      • TIMER
      • USART
    • Troubleshooting
    • mbed for STM32
      • Tutorial: mbed-Part 1
      • Tutorial: mbed - Part 2
      • Tutorial: mbed - Part 3
      • Using mbed API on uVision
    • mbed OS
  • Other Programming
    • Arduino
    • Socket Programming
      • Window Socket Programming
      • Arduino WiFi
    • Cube-MX
    • Github
    • Markdown
      • Example: API documentation
    • MATLAB
  • C Programming
    • C-Programming Lessons
      • Installing Visual Studio Community
        • Visual Studio Community 2022
      • Installing VS Code(Mac/Linux)
      • Creating Header Lib
      • Pointer
      • Array
      • 2D Array
      • Structure
      • Dynamic Alloc
      • Bitwise Operation
  • Numerical Programming
    • Syllabus
    • Preparation for NP
    • Math Review
    • Tutorial
      • TA Session Video
      • Tutorial: NP Library Header Files
      • Tutorial - Sine Taylor
      • Tutorial: Passing a Function, Function callback
      • Tutorial: Nonlinear solver
      • Tutorial: Differentiation
      • Tutorial: Integration
      • Tutorial: Matrix Structure
      • Tutorial: Eigenvalue problem
      • Tutorial: ODE-IVP
      • Tutorial: Curve Fitting
      • Tutorial: Create Github Repos of NP lib
      • Tutorial: Version Control in Github
      • Tutorial: Documentation with Markdown
      • Exercise: Version Control and Documentation
    • Example: MATLAB
    • Example: NP Library
    • Assignment
      • Assignment Factorial and Power
      • Assignment: Version Control and Documentation
    • Problem Bank
Powered by GitBook
On this page
  • Tutorial - Sine Taylor
  • Tutorial - Programming Sin(x) with Taylor Series
  • Troubleshooting
  • What are the reasons for these compilation error?

Was this helpful?

  1. Numerical Programming
  2. Tutorial

Tutorial - Sine Taylor

PreviousTutorial: NP Library Header FilesNextTutorial: Passing a Function, Function callback

Last updated 8 months ago

Was this helpful?

Tutorial - Sine Taylor

Tutorial - Programming Sin(x) with Taylor Series

Part 1

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

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>
#define		PI		3.14159265358979323846264338327950288419716939937510582


double factorial(int _x);
double power(double _x, int N);
double sinTaylor(double _x);
double sindTaylor(double _x);

int main(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");
	return 0;
}


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


// power function
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) (input unit: [rad])
double sinTaylor(double _x)
{	
	int N_max = 10;
	double sinN = 0;			

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


// Taylor series approximation for sin(x) (input unit: [deg])
double sindTaylor(double _x)
{
	double sinDeg=0;
	// [TODO] add your algorithm here
	return 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

Pseudocode for Programming power()


Part 2

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"

int main(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");
	return 0;
}

  • 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 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
/*----------------------------------------------------------------\
@ C-Tutorial by Young-Keun Kim - Handong Global University

Author           : SSS LAB
Created          : 05-03-2021
Modified         : 08-19-2022
Language/ver     : C++ in MSVS2022

Description      : myNP_tutorial.c
/----------------------------------------------------------------*/

#include "myNP_tutorial.h"



// factorial function
double factorial(int N)
{
	// [TODO] add your algorithm here
	return 0;
}

// power function
double power(double _x, int N)
{
	// [TODO] add your algorithm here
	return 0;
}



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

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


// Taylor series approximation for sin(x) using pre-defined functions (input unit: [deg])
double sindTaylor(double _x)
{
	double sinDeg = 0;
	// [TODO] add your algorithm here
	return sinDeg;
}
  • Run and check the answer


Extra Work

  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


Troubleshooting

What are the reasons for these compilation error?

Case 1

Case 2

image
image
image

image

image
image
Download Supplementary PPT
Assignment 0
Exercise solutions
See here for the TA Tutorial Video
See here for the TA Tutorial Video