💾
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: Integration
  • Exercise - MATLAB (30 min)
  • Exercise - C Programming
  • Part 1 : Integration of Discrete Points (50min)
  • Part 2: Integration from a Function (25min)

Was this helpful?

  1. Numerical Programming
  2. Tutorial

Tutorial: Integration

PreviousTutorial: DifferentiationNextTutorial: Matrix Structure

Last updated 8 months ago

Was this helpful?

Tutorial: Integration

Exercise - MATLAB (30 min)

Estimate the velocity from the dataset of acceleration

clear all
x=[0 5 10 15 20 25 30 35 40 47 50 54 60];
y=[0 3 8 20 33 42 40 48 60 12 8 4 3];
N=length(x);
plot(x,y,'.-');

% Matlab function
I_matlab = trapz(x,y);    

Download the tutorial source file and fill in the blanks.

Run the code and validate your answer

  • MATLAB tutorial source file :

Print the output in PDF and submit it to LMS


Exercise - C Programming

Create a new empty project in Visual Studio Community.

Name the project as TU_Integration

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

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

  • Name the source file as TU_integration_main.cpp

This is NOT for the assignment


Part 1 : Integration of Discrete Points (50min)

Create a function for numerical differentiation from a set of data. Read the instructions in the source code.

Exercise 1: Trapezoid (25 minutes)

Create Trapezoidal method for discrete data inputs. Upload the result in LMS.

I(f)=12∑i=0N−1[f(xi)+f(xi+1)](xi+1−xi)I(f) = \frac{1}{2}\sum\limits_{i = 0}^{N - 1} {\left[ {f\left( {{x_i}} \right) + f\left( {{x_{i + 1}}} \right)} \right]({x_{i + 1}} - {x_i})}I(f)=21​i=0∑N−1​[f(xi​)+f(xi+1​)](xi+1​−xi​)
double trapz (double x[ ], double y[ ], int m);
  • In the report, screen capture the output window and paste your code

  • Use 1D array type with dataset length m.

  • intervals= N, # dataset=N+1=m, The ranges are x[0] to x[m-1]

Declare and define your functions in your header files, located in \include folder

  • function definitions: myNP.h

  • function declaration: myNP.cpp

Exercise 2: Simpson13 (25 minutes)

Create Simpson13 method for discrete data inputs. Upload the result in LMS.

  • In the report, screen capture the output window and paste your code

  • Use Simpson 13 method :

  • N even numbers, same intervals, from a(=x0) to b(=xN), h=(b-a)/N.

  • The interval should be h=(b-a)/N

  • Defined in myNM.cpp source file

double simpson13(double x[ ], double y[ ], int m);

Simpson 13 method :

I=h3[f(x0)+4∑i=1,3,5N−1f(xi)+2∑k=2,4,6N−2f(xk)+f(xN)]I = \frac{h}{3}[f({x_0}) + 4\sum\limits_{i = 1,3,5}^{N - 1} {f\left( {{x_i}} \right) + 2\sum\limits_{k = 2,4,6}^{N - 2} {f\left( {{x_k}} \right)} + } f({x_N})]I=3h​[f(x0​)+4i=1,3,5∑N−1​f(xi​)+2k=2,4,6∑N−2​f(xk​)+f(xN​)]

On the Answer template document, show your code and the output window, and submit it to LMS


Part 2: Integration from a Function (25min)

Exercise 3 : (25 min)

Create Simpson13() when a function is given as the input. Upload the result in LMS

double myFunc (const double x);  // in main.cpp
double integral(double func(const double x), double a, double b, int n);  // in myNM.h
  • In the report, screen capture the output window and paste your code

  • Use Simpson 13 method :

  • N even numbers, same intervals, from a(=x0) to b(=xN), h=(b-a)/N.

  • The interval should be h=(b-a)/N

  • Defined in myNM.cpp source file

Create the following function that calls myFunc() . The true answer is PI/2

I(f)=∫−111−x2dxI(f) = \int_{ - 1}^1 {\sqrt {1 - {x^2}} dx}I(f)=∫−11​1−x2​dx

Paste from the following code:

Download the answer report file for this tutorial. :

The answer report file download :

TU_Integration_Matlab 2024.zip
TU_integration_student
Answer Report
Answer Report