💾
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: Differentiation
  • Ploblem
  • Tutorial: MATLAB
  • Example code
  • Exercise (25 min)
  • Tutorial: C-Programming
  • Exercise 1 : Differentiation from Discrete Points (25 min)
  • Exercise 2 : Differentiation from a function (25 min)

Was this helpful?

  1. Numerical Programming
  2. Tutorial

Tutorial: Differentiation

Tutorial: Differentiation

Ploblem

Estimate the velocity and acceleration from datasets of the position of an object.

t = 0:0.2:4;
y = [-5.87 -4.23 -2.55 -0.89 0.67 2.09 3.31 4.31 5.06 5.55 5.78 5.77 5.52 5.08 4.46 3.72 2.88 2.00 1.10 0.23 -0.59];

Tutorial: MATLAB

Example code

% asusume evenly spaced h
h=0.2;
vel = diff(y)./h;
tv=t(1:length(vel));
acc = diff(y,2)./h^2;
ta=t(1:length(acc));

Exercise (25 min)

Download the tutorial source file and fill in the blanks.

Run the code and validate your answer


Tutorial: C-Programming

Create a new folder under \tutorial Directory and name it as differentiation

  • e.g ) C:\Users\yourID\source\repos\NP\tutorial\TU_differentiation

Create a new empty project in Visual Studio Community. Name the project as TU_Differentiation

Create a new C/C++ source file for main()

  • Name the source file as TU_Differentiation_main.cpp

Paste from the following code

TU_differentiation_student.cpp
/*------------------------------------------------------------------------------\
@ Numerical Methods by Young-Keun Kim - Handong Global University

Author          : Young-Keun Kim
Created         : 01-04-2019
Modified        : 11-27-2023
Language/ver	: C in MSVS2017
Course		: Numerical Programming

Description      : [Tutorial]Differentiation.cpp
-------------------------------------------------------------------------------*/

#include "stdio.h"
#include "stdlib.h"

#include "../../include/myNP.h"
//#include "../../include/myNP_myID.h"

double myFunc(const double x);


int main(int argc, char* argv[])
{

	/*==========================================================================*/
	/*   Part 1 -     Differentiation from discrete dataset points              */
	/*==========================================================================*/

	printf("\n**************************************************");
	printf("\n|                     PART 1.                    |");
	printf("\n**************************************************\n");

	/************      Variables declaration & initialization      ************/
	int m = 21;
	double t[21] = { 0 };
	for (int i = 0; i < m; i++) t[i] = 0.2 * i;

	double x[] = { -5.87, -4.23, -2.55, -0.89, 0.67, 2.09, 3.31, 4.31, 5.06, 5.55, 5.78, 5.77, 5.52, 5.08, 4.46, 3.72, 2.88, 2.00, 1.10, 0.23, -0.59 };
	double  dxdt[21] = { 0 };

	/************      Solve  &	Show Output	   ************/
	// Differentiation from discrete dataset points
	
	// [YOUR CODE GOES HERE]
	// gradient1D(t, x, dxdt, m);
	// printVec(dxdt, m);

	

	/*==========================================================================*/
	/*   Part 2 -     Differentiation from a function                           */
	/*==========================================================================*/

	
	printf("\n**************************************************");
	printf("\n|                     PART 2.                    |");
	printf("\n**************************************************\n");

	/************      Variables declaration & initialization      ************/
	double xin = 2.5;
	double dydx[21] = { 0 };  // m=21 points

	// User defined function F(x)
	double y = myFunc(xin);
	printf("\n y=myFun(xin) = %f \n\n", y);


	/************      Solve  &	Show Output	   ************/
	// Estimate differentiation from the user defined function 
	
	// [YOUR CODE GOES HERE]
	// gradientFunc(myFunc, t, dydx, m);
	// printVec(dydx, m);


	system("pause");
	return 0;
}


// User defined function:  example  y=x*x
// Modify to User Function
double myFunc(const double x) {
	return  x * x;
}

Exercise 1 : Differentiation from Discrete Points (25 min)

Create a C-program function for numerical differentiation from a set of discrete data. Read the instruction in the source code.

For m dataset, x[0] to x[m-1]

  • 2-Point forward difference: for the first point x[0] to [m-2]

  • 2-Point backward difference: for the last point x[m-1]

void gradient1D(double _x[], double _y[], double dydx[], int m);

The gradient1D function should be defined in your library header files, located in \include folder

  • function definitions: myNP_yourID.h

  • function declaration: myNP_yourID.cpp

// Example Code using 1D Array

void printVec(double _vec[], int _row);

int main()
{
	// Static 1-D array with fixed array size and initialized
	double a[4] = { 1, 2, 3, 4 };
	// Print 1-D array element
	printVec(a, 4);
	return 0;
}

void printVec(double _vec[], int _row)
{
	for (int i = 0; i<_row; i++)
	  printf("Vector[%d] = %f \n", i, _vec[i]);
}

Exercise 2 : Differentiation from a function (25 min)

Define a function named as myFunc() that defines the user's equation F(x).

For this tutorial, use y=x^3 , for x=0:0.2:4

double myFunc(const double _x);

Create a function named as gradientFunc() that returns the gradient of the user's equation.

  • It should receive myFunc() as the input argument.

  • It generates a discrete dataset of the user's F(x)

  • This function should return a 1D array of dydx[] .

void gradientFunc(double func(const double x), double _x[ ], double dydx[ ], int m);

This function should be defined in your library header files, located in \include folder

  • function definitions: myNP_yourID.h

  • function declaration: myNP_yourID.cpp

Validate the result

PreviousTutorial: Nonlinear solverNextTutorial: Integration

Last updated 8 months ago

Was this helpful?

MATLAB tutorial source file :

Review how to use 1D arrays in C-Programming.

TU_differentiation_student.mlx
See tutorial here