💾
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
  • Part 1: 1st order ODE-IVP problem
  • Problem
  • Tutorial: MATLAB
  • Analytical Solution (Ground-truth)
  • Exercise
  • Part 2: Higher-order ODE-IVP
  • Problem
  • Tutorial: MATLAB
  • Exercise
  • Assignment
  • Q1. Create C/C++ function for 1st order ODE
  • Q2. Create C/C++ function for 2nd order ODE

Was this helpful?

  1. Numerical Programming
  2. Tutorial

Tutorial: ODE-IVP

PreviousTutorial: Eigenvalue problemNextTutorial: Curve Fitting

Last updated 6 months ago

Was this helpful?

Part 1: 1st order ODE-IVP problem

Download the tutorial source file

Problem

Solve for the response (Vout) of an RC circuit with a sinusoidal input (Vin), from t=0 to 0.1 sec.

where RC=tau=0.01; T=1/tau; f=100; Vm=1; w=2pif;

Tutorial: MATLAB

MATLAB : ode45()

Lets define the RC circuit ODE function f(t,v) as gradFunc_RC(t,v).

% Initial Condition
% time
a=0; b=0.1; 
h=0.001; 
t=a:h:b;
N = (b-a)/h;

% IVP-Initial Condition
v0 = 0;
y0= v0;

%% MATLAB's function ODE45
[tmat,vmat] = ode45(@gradFunc_RC, [a b], v0);  


% gradFunc_RC.m

% function dvdt = gradFunc_RC(t,v)
%    tau=0.01; f=100; Vm=1;
%    dvdt =-v/tau + (1/tau)*Vm*cos(2*pi*f*t);
% end

Analytical Solution (Ground-truth)

The true analytical solution can be expressed as

% Analytical Solution 
tau=0.01; f=100; Vm=1; w=2pif;
A=Vm/(sqrt(1+(wtau)^2)); 
alpha=-atan(wtau);
v_true=A*(-cos(alpha)exp(-t/tau)+cos(wt+alpha));


Exercise

Exercise 1: Euler's Explicit Method

[t, yE] = odeEU_student(@gradFunc_RC,a,b,h,y0);

figure()
plot(t,yE,'--b',t,v_true,'k')
xlabel('t'); ylabel('v(t)')

% function [x, yE] = odeEU_student(ODE,a,b,h,y0)
% Variable Initialization
N = (b-a)/h;
yE=zeros(1,N+1);
t=zeros(1,N+1);

% Initial Condition
yE(1) = y0;
t(1)=a;

% Euler Explicit ODE Method
for i = 1:N
    % Calculate: t(i+1)=_________
    % [TO-DO] your code goes here
    
    % Estimate: yE(i+1)=________
    % [TO-DO] your code goes here    
end

% end % End of Function

Exercise 2: Euler's Modified Method

Create odeEM_student.m

Modify the given template code

[t, yEM] = odeEM_student(@gradFunc_RC,a,b,h,y0);

Exercise 3: 2nd Order Runge-Kutta

Let alpha=1, C1=0.5, C2=0.5.

Modify the given template code:

[t, yRK2] = odeRK2_student(@gradFunc_RC,a,b,h,y0);

Part 2: Higher-order ODE-IVP

Download the tutorial source file

Problem

Solve for the response of the given system:

Let, m=1; k=6.9; c=7; f=5; u(t)=Fin(t)=2cos(2pift)

Tutorial: MATLAB

Lets define the mck ODE functions as gradFunc_mck(t, vecY).

gradFunc_mck.m

function [dYdt] = gradFunc_mck(t,vecY)
    % Input:
    %   vecY=[y ; z] 
    % Output:
    %   dYdt=[dydt ; dzdt] 

    % Variable Initialization
    dYdt=zeros(2,1);    
    
    % System Modeling parameters 
    m=1; k=6.9; c=10*0.7;
    FinDC=2;    f=5;
    Fin=FinDC*cos(2*pi*f*t);
    
    % Output    
    dYdt(1)=vecY(2);
    dYdt(2)=1/m*(Fin-c*vecY(2)-k*vecY(1));

end 

MATLAB : ode45()

% Initial Condition
% time
a=0;
b=1;
h=0.01;
N = (b-a)/h;


% IVP-Initial Condition
yINI = 0;
vINI = 0.2;

% ODE Solver
[tref, vecY] = ode45(@gradFunc_mck, [a:h:b], [yINI, vINI]);

Exercise

Exercise 1: Euler's Explicit Method for 2nd-order ODE

sys2EU_student.m

First, fill-in this blank and complete the algorithm

% function [t, yE, vE] = sys2EU_student(gradF,a,b,h,yINI, vINI)

    % Variable Initialization
    N = (b-a)/h;    
    t=zeros(1,N+1);    
    yE=zeros(1,N+1);
    vE=zeros(1,N+1);

    % Initial Condition
    yE(1) = yINI;
    vE(1) = vINI;
    t(1)=a;


    % Euler Explicit ODE Method
    for i = 1:N
        t(i+1) = t(i) + h;        
        dYdt=gradFunc_mck(t(i),[yE(i),vE(i)]);
        
        % Estimate: yE(i+1)=________
        % [TO-DO] your code goes here  

        % Estimate: vE(i+1)=________
        % [TO-DO] your code goes here  
    end

% end  % End of Function

Then, create the function file as sys2EU_student.m

[t, yE, vE] = sys2EU_student(@gradFunc_mck,a,b,h,yINI, vINI);

Exercise 2: 2nd order Runge-Kutta for 2nd-order ODE

Create sys2RK2_student.m

Modify the given template code

[t, yRK2, vRK2] = sys2RK2_student(@mckFunc,a,b,h,yINI,vINI);

Assignment

Q1. Create C/C++ function for 1st order ODE

Use 2nd, 3rd order Runge-Kutta method

// 2nd order Runge-Kutta method 
void odeRK2(double y[], double odeFunc(const double t, const double y), 
            const double t0, const double tf, const double h, const double y_init) 

// 3rd order Runge-Kutta method 
void odeRK3 (double y[], double odeFunc(const double t, const double y), 
             const double t0, const double tf, const double h, const double y_init) 

For RK2, use alpha=1, C1=0.5

For RK3, use classical third-order Runge-Kutta

Q2. Create C/C++ function for 2nd order ODE

Use 2nd order Runge-Kutta method

void sys2RK2(double y1[], double y2[], 
                void odeFuncSys(double dYdt[], const double t, const double Y[]), 
                const double t0, const double tf, const double h, const double y1_init, 
                const double y2_init);

TU_ODE_Part2_Student_2024.zip
TU_ODE_Part1_Student_2024.zip