💾
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: Nonlinear solver
  • Problem
  • Tutorial: MATLAB
  • Exercise
  • Tutorial: C-Programming
  • Exercise 1
  • Exercise 2
  • Exercise 3
  • Exercise 4

Was this helpful?

  1. Numerical Programming
  2. Tutorial

Tutorial: Nonlinear solver

PreviousTutorial: Passing a Function, Function callbackNextTutorial: Differentiation

Last updated 8 months ago

Was this helpful?

Tutorial: Nonlinear solver

Problem

Solve for x, in a non-linear function of

f(x)= 8-4.5*(x-sin(x))=0

image

Tutorial: MATLAB

Sample Code

clear all
F = inline('8-4.5*(x-sin(x))');
dF=inline('-4.5*(1-cos(x))');​
x=0:0.001:10;
plot(x,F(x)); grid on

Exercise

Download the tutorial source file and fill in the blanks. Run the code and validate your answer.


Tutorial: C-Programming

  1. Create a new project “ TU_Nonlinear” with Visual Studio, under the directory \NP\tutorial\

  2. Download the tutorial source file from

  1. Add the downloaded source file under the project folder.

  2. Prepare your library header files in your project: myNP_tutorial.h and myNP_tutorial.c

    • They must be located in \NP\include\ folder

  3. Rename your library header files as myNP_yourID.h and myNP_yourID.c

    • Example: myNP_20030011.c, myNP_20030011.h

    From now on, you will update your functions in the library header files.

Exercise 1

On the given source code template TU_nonlinear_student.cpp, fill-in the missing codes.

Bisection Method

Assuming (func(a) * func(b) <0 )

  1. First, Write down a pseudocode for the bisection

    // YOUR pseudocode goes here// YOUR pseudocode goes here
  2. Based on the pseudocode, fill in the blanks in the source code.

    double bisection(float _a, float _b, float _tol);
  3. Move your functions from main source file to your header files

    • function definitions: myNP_yourID.h

    • function declaration: myNP_yourID.c

Exercise 2

Modify your Bisection function with considering the following conditions

  • if(func(a) * func(b) > 0), No solution exists

  • if(func(a) * func(b) ==0), Either (1) a=Xtrue or (2) b=Xtrue

  • if(k==Nmax) Solution did not converge within given iteration

Exercise 3

Newton Raphson Method

  1. First, Write down a pseudocode for the method

    // YOUR pseudocode goes here// YOUR pseudocode goes here
  2. Based on the pseudocode, fill in the blanks in the source code.

    double newtonRaphson(float _a, float _b, float _tol);
  3. You need to define the function f(x) and the derivative function dfdx(x) as

    double func(float _x);
    double dfunc(float _x);

    For dfdx(x), get the derivative formula analytically.

  4. After you have compiled them successfully, move your functions to your header files

Exercise 4

Modify Newton Raphson Method with function callback

Modify the newton-raphson function that calls the problem functions f(x) and dfdx(x) as input arguments.

double newtonRaphson(double func(double _x), double dfunc(double _x), double _x0, double _tol);

Now, the problem function f(x) and the derivative function dfdx(x) should be located in the main source code TU_nonlinear_student.cpp

NOT in myNP_yourID.h

// Move these declaration and definitions of problem functions to
// TU_nonlinear_student.cpp
double func(float _x);
double dfunc(float _x);

MATLAB tutorial source file :

C-program tutorial source file :

This is the same header files used in the previous tutorial:

If you do not have them, you can

TU_nonlinear_student.mlx
TU_nonlinear_student.cpp
Tutorial-Sine Taylor: Part 2
download from here
Read how to pass a function as an input argument