11root Finding
11root Finding
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
• 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
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
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
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
• 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
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
• Try: xy_plot(@sin,0:0.01:2*pi);
Applications of MATLAB in Engineering Y.-F. Kuo 12
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)
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)
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
Calculate
𝑙+𝑢 no
𝑟= 2
𝑓(𝑟)
yes
(𝑢 − 𝑙) < 𝜀 Stop
yes no
𝑓(𝑟) ∙ 𝑓(𝑢) < 0
𝑙=𝑟 𝑢=𝑟
Applications of MATLAB in Engineering Y.-F. Kuo 21
f(x00)
f(x11)
Algorithm: x22
Loop f(x22) x11 x00 x
𝑓 𝑥𝑛
𝑥𝑛+1 = 𝑥𝑛 −
𝑓′ 𝑥𝑛
End
Applications of MATLAB in Engineering Y.-F. Kuo 22
Start:
Given 𝑥0
Calculate 𝑓(𝑥0 ), 𝑛 = 0
no
Calculate
𝑓 𝑥𝑛
𝑥𝑛+1 = 𝑥𝑛 − ′ yes
𝑓 𝑥𝑛 𝑓(𝑥0 ) < 𝜀 Stop
𝑛 =𝑛+1
Applications of MATLAB in Engineering Y.-F. Kuo 23
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
End of Class