NM3 Root s02
NM3 Root s02
NM3 Root s02
Numerical Methods Lecture 3 Nonlinear Equations and Root Finding Methods page 68 of 82
Numerical Methods Lecture 3 Nonlinear Equations and Root Finding Methods
Lecture covers two things:
1) Solving systems of linear equations symbolically
2) Using Mathcad to solve systems of nonlinear equations
3) Investigating algorithms to find roots of equations
Solving Systems of Linear Equations Symbolically
Lets take a look at a very powerful tool in Mathcad that started a revolution in computational analysis. It
started in the late 1980s when I was an undergraduate. A company called Wolfram created a computer
program called Mathematica. This was the first computer code that could solve algebraic and calculus
equations symbolically. That is, if I had an equation that said x*y = z, Mathematica could tell me that
y = z / x, without ever needing me to assign numbers to x, y, or z. It also was able to solve integrals, dif-
ferential equations, and derivatives symbolically. This was an incredible advance, and opened the doors
to a whole new world of programming, numerical methods, pure mathematics, engineering, and science.
Since then, a competing code called Maple was developed and sold itself to other software companies to
include in their programs.
The end result: Mathcad uses Maple as a solving engine in the background (you dont see it) to solve
problems symbolically. Here we will look at a brief example of how to use this capability in the context
of solving a system of linear equations.
Example: The structural system below is something you will see in CES 3102 or CES 4141. r1 and r2 are
labels that indicate how the ends of the beam are allowed to move. Q and W represent the external loads
(a couple and a distributed load, respectively), and material properties are given as E and I. L is the length
of the beam. The goal is to solve for the amount or rotation at r1, and the deflection at r2 that occurs for
thegiven loads. This would help us to solve for internal stresses, allowing us to design the beam to sur-
vive these internal forces.
Solution: The way we learn to solve this problem in CES 4141 is using a Matrix-based solution proce-
dure. The generic form of the solution is
K * r = R
K is a 2 x 2 stiffness matrix that contains information about the structures shape, boundary conditions
and material properties. THe information needed is L, E, and I
r is a 2 x 1 vector that contains the unknown rotation and displacement quantities sought.
R is a 2 x 1 vector that contains only information about the external loads (W and Q for this problem).
r2
r1
W
Q
L
W = 3 K/FT
Q = 2 K*FT
L = 15 FT
E = 4000 ksi
I = 1400 in^4
CGN 3421 - Computer Methods Gurley
Numerical Methods Lecture 3 Nonlinear Equations and Root Finding Methods page 69 of 82
Solution continued:
So we will have K as a known 2 x 2 stiffness matrix
We will have R as a known 2 x 1 load vector
We will solve for the unknown displacement vector r
We will not go into any detail on HOW we fill in the values for K and R, thats for another class.
Here we will just consider how to solve the ssytem of matrix equations symbolically.
Define Load Vector R as a
function of Q, W, L Define Stiffness K as a function of E, I, L
K E I , L , ( )
4 E I
L
6 E I
L
2
6 E I
L
2
12 E I
L
3
|
\
|
|
|
|
|
.
:= R Q W , L , ( )
Q
W L
2
30
7
20
W L
|
\
|
|
|
|
|
.
:=
The variables in the parenthesis (E,I,L) and (Q,W,L) is telling mathcad that the
variables K and R, respectively, will be a function of those variables.
known information
Show me the inverse of the stiffness matrix
Not a necessary step, but pretty cool see the
matrix version of 1/K
Note the arrow --> is the way to
initiate a symbolic operation. Here
we are asking what is the inverse of
K ? The arrow comes from the
view => toolbars => symbolic pad
K E I , L , ( )
1
1
E I ( )
L
1
2 E I ( )
L
2
1
2 E I ( )
L
2
1
3 E I ( )
L
3
(
(
(
(
\
|
|
.
7
40 E I ( )
L
3
W
1
2 E I ( )
L
2
Q
1
30
W L
2
|
\
|
|
.
7
60 E I ( )
L
4
W +
(
(
(
(
Symbolic Solution
Symbolic Inverse
Symbolic Calculation
CGN 3421 - Computer Methods Gurley
Numerical Methods Lecture 3 Nonlinear Equations and Root Finding Methods page 70 of 82
We can now send specific values into the function for r by first assigning
numbers to the parameters. Note that units are not being declared, so its
up to the user to be consistent.
E 4000 := I 1400 := L 15 12 :=
Here are the material/geometric
properties
W
3
12
:= Q 2 12 :=
Here are the external load values
Finally, we can now ask Mathcad to evaluate the results of sending the
parameters into the function we created above. Now that E, I, L, Q, W
have numbers, they will be subsituted into the equations to get answers
Be sure to use the same order for the parameters as defined above
r E I , L , Q , W , ( )
0.053
6.179
|
\
|
|
.
=
Now let's change the external load values and get new answers
W
5
12
:= Q 4 12 :=
r E I , L , Q , W , ( )
0.092
10.553
|
\
|
|
.
=
CGN 3421 - Computer Methods Gurley
Numerical Methods Lecture 3 Nonlinear Equations and Root Finding Methods page 71 of 82
Solving Systems of Nonlinear Equations
We wont go into the algorithms themselves here. We will just focus on how to use Mathcad to solve the
problem.
Use Mathcad help and use the keywords nonlinear equations to get some information. Follow the link to
the Find function in Mathcad. Find is the workhorse that gets the solution. All you need to do is give it
the correct description of the problem. This includes the equations to solve, any possible constraints to
the solutions you are interested in, and a place to start looking for a solution (i.e., initial guess for the
solution). Here we will go straight into an example to show how its done.
f3 x ( ) 1 x .15x :=
6 4 2 0 2 4
6
4
2
0
2
4
6
f1 i ( )
f2 i ( )
f3 i ( )
i
ORIGIN 1
Using the Find function to solve systems of nonlinear equations
This example will solve for the intersecting values of the followin
system of 2 equations
x
2
y 2 ( )
2
+ 8
x .15 x
2
+ y + 1
Let's first take a look at the solution visually by plotting
We will create some functions that allow us to plot the
two equations above
f1 x ( ) 8 x
2
2 + :=
f2 x ( ) 8 x
2
2 + :=
i 5 4.99 , 4 .. :=
f3 x ( ) 1 x .15 x
2
:=
f3 x ( ) 1 x .15x :=
6 4 2 0 2 4
6
4
2
0
2
4
6
f1 i ( )
f2 i ( )
f3 i ( )
i
We can see that there are two solutions to the two equations above.
We'll try to find both solutions by using the 'find' function
CGN 3421 - Computer Methods Gurley
Numerical Methods Lecture 3 Nonlinear Equations and Root Finding Methods page 72 of 82
Use the help desk with the keyword 'find function'
Step one: start by guessing the solution to the set of two equations
we are trying to solve. Let's guess that x=4 and y= -1
x 4 := y 1 :=
Next we create what is called a 'solve block'. This defines the
equations to be solved.
1) type the word 'Given'
2) type below 'Given' all the equations to be solved
Note that we use the 'boolean' tab to
get the equal sign, not the keyboard stroke.
Given
x
2
y 2 ( )
2
+ 8
x .15 x
2
+ y + 1
Finally, we use the 'Find' function to solve by sending in the
two guesses we made above for x and y
Find x y , ( )
1.278
0.523
|
\
|
|
.
=
'Find' starts looking at the initial guess values, and iteratively updates
the values of x and y until a pair is found that solves the equations
defined in the solve block.
The results give the x and y values corresponding to one of the
solutions. find where x=1.278 and y= -0.523 in the graph above
'Find' found this solution because the initial guesses were closer
to this solution.
CGN 3421 - Computer Methods Gurley
Numerical Methods Lecture 3 Nonlinear Equations and Root Finding Methods page 73 of 82
Now let's find the other solution seen in the graph, we'll redifine
the initial guesses to somewhere near the other solution and
use 'Find' again
x 4 := y 2 :=
Given
x
2
y 2 ( )
2
+ 8 x .15 x
2
+ y + 1 Find x y , ( )
2.76
2.617
|
\
|
|
.
=
Notice now that the solution changes to the other one shown in
the earlier graph
What else can we do with this solve block?
Suppose we want only to find solutions if x > 0
that is, we want to tell Mathcad to not even bother
with solutions if x < 0
We can do this by adding constraints to the solve block
x 4 := y 2 :=
Given
x
2
y 2 ( )
2
+ 8 x .15 x
2
+ y + 1
x 0 >
Adding the constraint x > 0
Solution changes to the first one
we found earlier, even though the
initlal guess is closer to the solution
with the negative x value
Find x y , ( )
1.278
0.523
|
\
|
|
.
=
CGN 3421 - Computer Methods Gurley
Numerical Methods Lecture 3 Nonlinear Equations and Root Finding Methods page 74 of 82
Solving for Roots of Equations
What?
Locating where some function crosses the x-axis
Find x such that
Why?
We can then find where crosses any value
Find x such that
or
Find x such that
When?
Numerical methods in general are:
1) less accurate
2) slower
than using an analytical (exact) solution.
Use numerical methods when analytical answers not available
Example:
Given: , Find x such that y = 0
analytical solution: ==> no need for
Given: , Find x such that y = 0
No idea? Good, a prime candidate for a numerical solution
We will consider several methods, each does the following in some way:
1) Start with some initial guess (or range in
which to look)
2) Use some information about the function
to make another, closer guess
3) Stop when our current guess is close
enough to a solution
The user will decide what is close enough
(how big the error can be before we can stop
looking)
Method 1: INCREMENTAL
SEARCH METHOD
f x ( ) 0 =
f x ( )
f x ( ) 49.35 =
g x ( ) f x ( ) 49.35 - 0 = =
y 5 6x + =
0 5 6x + = x 5 6 - =
y x ( ) sin ( ) 3 1.5x ( ) 5 - sin - exp =
x
y
x
1
first guess
error (y distance from 0)
b
a
search in the range [a,b] for a root
x
y
x
2
next guess
new error (y distance from 0)
b
a
f x ( )
CGN 3421 - Computer Methods Gurley
Numerical Methods Lecture 3 Nonlinear Equations and Root Finding Methods page 75 of 82
1) Start with an initial range that contains the root, and subdivided it into several smaller sub-ranges.
2) Look inside each sub-range one by one for the root. When the sub-range containing the root is identi-
fied, choose either end of the range as the guess.
3) Evaluate the error in your guess. If error is too big, subdivide your new range into smaller sub-ranges.
4) Loop back to step 1) and continue the loop until the error in step 3) is small enough
Out of the 4 subranges, we pick the one in which the function crosses the x-axis as the new range to
subdivide even smaller.
Visually we can see this, but how does the algorithm know how to identify the smaller range?
Remember we have the function, so we can evaluate it at any x location we choose.
Take a moment, work out a way to identify the new smaller subrange.
a
1
b
1
a
2
b
2
start looking in range [a
1
, b
1
]
segment into 4 pieces (we picked 4 out of thin air)
look in each subrange until [a
2
, b
2
] is found
Now divide [a
2
, b
2
] into 4 segments and look again
evaluate the error err = minimum ( | f(a
2
) |, | f(b
2
) | )
subrange subrange
subrange
subrange
a
2
b
2
a
3
b
3
look in each subrange until [a
3
, b
3
] is found
evaluate the error err = minimum ( | f(a
3
) |, | f(b
3
) | )
subrange subrange
subrange
subrange
Now divide [a
3
, b
3
] into 4 segments and look again ...
CGN 3421 - Computer Methods Gurley
Numerical Methods Lecture 3 Nonlinear Equations and Root Finding Methods page 76 of 82
Incremental Search Root Finding Algorithm
Something new to notice in how we use the above function....well discuss in class the idea of passing a
function into another function. Note that the equation we are solving in the above example is not explic-
itly typed intop the IncSearch function. Is this a good thing? Why?
This is a brute force method that uses very little information about the function to improve the guess.
2
4
2.5
3
subrange subrange
subrange
subrange
3.5
8
2
-2.5
-6
-9
2
4
2.5
3
subrange subrange
subrange
subrange
3.5
8
2
-2.5
-6
-9
IncSearch numsegs tol , func , a , b , ( ) err min func a ( ) func b ( ) , ( )
dx
b a ( )
numsegs
x a i 1 ( ) dx +
prod func x ( ) func x dx + ( )
a x
b x dx +
prod 0 if
i 1 numsegs .. for
err min func a ( ) func b ( ) , ( )
err tol while
root a func a ( ) func b ( ) < if
root b otherwise
root
:=
IncSearch 4 .01 , func , 1 , 3 , ( ) 1.582 =
func x ( ) 5 2 x
2
+ :=
i 1 .9 , 3 .. :=
1 0 1 2
10
0
10
20
func i ( )
0
i
CGN 3421 - Computer Methods Gurley
Numerical Methods Lecture 3 Nonlinear Equations and Root Finding Methods page 77 of 82
Method 2: BISECTION METHOD
1) Start with an initial range [a, b], and cut the
range in half, assume this half point m is the
root
2) Evaluate the error err = | f(m) |.
3) If error is too big, decide if the root is to the
left or right of [m].
If root is to the left of m, reassign a new
range a = a, b = m
Else If root is to the right of m, reassign a
new range a = m, b = b
4) Go back to step 1), stop bisecting when the
error at m is small enough.
Three Bisection iterations
Eventually we zero in on the root
How do we decide where the root is
relative to m for the next [a , b] ??
Same as the incremental search:
prod = f(a) * f(m)
if prod < 0, root is to the left
otherwise root is to the right
a
b m
err > tol - yes?
evaluate the error err = | f(m) |
a
err = |f(m)|
err = |f(m)|
new m
make new range:
root is to left of m ===> a = a, b = m
root is to right of m ==> a = m, b = b
divide new [a , b] in half to get new m
evaluate the error err = | f(m) |
....etc.
new b
a1
b1
m1
f(m1)
iteration 1
b2
m2
f(m2)
a2
iteration 2
a3
m3
b3
f(m3)
iteration 3
b stays, a moves over from left
error at m1 too big,
so guess again
error at m2 too big,
so guess again
CGN 3421 - Computer Methods Gurley
Numerical Methods Lecture 3 Nonlinear Equations and Root Finding Methods page 78 of 82
This slow method does not use much function information. We can do better.
Method 3: FALSE POSITION
- Relative height of function at end points used to make better guesses
1) Define initial range [a b] (possibly the result of a single pass of the incremental search method).
first guess c - intersection of a line from f(a) to f(b) with x-axis
(from similar triangles)
2) Evaluate the error err = | f(c) |
If error is too big, decide if the root is to the left or right of [c].
for f(a) * f(c) < 0, reassign a new range a = a, b = c
for f(a) * f(c) > 0, reassign a new range a = c, b = b
new guess
3) Stop iterating when the error is small enough
b
a
f(a)
f(b)
it seems more likely that the root
would be closer to a than b, and not in the middle
Given the heights of f(a) vs. f(b)
Lets use this concept in an algorithm
c
af b ( ) bf a ( ) -
f b ( ) f a ( ) -
-------------------------------- =
c
af b ( ) bf a ( ) -
f b ( ) f a ( ) -
-------------------------------- =
CGN 3421 - Computer Methods Gurley
Numerical Methods Lecture 3 Nonlinear Equations and Root Finding Methods page 79 of 82
Two False Position iterations
Eventually we zero in on the root
Tends to zero in faster than using bisection
method. why?
This method uses more function information
than bisection
bisection: only knows if f(a), f(m) are positive
or negative
false position: Uses +/- of f(a) and f(c), and
uses their relative magnitudes to make next
guess at c
Method 3: Root finding - Newton
Raphson method
Incremental search - uses sign of f(a) and f(b)
Bisection - uses sign of f(a) and f(b)
False position - uses sign and relative magnitude of f(a) vs. f(b)
Newton Raphson - uses derivative of f(x) df(x)/dx
1) Pick a single point (not a range) as an initial guess x(1)
2) evaluate the error err = | f(x(1)) |
3) Next guess - draw tangent line from initial guess to the
x-axis. New guess, x(2), is the intersection of the tangent
line with the x-axis.
4) Back to step 2 to evaluate error of new guess ...etc.
Tangent line (slope) is given by derivative of f(x)
General formula for next guess x(k+1) in terms of previous guess x(k)
new guess
a1
b1 c1
f(c1)
iteration 1
b2
f(c2)
a2
c2
iteration 2
a stays, b moves over from right
error at m2 too big,
so guess again
x
1
first guess
error (y distance from 0)
tangent to f(x
1
)
second guess
x
2
x
x
k 1 +
x
k
f x
k
( )
d f x
k
( ) dx
--------------------------- - =
CGN 3421 - Computer Methods Gurley
Numerical Methods Lecture 3 Nonlinear Equations and Root Finding Methods page 80 of 82
this is from the definition of slope
x
x
k
tangent to f(x
k
)
x
k+1
solve for x
k+1
s|ope
df x
k
( )
dx
--------------- = s|ope
rise
run
---------- = s|ope
0 f x
k
( ) -
x
k 1 +
x
k
-
------------------------- =
f(x
k
)
CGN 3421 - Computer Methods Gurley
Numerical Methods Lecture 3 Nonlinear Equations and Root Finding Methods page 81 of 82
Lets continue with a few more iterations
Another example, 2 iterations in one figure
x x
1
first guess
error (y distance from 0)
tangent to f(x
1
)
second guess
x
2
f(x
2
)
x
x
1
tangent to f(x
2
)
second guess
x
2
f(x
2
)
x
3
x x
1
x
2
f(x
3
)
x
3
x
4
x1
f(x1)
slope = f(x1)
x2
f(x2)
slope = f(x2)
x3
CGN 3421 - Computer Methods Gurley
Numerical Methods Lecture 3 Nonlinear Equations and Root Finding Methods page 82 of 82
Some Comments
1) Quickly converges to the root under the right conditions
2) Can be divergent ( a very bad word ) if initial guess not close to the root
Must have condition in the indefinite loop to stop if divergent
may need incremental search to narrow the range
example: x1 is not close to the root, tangent line send us off in wrong direction
3) Requires the function AND the functions derivative
Subtle Foreshadowing
What if an equation for the derivative df(x) is not readily available ????
Sure would be nice to have an algorithm that numerically estimates the derivative of any function
...HHmmmm...
Good test question:
Under what conditions will Newton Raphson find the exact root after a single iteration??
x
x
2
x
1
were not getting closer....