0% found this document useful (0 votes)
10 views26 pages

11root Finding

Uploaded by

haoyujiang13
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views26 pages

11root Finding

Uploaded by

haoyujiang13
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 26

APPLICATIONS OF

MATLAB IN ENGINEERING
Yan-Fu Kuo Fall 2015
Dept. of Bio-industrial Mechatronics Engineering
National Taiwan University

Today:
• Symbolic approach
• Numeric root solvers
• Recursive functions
Applications of MATLAB in Engineering Y.-F. Kuo 2

Problem Statement
• Suppose you have a mathematical function 𝑓(𝑥)
and you want to find 𝑥0 such that 𝑓 𝑥0 = 0, e.g.
𝑓 𝑥 = 𝑥 2 − 2𝑥 − 8 = 0
• How do you solve the 100

problem using MATLAB?


80

• Analytical Solutions
60

f(x)
• Graphical Illustration
40

• Numerical Solutions
20

-10 -5 0 5 10
x
Applications of MATLAB in Engineering Y.-F. Kuo 3

Symbolic Root Finding Approach


• Performing mathematics on symbols, NOT numbers
• The symbols math are performed using “symbolic
variables”
• Use sym or syms to create symbolic variables

syms x x=sym('x');
x + x + x x + x + x
(x + x + x)/4 (x + x + x)/4

• Define: 𝑦 = 𝑥 2 − 2𝑥 − 8
Applications of MATLAB in Engineering Y.-F. Kuo 4

Symbolic Root Finding: solve()


• Function solve finds roots for equations
𝑦 = 𝑥 ∙ sin(𝑥) − 𝑥 = 0
syms x syms x
solve('x*sin(x)-x', x) y = x*sin(x)-x;
solve(y, x)

• Find the roots for:


cos(𝑥)2 − sin(𝑥)2 = 0 and cos(𝑥)2 + sin(𝑥)2 = 0
Applications of MATLAB in Engineering Y.-F. Kuo 5

Solving Multiple Equations


• Solve this equation using symbolic approach:
𝑥 − 2𝑦 = 5
𝑥+𝑦 =6

syms x y
eq1 = x - 2*y - 5;
eq2 = x + y - 6;
A = solve(eq1,eq2,x,y)
Applications of MATLAB in Engineering Y.-F. Kuo 6

Solving Equations Expressed in Symbols


• What if we are given a function expressed in
symbols?
𝑎𝑥 2 − 𝑏 = 0
syms x a b
solve('a*x^2-b')

• 𝑥 is always the first choice to be solved


• What if one wants to express 𝑏 in terms of 𝑎 and 𝑥?

syms x a b
solve('a*x^2-b', 'b')
Applications of MATLAB in Engineering Y.-F. Kuo 7

Exercise
• Solve this equation for 𝑥 using symbolic approach
2 2
𝑥−𝑎 + 𝑦−𝑏 = 𝑟2

• Find the matrix inverse using symbolic approach


𝑎 𝑏
𝑐 𝑑
Applications of MATLAB in Engineering Y.-F. Kuo 8

Symbolic Differentiation: diff()


• Calculate the derivative of a symbolic function:
𝑦 = 4𝑥 5
syms x
y = 4*x^5;
yprime = diff(y)

• Exercise:
𝑥 2
𝑒 𝑑𝑓
𝑓 𝑥 = , =?
−𝑥+3 𝑥3 𝑑𝑥
𝑥 2 + 𝑥𝑦 − 1 𝜕𝑓
𝑔 𝑥 = 3 , =?
𝑦 +𝑥+3 𝜕𝑥
Applications of MATLAB in Engineering Y.-F. Kuo 9

Symbolic Integration:
• Calculate the integral of a symbolic function:

𝑧= 𝑦𝑑𝑥 = 𝑥 2 𝑒 𝑥 𝑑𝑥 , 𝑧(0) = 0

syms x; y = x^2*exp(x);
z = int(y); z = z-subs(z, x, 0)

• Exercise:
10
𝑥2 − 𝑥 + 1
𝑑𝑥
0 𝑥+3
Applications of MATLAB in Engineering Y.-F. Kuo 10

Symbolic vs. Numeric

Advantages Disadvantages
Symbolic • Analytical solutions • Sometimes can't be solved
• Lets you intuit things about • Can be overly complicated
solution form
Numeric • Always get a solution • Hard to extract a deeper
• Can make solutions understanding
accurate
• Easy to code
Applications of MATLAB in Engineering Y.-F. Kuo 11

Review of Function Handles (@)


• A handle is a pointer to a function
• Can be used to pass functions to other functions
• For example, the input of the following function is
another function:
function [y] = xy_plot(input,x)
% xy_plot receives the handle of a function and plots that
% function of x
y = input(x); plot(x,y,'r--');
xlabel('x'); ylabel('function(x)');
end

• Try: xy_plot(@sin,0:0.01:2*pi);
Applications of MATLAB in Engineering Y.-F. Kuo 12

Using Function Handles

xy_plot(@sin,0:0.01:2*pi); xy_plot(@atan,0:0.01:2*pi);

1 1.5

0.8

0.6

0.4
1
0.2
function(x)

function(x)
0

-0.2
0.5
-0.4

-0.6

-0.8

-1 0
0 1 2 3 4 5 6 7 0 1 2 3 4 5 6 7
x x
Applications of MATLAB in Engineering Y.-F. Kuo 13

fsolve()
• A numeric root solver
• For example, solve this equation:
𝑓 𝑥 = 1.2𝑥 + 0.3 + 𝑥 ∙ sin(𝑥)

f2 = @(x) (1.2*x+0.3+x*sin(x));
fsolve(f2,0)

A function handle Initial guess


Applications of MATLAB in Engineering Y.-F. Kuo 14

Exercise
• Find the root for this equation :
2𝑥 − 𝑦 − 𝑒 −𝑥
𝑓 𝑥, 𝑦 =
−𝑥 + 2𝑦 − 𝑒 −𝑦
using initial value 𝑥, 𝑦 = (−5, −5)
Applications of MATLAB in Engineering Y.-F. Kuo 15

fzero() 4

3.5
• Anther numeric root solver 3

2.5
• Find the zero if and only if
2
the function crosses the 1.5
x-axis 1

0.5
f=@(x)x.^2
0
fzero(f,0.1) -2 -1 0 1 2

fsolve(f,0)

Number of iterations Tolerance


• Options:
f=@(x)x.^2
options=optimset('MaxIter',1e3,'TolFun',1e-10);
fsolve(f,0.1,options)
fzero(f,0.1,options)
Applications of MATLAB in Engineering Y.-F. Kuo 16

Finding Roots of Polynomials: roots()


• Find the roots of this polynomial:
𝑓(𝑥) = 𝑥5 − 3.5𝑥4 + 2.75𝑥3 + 2.125𝑥2 − 3.875𝑥 + 1.25

roots([1 -3.5 2.75 2.125 -3.875 1.25])

• roots only works for polynomials

• Find the roots of the polynomial:


𝑓 𝑥 = 𝑥3 − 6𝑥2 − 12𝑥 + 81
Applications of MATLAB in Engineering Y.-F. Kuo 17

How Do These Solvers Find the Roots?


• Now we are going to introduce more details of
some numeric methods
Applications of MATLAB in Engineering Y.-F. Kuo 18

Numeric Root Finding Methods


• Two major types:
• Bracketing methods (e.g., bisection method)
Start with an interval that contains the root
• Open methods (e.g., Newton-Raphson method)
Start with one or more initial guess points

• Roots are found iteratively until some criteria are


satisfied:
• Accuracy
• Number of iteration
Applications of MATLAB in Engineering Y.-F. Kuo 19

Bisection Method (Bracketing)


Assumptions:
• 𝑓(𝑥) continuous on [𝑙, 𝑢]
• 𝑓(𝑙) ∙ 𝑓(𝑢) < 0

Algorithm:
Loop
1. 𝑟 = (𝑙 + 𝑢)/2
2. If 𝑓(𝑟) ∙ 𝑓(𝑢) < 0 then new interval [𝑟, 𝑢]
If 𝑓(𝑙) ∙ 𝑓(𝑟) < 0 then new interval [𝑙, 𝑟]
End
Applications of MATLAB in Engineering Y.-F. Kuo 20

Bisection Algorithm Flowchart


Start:
Given 𝑙, 𝑢

Calculate 𝑓(𝑙) 𝑓(𝑢)

Calculate
𝑙+𝑢 no
𝑟= 2
𝑓(𝑟)

yes
(𝑢 − 𝑙) < 𝜀 Stop
yes no
𝑓(𝑟) ∙ 𝑓(𝑢) < 0

𝑙=𝑟 𝑢=𝑟
Applications of MATLAB in Engineering Y.-F. Kuo 21

Newton-Raphson Method (Open)


Assumption:
f(x)
• 𝑓(𝑥) continuous y
• 𝑓′(𝑥) known

f(x00)
f(x11)

Algorithm: x22
Loop f(x22) x11 x00 x
𝑓 𝑥𝑛
𝑥𝑛+1 = 𝑥𝑛 −
𝑓′ 𝑥𝑛
End
Applications of MATLAB in Engineering Y.-F. Kuo 22

Newton-Raphson Algorithm Flowchart

Start:
Given 𝑥0

Calculate 𝑓(𝑥0 ), 𝑛 = 0

no
Calculate
𝑓 𝑥𝑛
𝑥𝑛+1 = 𝑥𝑛 − ′ yes
𝑓 𝑥𝑛 𝑓(𝑥0 ) < 𝜀 Stop
𝑛 =𝑛+1
Applications of MATLAB in Engineering Y.-F. Kuo 23

Bisection vs. Newton-Raphson

Bisection • Reliable
• No knowledge of derivative is needed
• Slow
• One function evaluation per iteration
• Needs an interval [a,b] containing the root, f(a)·f(b)<0
Newton • Fast but may diverge
• Needs derivative and an initial guess x0, f’(x0) is
nonzero
Applications of MATLAB in Engineering Y.-F. Kuo 24

Recursive Functions
• Functions that call themselves
• Example, factorial of an integer n
𝑛! = 1 × 2 × 3 × ⋯ × 𝑛
• A factorial can be defined in terms of another
factorial:
𝑛! = 𝑛 × (𝑛 − 1)!
= 𝑛 × (𝑛 − 1) × (𝑛 − 2)!
= 𝑛 × (𝑛 − 1) × (𝑛 − 2) × (𝑛 − 3)!
=𝑛× 𝑛−1 × 𝑛−2 ×⋯
Applications of MATLAB in Engineering Y.-F. Kuo 25

Factorial Recursive Function


• The function includes a recursive case and a base
case
• The function stops when it reaches the base case

function output = fact(n)


% fact recursively finds n!
if n==1
output = 1; Base case
else
output = n * fact(n-1); Recursive case
end
end
Applications of MATLAB in Engineering Y.-F. Kuo 26

End of Class

You might also like