Professional Documents
Culture Documents
Lab. Sheet: Finding Root of Equation Using Bisection Method
Lab. Sheet: Finding Root of Equation Using Bisection Method
Sheet 440
Finding Root of Equation Using
Bisection Method
Part 1
1
Department of Mechanical Engineering City University of Hong Kong
2
“roots” problem
gm gc
-1=x2-2x v(t ) = tanh t
c
m
Define: Define:
gc
f(x)=x2-2x+1 f (m ) =
gm
c
tanh
t - v(t)
m
Let: Let:
f(x)=0 f(m)=0
We have: We have:
gm gc
f(x)=x2-2x+1=0 f (m ) = tanh t - v(t) = 0
c
m
X=1
m=?
Department of Mechanical Engineering Bisection method: MATLAB of
City University can helpKong
Hong you
3
Problem Statement
• A parachutist jumps out of a stationary
hot air balloon. The air resistance force
on the parachutist is assuming to be
proportional to the square of the velocity
of the parachutist:
Fair resistance = cv 2
Eq 5
c is the drag coefficient,
v is the velocity of the falling parachutist
Fair resistance is the air resistance force
fu
fr
xr xu mass
xl
xr2
fl
fu
fr
xr xu mass
xl
xr2
fl
Bisection Method
The initial guesses could be obtained from a plot of the function
or from an incremental search method
• A simple algorithm for the bisection calculation:
– Step 1: choose lower xL and upper xU guesses for the root
that the function changes sign over the interval, i.e.
f(xL)f(xU)<0
– Step 2: An estimate of the root xr is determined by
x +x
xr = L U Eq 5.6 Assuming the root is in the middle of the interval
2
– Step 3: Make the following evaluation to determine in which
Check
whether
subinterval the root lies:
the a. If f(xL)f(xr)<0, the root lies in the lower subinterval. Therefore,
assumption set xU=xr and return to step 2
in step 2 is b. If f(xL)f(xr)>0, the root lies in the upper subinterval. Therefore,
true set xL=xr and return to step 2
c. If f(xL)f(xr)=0, the root equals xr; terminate the computation
Step 3c is not a good criterion for many practical applications!
x rnew − x rold
a = new
100% Eq 5.7 and a s
xr
a is the approximat e percent relative error
x rnew is the estimated root for the current iteration
x rold is the estimated root for the previous iteration
and s is the stopping criterion
This method is more suitable for practical applications
Example
function root = MyUnfinishSearch (func, xl, xu, es) %%%----start of while loop---%%%
% This is only a dummy search program xrold = xr;
% func = name of function xr = (xl + xu)/2;
x −x
% xl, xu = lower and upper guesses % if xr ~=0, calculate ea: εa = abs r rold 100
% es = stopping criterion in percentage xr
% <insert your program here>
% ‘~=‘ is the operator for Not-Equal in MATLAB
fl= feval(func,xl); %fl=function(xl), the function name fl = feval(func,xl);
%is from variable input ‘func’, fr = feval(func,xr);
% for example: ’cos’,’sin’,’fm’,etc % check fl*fr >0, <0 or =0 and calculate xu and xl for
fu = feval(func, xu); %fu=function(xu) the next iteration
<insert your program here>
if fl*fu >0
error(‘Not bracketing the root') % if ea <= es, break from while loop
return % If there is no sign change,
end %error so exit the function %%% end of while loop---%%%
xr = xl; %To initialize xr to the value of xl root = xr; % Return the final xr as the best estimate of
% this is for calculating the ea for the first iteration the root
Example (cont’)
y=x
function y = myfunc(x)
% a simple function
% you can replace the following function
% with you own
y = x;
The xl and xu must bracket the root. Otherwise, error will happen.
following simple
function to test
0.5
your program:
cos(x)
➢y= cos(x); x is angle
0
in radian
➢y=0 when
-0.5
x =1.570796327 0 0.2 0.4 0.6 0.8 1 1.2
x - angle in radian
1.4 1.6 1.8 2
Learning Outcomes
After this lecture, the student would be able to
understand the following:
– The details of the coursework
– How functions can be passed to a M-file in
MATLAB
• outvar = feval (fname, arg1, arg2, …)
User interface
Appendix
Useful Commands
.* Array multiply.
X.*Y denotes element-by-element multiplication. X and Y
must have the same dimensions unless one is a scalar.
A scalar can be multiplied into anything.
== Equal.
A == B does element by element comparisons between A and B
and returns a matrix of the same size with elements set to logical 1
where the relation is true and elements set to logical 0 where it is
not. A and B must have the same dimensions unless one is a
scalar. A scalar can be compared with any size array.
~= Not equal.
A ~= B does element by element comparisons between A and B
and returns a matrix of the same size with elements set to logical 1
where the relation is true and elements set to logical 0 where it is
not. A and B must have the same dimensions unless one is a
scalar. A scalar can be compared with any size array.
while expression
statements
end
The statements are executed while the real part of the expression
has all non-zero elements. The expression is usually the result of
expr rop expr where rop is ==, <, >, <=, >=, or ~=.
e.g.
>> fx = inline ('cos(x)*sin(x)')
fx =
Inline function:
fx(x) = cos(x)*sin(x)
feval(inline('cos(x)*sin(x)'), pi/6)
>> fx(pi/6)
ans =
cos(pi/6)*sin(pi/6)
0.4330
y = x;
feval(inline('x'),3) 3
feval(inline(‘cos(x)'),pi) cos(pi)
Department of Mechanical Engineering City University of Hong Kong
26
Sequence
Matlab example:
x=3;
y=x+5;
z=x*y;
…
Selection
Matlab example:
if x<0
y=-10;
else if x>0
y=10;
else
y=0;
end
Iteration
Matlab example:
for i=0:2:20
x=x+i;
end
Another iteration
Matlab example:
while (1)
i=i+1;
y=x^2;
end
function mainprog
m=68.1;
g=9.8; function myvelocity(m,g,c,t)
c=0.25;
t=0:2:12;
v1=velocity_profile(m,g,c,t)
Your tasks
• Complete the rootfind function
• Prepare a main function to solve the
following three equations (precision 0.1%):
– f(x)=x=0 within [-2 2] range
– f(x)=cos(x)=0 within [1.4 1.6] range
– At least two roots of f(x)=cos(x)+exp(-x2)=0