Tutorial: Nonlinear solver

Tutorial: Nonlinear Solver- student

Problem

Solve for x, in f(x)= 8-4.5*(x-sin(x))

Matlab Functions

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 - MATLAB

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

Exercise - C Programming

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

  2. Download the tutorial source file from

3. Add it as the source file.

4. Add the header files in your project: myNP_tutorial.h and myNP_tutorial.c

Exercise 1

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);
  1. Move your functions from main source file to your header files

    • function definitions: myNP.h

    • function declaration: myNP.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) aXtrue or (2) b=Xtrue

  • if(k==Nmax) Solution did not converged within given it teration

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. Move your functions from main source file to your header files

  • function definitions: myNP.h

  • function declaration: myNP.c

Exercise 4

Modify Newton Raphson Method with function callback

Modify the newton raphson function that calls functions as inpur argment as

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

Last updated