💾
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
  • Numerical Programming API
  • Non-Linear Solver
  • newtonRaphson()
  • Linear Solver
  • gaussElim()
  • inv()
  • Numerical Differentiation
  • gradient1D()
  • Integration
  • integral()
  • ODE-IVP
  • odeEU()
  • Class or Header name
  • Function Name

Was this helpful?

  1. Other Programming
  2. Markdown

Example: API documentation

Numerical Programming API

#include "myNM.h"

Non-Linear Solver

newtonRaphson()

Solves the non-linear problem using Newton-Raphson method

double newtonRaphson(double x0, double tol);

Parameters

  • x0: initial value.

  • tol: tolerance error

Example code

double tol = 0.00001;
double x0 = 3;
double NR_result;
​
NR_result = newtonRaphson(x0, tol);

Linear Solver

gaussElim()

solves for vector x from Ax=b, a linear system problem Using Gauss Elimination

void gaussElim(Matrix _A, Matrix _B, Matrix* _U, Matrix* _B_out);

Parameters

  • A: Matrix A in structure Matrix form. Should be (nxn) square.

  • B: vector b in structure Matrix form. Should be (nx1)

  • U: Matrix U in structure Matrix form. Should be (nxn) square.

  • B_out: vector B_out in structure Matrix form. Should be (nx1)

Example code

Matrix matA = txt2Mat(path, "prob1_matA");
Matrix vecb = txt2Mat(path, "prob1_vecb");
Matrix matU = zeros(matA.rows, matA.cols);
Matrix vecd = zeros(vecb.rows, vecb.cols);
​
gaussElim(matA, vecb, matU, vecd);

inv()

Find the inverse Matrix.

void inv(Matrix _A, Matrix _Ainv);

Parameters

  • A: Matrix A in structure Matrix form. Should be (nxn) square.

  • Ainv: Matrix Ainv in structure Matrix form. Should be (nxn) square.

Example code

Matrix matA = txt2Mat(path, "prob1_matA");
Matrix matAinv = zeros(matA.rows, matA.cols);
​
inv(matA, matAinv);

Numerical Differentiation

gradient1D()

Solve for numerical gradient (dy/dt) from a 1D-array form.

void gradient1D(double x[], double y[], double dydx[], int m);

Parameters

  • x[]: input data vector x in 1D-array .

  • y[]: input data vector y in 1D-array.

  • dydx[]: output vector dydx in 1D-array.

  • m: length x and y.

Example code

double x[21];
    for (int i = 0; i < 21; i++) {
        x[i] = 0.2 * i;
    }
double 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 };
double dydx[21];
​
gradient1D(x, y, dydx, 21);

Integration

integral()

Integral using Simpson 1/3 Method.

double integral(double func(const double _x), double a, double b, int n);

Parameters

  • func: Function func is defined.

  • a is starting point of x.

  • b is ending point of x.

  • n is the length between a and b

Example code

double I_simpson13 = integral(myFunc, -1, 1, 12);

double myFunc(const double _x) {
	return sqrt(1 - (_x * _x));
}

ODE-IVP

odeEU()

Solve the 1st-order ODE using Euler's Explicit Method.

void odeEU(double func(const double x, const double y), double y[], double t0, double tf, double h, double y0);

Parameters

  • func: Function func is defined.

  • y[]: Solution of ODE in structure 1D-array form.

  • t0 is starting point.

  • tf is ending point.

  • h is length of step.

  • y0 is initial value of y[].

Example code

double a = 0;
double b = 0.1;
double h = 0.001;
double y_EU[200] = { 0 };
double v0 = 0;

odeEU(odeFunc_rc, y_EU, a, b, h, v0);

double odeFunc_rc(const double t, const double v) {
	double tau = 1;
	double T = 1 / tau;
	double f = 10;
	double Vm = 1;
	double omega = 2 * PI * f;
	return  -T * v + T * Vm * cos(omega * t);
}

-------------------------------------------------------------------------------------------------------

Class or Header name

Function Name

Parameters

  • p1

  • p2

Example code

PreviousMarkdownNextMATLAB

Last updated 2 years ago

Was this helpful?

See full example code:

TutorialDifferentiation.cpp