SlideShare ist ein Scribd-Unternehmen logo
1 von 54
Applications of Numerical methods
Numerical Methods
I. Finding Roots
II. Integrating Functions
What computers can’t do
• Solve (by reasoning) general mathematical
problems  they can only repetitively
apply arithmetic primitives to input.
• Solve problems exactly.
• Represent all numbers. Only a finite subset
of the numbers between 0 and 1 can be
represented.
Finding roots / solving equations
• General solution exists for equations such as
ax2 + bx + c = 0
The quadratic formula provides a quick
answer to all quadratic equations.
However, no exact general solution (formula)
exists for equations with exponents greater
than 4.
Finding roots…
• Even if “exact” procedures existed, we are
stuck with the problem that a computer can
only represent a finite number of values…
thus, we cannot “validate” our answer
because it will not come out exactly
• However we can say how accurate our
solution is as compared to the “exact”
solution
Finding roots, continued
• Transcendental equations: involving
geometric functions (sin, cos), log, exp. These
equations cannot be reduced to solution of a
polynomial.
• Convergence: we might imagine a
“reasonable” procedure for finding solutions,
but can we guarantee it terminates?
Problem-dependent decisions
• Approximation: since we cannot have exactness,
we specify our tolerance to error
• Convergence: we also specify how long we are
willing to wait for a solution
• Method: we choose a method easy to implement
and yet powerful enough and general
• Put a human in the loop: since no general
procedure can find roots of complex equations, we
let a human specify a neighbourhood of a solution
Practical approach hand calculations
• Choose method and initial guess wisely
• Good idea to start with a crude graph. If
you are looking for a single root you only
need one positive and one negative value
• If even a crude graph is difficult, generate a
table of values and plot graph from that.
Practical approach - example
• Example
e-x = sin(πx/2)
• Solve for x.
• Graph functions - the crossover points are
solutions
• This is equivalent to finding the roots of the
difference function:
f(x)= e-x - sin(πx/2)
Graph
Solution, continued
• One crossover point at about 0.5, another at
about 2.0 (there are very many of them …)
• Compute values of both functions at 0.5
• Decrement/increment slightly – watch the
sign of the difference-function!!
• If there is an improvement continue, until
you get closer.
• Stop when you are “close enough”
Tabulating the function
Step

x

e-x

sin(1/2πx)

f(x)= e-x sin(1/2πx)

0

0.3

0.741

0.454

0.297

1

0.4

0.670

0.588

0.082

2

0.5

0.606

0.707

- 0.101

3

0.45

0.638

0.649

- 0.012

4

0.425

0.654

0.619

0.0347

5

0.4375

0.6456

0.6344

0.01126

6

0.44365

0.6417

0.6418

- 0.00014
Bisection Method
• The Bisection Method slightly modifies
“educated guess” approach of hand
calculation method.
• Suppose we know a function has a root
between a and b. (…and the function is
continuous, … and there is only one root)
Bisection Method
• Keep in mind general approach in
Computer Science: for complex problems
we try to find a uniform simple systematic
calculation
• How can we express the hand calculation of
the preceding in this way?
• Hint: use an approach similar to the binary
search…
Bisection method…
• Check if solution lies between a and b…
F(a)*F(b) < 0 ?
• Try the midpoint m: compute F(m)
• If |F(m)| < tol select m as your approximate solution
• Otherwise, if F(m) is of opposite sign to F(a) that is if
F(a)*F(m) < 0, then b = m.
• Else a = m.
Bisection method…
• This method converges to any pre-specified
tolerance when a single root exists on a
continuous function
• Example Exercise: write a function that
finds the square root of any positive number
that does not require programmer to specify
estimates
Square root program
• If the input c < 1, the root lies between c
and 1.
• Else, the root lies between 1 and c.
• The (positive) square root function is
continuous and has a single solution.
c = x2

6
4

Example:
F(x) = x2 - 4

2
0
-2
-4
-6

0

0.5

1

1.5

2

2.5

3

F(x) = x2 - c
double Sqrt(double c, double tol)
{
double a,b, mid, f;
// set initial boundaries of interval
if (c < 1) { a = c; b = 1}
6
else { a = 1; b = c}
4
do {
2
mid = ( a + b ) / 2.0;
0
0
f = mid * mid - c;
-2
-4
if ( f < 0 )
-6
a = mid;
else
b = mid;
} while( fabs( f ) > tol );
return mid;
}

0.5

1

1.5

2

2.5

3
Program 13-1 (text; bisection)
•
•
•
•
•

Echos inputs
Computes values at endpoints
Checks whether a root exists
If root exists, employs bisection method
Convergence criterion: Root is found if size of
current interval < ε (predefined tolerance)
Note difference with the algorithm on the previous
slide!!!
• If root found within number of iterations, prints
results, else prints failure…
Problem with Bisection
• Although it is guaranteed to converge under its
assumptions,
• Although we can predict in advance the number of
iterations required for desired accuracy
(b - a)/2n <ε −> n > log( (b - a ) / ε )
• Too slow! Computer Graphics uses square roots to
compute distances, can’t spend 15-30 iterations on
every one!
• We want more like 1 or 2, equivalent to ordinary
math operation.
Improvement to Bisection
• Regula Falsi, or Method of False Position.
• Use the shape of the curve as a cue
• Use a straight line between y values to
select interior point
• As curve segments become small, this
closely approximates the root
curve

approximation
adjacent1
adjacent2

similar
triangles

fa*b-fb*a
fa*b – fb*a

The values needed for x are values already computed…
(Different triangles used from text, but same idea)
Method of false position
• Pro: as the interval becomes small, the
interior point generally becomes much
closer to root
• Con 1: if fa and fb become too close –
overflow errors can occur
• Con 2: can’t predict number of iterations to
reach a give precision
• Con 3: can be less precise than bisection –
no strict precision guarantee
fa

a

b

fb
Problem with Regula Falsi -- if the graph is convex down, the
interpolated point will repeatedly appear in the larger
segment….
Therefore a problem arises if we use the size of the
current interval as a criterion that the root is found

fb

a

fa

fx
x

b
If we use the criterion abs(fx) < ε, this is not
a problem. But this criterion can’t be always
used (e.g. if function is very steep close to the
root…)
Another problem with Regular Falsi:
if the function grows too fast
 very slow convergence
Modified Regula Falsi
• Because Regula Falsi can be fatally slow
some of the time (which is too often)
• Want to clip interval from both ends
• Trick: drop the line from fa or fb to some
fraction of its height, artificially change
slope to cut off more of other side
• The root will flip between left and right
interval
Modified Regula Falsi
• If the root is in the left segment [a, interior]
– Draw line between (a, fa*0.5) and (interior, finterior)

• Else (in the right segment [interior, b])
-- Draw line between (interior, finterior) and (b, fb*0.5)

fb
a

interior2
interior3

fa

interior1

b
Secant Method
Exactly like Regula Falsi, except:
• No need to check for sign.
• Begin with a, b, as usual
• Compute interior point as usual
• NEW: set a to b, and b to interior point
• Loop until desired tolerance.
Secant method
• No animation ready yet: Intuition
• It automatically flips back and forth,
solving problem with unmodified regula
falsi
• Sometimes, both fa and fb are positive, but
it quickly tracks secant lines to root
Secant Illustration
F(x) = x2 - 10
120
100
80
60
40
20
0
-20

1

2

3

1 (a=1, fa=-9) (b=10, fb=90)
 int = 1.8, fint = -6.7
2 (a=10, fa=90) (b=1.8, fb= -6.7)

int = 0.88, fint = -9.22
3 (a=1.8, fa=-6.7) (b=0.88, fb=-9.22)

int = 4.25, fint = 8
4 (a=0.88, fa=-9.22) (b=4.25, fb=8)

Int =2.68, fint = -2.8
Etc…
Series1

4

1 2 3 4 5 6 7 8 9 10 11 12
Root finding algorithms
The algorithms have the following declarations:
double bisection(double c, int iterations, double tol);
double regula(double c, int iterations, double tol);
double regulaMod(double c, int iterations, double tol);
double secant(double c, int iterations, double tol);

i.e., they have the same kinds of inputs
Called function
All functions call this function: func(x, c) = x2 - c
double func(double x, double c)
{
return x * x - c;
}
* Note that the root of this function is the square root of c.
The functions call it as follows:
func( current_x, square_of_x );
Initializations
All functions implementing the root-finding algorithm have the
same initialization:
double a, b;
if ( c < 1 )
{ a = c ; b= 1;}
else
{ a = 1; b = c;};
double fa = func( a, c);
double fb = func( b, c);

The next slides illustrate the differences between algorithms.
Code
• Earlier code gave “square root” example
with logic of bisection method.
• Following programs, to fit on a slide, have
no debug output, and have the same
initializations (as on the next slide)
double bisection(double c, int iterations, double tol)
for ( int i = 0; i < iterations; i++)
{
double x = ( a + b ) / 2;
double fx = func( x, c );
if ( fabs( fx ) < tol )
return x;
if ( fa * fx < 0 ) {
b = x;
fb = fx;
}
BISECTION METHOD
else {
This is the heart of the
a = x;
algorithm. Compute the
fa = fx;
}
midpoint and the value of
}
the function there; iterate
return -1;

half with root

…

on
double regula (double c, int iterations, double tol)…
for ( int i = 0; i < iterations; i++)
{
double x = ( fa*b - fb*a ) / ( fa - fb );
double fx = func( x, c );
if ( fabs( fx ) < tol )
return x;
if ( fa * fx < 0 ) {
b = x;
fb = fx; }
REGULA FALSI
else {
The main change from
a = x;
bisection is computing
fa = fx; }
an interior point that
}
more closely
return -1;

approximates the
root….
double regulaMod (double c, int iterations, double tol)…
for ( int i = 0; i < iterations; i++)
{
double x = ( fa*b - fb*a ) / ( fa - fb );
double fx = func( x, c );
if ( fabs( fx ) < tol )
return x;
if ( fa * fx < 0 ) {
b = x;
fb = fx;
fa *= RELAX }
MODIFIED REGULA FALSI
else {
a = x;
The only change from
fa = fx;
ordinary regula is the
fb *= RELAX }
RELAXation factor
}
return -1;
double secant(double c, int iterations, double tol)…
for ( int i = 0; i < iterations; i++)
{
double x = ( fa*b - fb*a ) / ( fa - fb );
double fx = func( x, c );
if ( fabs( fx ) < tol )
return x;
a = b;
SECANT METHOD
fa = fb;
The change from ordinary
b = x;
regula is that the sign check is
fb = fx;
}
dropped and points are just “shifted
return -1;
over”
Actual performance
• Actual code includes other output
commands
• Used 4 methods to compute roots of 4, 100,
1000000, 0.25
• Maximum allowable iterations 1000
• Tolerance = 1e-15
• RELAXation factor = 0.8
Actual performance (tol = 1e-15)
Method
Inputs
Bisection 4
1000000

Regula

4
1000000

Mod

4
1000000

Secant

4
1000000

100
0.25
100
0.25
100
0.25
100
0.25

Answers
2
10
1000 0.5
2
-1
-1
0.5
2
10
1000 0.5
2
10
1000 0.5

Iterations
51
54
61
47
32
1000
1000
30
21
28
46
19
7
10
20
6
Important differences from text
• Assumed all methods would be used to find
square root of k between [1, c] or [c,1] by
finding root of x2 – c.
• All used closeness of fx to 0 as convergence
criteria. Text uses different criteria for
different algorithms
double bisection(double c, int iterations, double tol) …
for ( int i = 0; i < iterations; i++)
{
double x = ( a + b ) / 2;
double fx = func( x, c );
if ( fabs( fx ) < tol )
return x;
if ( fa * fx < 0 ) {
b = x;
Example….
fb = fx;
}
else {
All four code examples
a = x;
used the closeness of fx
fa = fx;
}
zero as convergence
}
return -1;
criterion.

to
Convergence criterion…
• Closeness of fx to zero not always a good
criterion, consider a very flat function may
be close to zero a considerable distance
before the root
Other convergence criteria
• Width of the interval [a,b]. If this interval
contains the root, guaranteed that the root is
within this much accuracy
• However, interval does not necessarily
contain the root (secant method)
• Text uses the width of the interval as the
convergence criteria in the Bisection
Method
Convergence criteria…
• Fractional size of search interval:
(cur_a – cur_b) / ( a – b)
• Used in modified falsi in text
• Indicates that further search may not be
productive. Does not guarantee small value
of fx.
Numerical Integration
• In NA, take visual view of integration as
area under the curve
• Many integrals that occur in science or
engineering practice do not have a closed
form solution – must be solved using
numerical integration
Trapezoidal Rule
• The area under the curve from
[a, fa] to [b, fb] is initially approximated by
a trapezoid:
I1 = ( b – a ) * ( fa + fb ) / 2
Simple
trapezoid over
large interval is
prone to error.
Divide interval
into halves…
fc

I2 = ( b – a )/2 * ( fa + 2*fc + fb ) / 2
(Note that interior sides count twice, since they belong to 2 traps.)
Further development…
I2 = ( b – a ) / 2 * ( fa + 2*fc + fb ) / 2
= ( b – a ) /2 * ( f a + fb + 2* fc ) / 2
= ( b – a ) / 2* ( fa + fb ) /2 + (b – a) / 2* fc
= I1 /2 + (b – a)/2 * fc
Notice that (b-a)/2 is the new interval width and fc is the value
of the function at all new interior points. If we call dk the new
interval width at step k, and cut intervals by half:
Ik = Ik-1/2 + dk Σ (all new interior f-values)
Trapezoidal algorithm
• Compute first trapezoid: I = (fa + fb ) * (b – a) /2
• New I = Old I / 2, length of interval by 2
• Compute sum of function value at all new
interior points, times new interval length
• Add this to New I.
• Continue until no significant difference
Simpson’s rule
•
•
•
•

.. Another approach
Rather than use straight line of best fit,
Use parabola of best fit (curves)
Converges more quickly

Weitere ähnliche Inhalte

Was ist angesagt?

Was ist angesagt? (20)

Applications of numerical methods
Applications of numerical methodsApplications of numerical methods
Applications of numerical methods
 
Engineering Numerical Analysis Lecture-1
Engineering Numerical Analysis Lecture-1Engineering Numerical Analysis Lecture-1
Engineering Numerical Analysis Lecture-1
 
Bisection method
Bisection methodBisection method
Bisection method
 
Numerical methods and its applications
Numerical methods and its applicationsNumerical methods and its applications
Numerical methods and its applications
 
Newton-Raphson Method
Newton-Raphson MethodNewton-Raphson Method
Newton-Raphson Method
 
Numerical integration
Numerical integrationNumerical integration
Numerical integration
 
Unit4
Unit4Unit4
Unit4
 
Application of differentiation
Application   of   differentiationApplication   of   differentiation
Application of differentiation
 
Gauss jordan method.pptx
Gauss jordan method.pptxGauss jordan method.pptx
Gauss jordan method.pptx
 
Secant method
Secant methodSecant method
Secant method
 
Presentation on application of numerical method in our life
Presentation on application of numerical method in our lifePresentation on application of numerical method in our life
Presentation on application of numerical method in our life
 
Secant Method
Secant MethodSecant Method
Secant Method
 
Secant method
Secant method Secant method
Secant method
 
Curve fitting - Lecture Notes
Curve fitting - Lecture NotesCurve fitting - Lecture Notes
Curve fitting - Lecture Notes
 
application of numerical method
application of numerical methodapplication of numerical method
application of numerical method
 
Error analysis
Error analysisError analysis
Error analysis
 
Why We Need To Study Calculus
Why We Need To Study CalculusWhy We Need To Study Calculus
Why We Need To Study Calculus
 
Numerical integration
Numerical integrationNumerical integration
Numerical integration
 
Numerical methods
Numerical methodsNumerical methods
Numerical methods
 
Secant method
Secant methodSecant method
Secant method
 

Ähnlich wie Applications of numerical methods

Bisection & Regual falsi methods
Bisection & Regual falsi methodsBisection & Regual falsi methods
Bisection & Regual falsi methodsDivya Bhatia
 
Differential Equations Homework Help
Differential Equations Homework HelpDifferential Equations Homework Help
Differential Equations Homework HelpMath Homework Solver
 
Single Variable Calculus Assignment Help
Single Variable Calculus Assignment HelpSingle Variable Calculus Assignment Help
Single Variable Calculus Assignment HelpMath Homework Solver
 
Differential Equations Assignment Help
Differential Equations Assignment HelpDifferential Equations Assignment Help
Differential Equations Assignment HelpMaths Assignment Help
 
Introduction to Functions
Introduction to FunctionsIntroduction to Functions
Introduction to FunctionsMelanie Loslo
 
Single Variable Calculus Assignment Help
Single Variable Calculus Assignment HelpSingle Variable Calculus Assignment Help
Single Variable Calculus Assignment HelpMaths Assignment Help
 
Single Variable Calculus Assignment Help
Single Variable Calculus Assignment HelpSingle Variable Calculus Assignment Help
Single Variable Calculus Assignment HelpMaths Assignment Help
 
Numerical Method Analysis: Algebraic and Transcendental Equations (Non-Linear)
Numerical Method Analysis: Algebraic and Transcendental Equations (Non-Linear)Numerical Method Analysis: Algebraic and Transcendental Equations (Non-Linear)
Numerical Method Analysis: Algebraic and Transcendental Equations (Non-Linear)Minhas Kamal
 
Bisection method in maths 4
Bisection method in maths 4Bisection method in maths 4
Bisection method in maths 4Vaidik Trivedi
 
A New Double Numerical Integration Formula Based On The First Order Derivative
A New Double Numerical Integration Formula Based On The First Order DerivativeA New Double Numerical Integration Formula Based On The First Order Derivative
A New Double Numerical Integration Formula Based On The First Order DerivativeIRJESJOURNAL
 
Introduction to functions
Introduction to functionsIntroduction to functions
Introduction to functionsElkin Guillen
 
Calculus Sections 4.1 and 4.3
Calculus Sections 4.1 and 4.3Calculus Sections 4.1 and 4.3
Calculus Sections 4.1 and 4.3calculusgroup3
 
P R E L I M R E V I S I O N U N I T 1
P R E L I M   R E V I S I O N    U N I T 1P R E L I M   R E V I S I O N    U N I T 1
P R E L I M R E V I S I O N U N I T 1mrmcdowall
 
Introduction to comp.physics ch 3.pdf
Introduction to comp.physics ch 3.pdfIntroduction to comp.physics ch 3.pdf
Introduction to comp.physics ch 3.pdfJifarRaya
 

Ähnlich wie Applications of numerical methods (20)

Bisection & Regual falsi methods
Bisection & Regual falsi methodsBisection & Regual falsi methods
Bisection & Regual falsi methods
 
Differential Equations Homework Help
Differential Equations Homework HelpDifferential Equations Homework Help
Differential Equations Homework Help
 
Single Variable Calculus Assignment Help
Single Variable Calculus Assignment HelpSingle Variable Calculus Assignment Help
Single Variable Calculus Assignment Help
 
Differential Equations Assignment Help
Differential Equations Assignment HelpDifferential Equations Assignment Help
Differential Equations Assignment Help
 
Calculus Homework Help
Calculus Homework HelpCalculus Homework Help
Calculus Homework Help
 
Introduction to Functions
Introduction to FunctionsIntroduction to Functions
Introduction to Functions
 
Calculus Assignment Help
Calculus Assignment HelpCalculus Assignment Help
Calculus Assignment Help
 
Single Variable Calculus Assignment Help
Single Variable Calculus Assignment HelpSingle Variable Calculus Assignment Help
Single Variable Calculus Assignment Help
 
Single Variable Calculus Assignment Help
Single Variable Calculus Assignment HelpSingle Variable Calculus Assignment Help
Single Variable Calculus Assignment Help
 
Numerical Method Analysis: Algebraic and Transcendental Equations (Non-Linear)
Numerical Method Analysis: Algebraic and Transcendental Equations (Non-Linear)Numerical Method Analysis: Algebraic and Transcendental Equations (Non-Linear)
Numerical Method Analysis: Algebraic and Transcendental Equations (Non-Linear)
 
Bisection method in maths 4
Bisection method in maths 4Bisection method in maths 4
Bisection method in maths 4
 
A New Double Numerical Integration Formula Based On The First Order Derivative
A New Double Numerical Integration Formula Based On The First Order DerivativeA New Double Numerical Integration Formula Based On The First Order Derivative
A New Double Numerical Integration Formula Based On The First Order Derivative
 
AppsDiff3c.pdf
AppsDiff3c.pdfAppsDiff3c.pdf
AppsDiff3c.pdf
 
Note introductions of functions
Note introductions of functionsNote introductions of functions
Note introductions of functions
 
Introduction to functions
Introduction to functionsIntroduction to functions
Introduction to functions
 
Calculus Sections 4.1 and 4.3
Calculus Sections 4.1 and 4.3Calculus Sections 4.1 and 4.3
Calculus Sections 4.1 and 4.3
 
P R E L I M R E V I S I O N U N I T 1
P R E L I M   R E V I S I O N    U N I T 1P R E L I M   R E V I S I O N    U N I T 1
P R E L I M R E V I S I O N U N I T 1
 
Introduction to comp.physics ch 3.pdf
Introduction to comp.physics ch 3.pdfIntroduction to comp.physics ch 3.pdf
Introduction to comp.physics ch 3.pdf
 
Ca 1.6
Ca 1.6Ca 1.6
Ca 1.6
 
Cg 04-math
Cg 04-mathCg 04-math
Cg 04-math
 

Mehr von Tarun Gehlot

Materials 11-01228
Materials 11-01228Materials 11-01228
Materials 11-01228Tarun Gehlot
 
Continuity and end_behavior
Continuity and  end_behaviorContinuity and  end_behavior
Continuity and end_behaviorTarun Gehlot
 
Continuity of functions by graph (exercises with detailed solutions)
Continuity of functions by graph   (exercises with detailed solutions)Continuity of functions by graph   (exercises with detailed solutions)
Continuity of functions by graph (exercises with detailed solutions)Tarun Gehlot
 
Factoring by the trial and-error method
Factoring by the trial and-error methodFactoring by the trial and-error method
Factoring by the trial and-error methodTarun Gehlot
 
Introduction to finite element analysis
Introduction to finite element analysisIntroduction to finite element analysis
Introduction to finite element analysisTarun Gehlot
 
Finite elements : basis functions
Finite elements : basis functionsFinite elements : basis functions
Finite elements : basis functionsTarun Gehlot
 
Finite elements for 2‐d problems
Finite elements  for 2‐d problemsFinite elements  for 2‐d problems
Finite elements for 2‐d problemsTarun Gehlot
 
Error analysis statistics
Error analysis   statisticsError analysis   statistics
Error analysis statisticsTarun Gehlot
 
Introduction to matlab
Introduction to matlabIntroduction to matlab
Introduction to matlabTarun Gehlot
 
Linear approximations and_differentials
Linear approximations and_differentialsLinear approximations and_differentials
Linear approximations and_differentialsTarun Gehlot
 
Local linear approximation
Local linear approximationLocal linear approximation
Local linear approximationTarun Gehlot
 
Interpolation functions
Interpolation functionsInterpolation functions
Interpolation functionsTarun Gehlot
 
Propeties of-triangles
Propeties of-trianglesPropeties of-triangles
Propeties of-trianglesTarun Gehlot
 
Gaussian quadratures
Gaussian quadraturesGaussian quadratures
Gaussian quadraturesTarun Gehlot
 
Basics of set theory
Basics of set theoryBasics of set theory
Basics of set theoryTarun Gehlot
 
Numerical integration
Numerical integrationNumerical integration
Numerical integrationTarun Gehlot
 
Applications of set theory
Applications of  set theoryApplications of  set theory
Applications of set theoryTarun Gehlot
 
Miscellneous functions
Miscellneous  functionsMiscellneous  functions
Miscellneous functionsTarun Gehlot
 

Mehr von Tarun Gehlot (20)

Materials 11-01228
Materials 11-01228Materials 11-01228
Materials 11-01228
 
Binary relations
Binary relationsBinary relations
Binary relations
 
Continuity and end_behavior
Continuity and  end_behaviorContinuity and  end_behavior
Continuity and end_behavior
 
Continuity of functions by graph (exercises with detailed solutions)
Continuity of functions by graph   (exercises with detailed solutions)Continuity of functions by graph   (exercises with detailed solutions)
Continuity of functions by graph (exercises with detailed solutions)
 
Factoring by the trial and-error method
Factoring by the trial and-error methodFactoring by the trial and-error method
Factoring by the trial and-error method
 
Introduction to finite element analysis
Introduction to finite element analysisIntroduction to finite element analysis
Introduction to finite element analysis
 
Finite elements : basis functions
Finite elements : basis functionsFinite elements : basis functions
Finite elements : basis functions
 
Finite elements for 2‐d problems
Finite elements  for 2‐d problemsFinite elements  for 2‐d problems
Finite elements for 2‐d problems
 
Error analysis statistics
Error analysis   statisticsError analysis   statistics
Error analysis statistics
 
Matlab commands
Matlab commandsMatlab commands
Matlab commands
 
Introduction to matlab
Introduction to matlabIntroduction to matlab
Introduction to matlab
 
Linear approximations and_differentials
Linear approximations and_differentialsLinear approximations and_differentials
Linear approximations and_differentials
 
Local linear approximation
Local linear approximationLocal linear approximation
Local linear approximation
 
Interpolation functions
Interpolation functionsInterpolation functions
Interpolation functions
 
Propeties of-triangles
Propeties of-trianglesPropeties of-triangles
Propeties of-triangles
 
Gaussian quadratures
Gaussian quadraturesGaussian quadratures
Gaussian quadratures
 
Basics of set theory
Basics of set theoryBasics of set theory
Basics of set theory
 
Numerical integration
Numerical integrationNumerical integration
Numerical integration
 
Applications of set theory
Applications of  set theoryApplications of  set theory
Applications of set theory
 
Miscellneous functions
Miscellneous  functionsMiscellneous  functions
Miscellneous functions
 

Kürzlich hochgeladen

The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxheathfieldcps1
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphThiyagu K
 
psychiatric nursing HISTORY COLLECTION .docx
psychiatric  nursing HISTORY  COLLECTION  .docxpsychiatric  nursing HISTORY  COLLECTION  .docx
psychiatric nursing HISTORY COLLECTION .docxPoojaSen20
 
Making and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdfMaking and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdfChris Hunter
 
Unit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxUnit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxVishalSingh1417
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxVishalSingh1417
 
Class 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfClass 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfAyushMahapatra5
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...christianmathematics
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfAdmir Softic
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxVishalSingh1417
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhikauryashika82
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeThiyagu K
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104misteraugie
 
Energy Resources. ( B. Pharmacy, 1st Year, Sem-II) Natural Resources
Energy Resources. ( B. Pharmacy, 1st Year, Sem-II) Natural ResourcesEnergy Resources. ( B. Pharmacy, 1st Year, Sem-II) Natural Resources
Energy Resources. ( B. Pharmacy, 1st Year, Sem-II) Natural ResourcesShubhangi Sonawane
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17Celine George
 
PROCESS RECORDING FORMAT.docx
PROCESS      RECORDING        FORMAT.docxPROCESS      RECORDING        FORMAT.docx
PROCESS RECORDING FORMAT.docxPoojaSen20
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibitjbellavia9
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfPoh-Sun Goh
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdfQucHHunhnh
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfagholdier
 

Kürzlich hochgeladen (20)

The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot Graph
 
psychiatric nursing HISTORY COLLECTION .docx
psychiatric  nursing HISTORY  COLLECTION  .docxpsychiatric  nursing HISTORY  COLLECTION  .docx
psychiatric nursing HISTORY COLLECTION .docx
 
Making and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdfMaking and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdf
 
Unit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxUnit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptx
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptx
 
Class 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfClass 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdf
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptx
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and Mode
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104
 
Energy Resources. ( B. Pharmacy, 1st Year, Sem-II) Natural Resources
Energy Resources. ( B. Pharmacy, 1st Year, Sem-II) Natural ResourcesEnergy Resources. ( B. Pharmacy, 1st Year, Sem-II) Natural Resources
Energy Resources. ( B. Pharmacy, 1st Year, Sem-II) Natural Resources
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17
 
PROCESS RECORDING FORMAT.docx
PROCESS      RECORDING        FORMAT.docxPROCESS      RECORDING        FORMAT.docx
PROCESS RECORDING FORMAT.docx
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibit
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdf
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 

Applications of numerical methods

  • 2. Numerical Methods I. Finding Roots II. Integrating Functions
  • 3. What computers can’t do • Solve (by reasoning) general mathematical problems  they can only repetitively apply arithmetic primitives to input. • Solve problems exactly. • Represent all numbers. Only a finite subset of the numbers between 0 and 1 can be represented.
  • 4. Finding roots / solving equations • General solution exists for equations such as ax2 + bx + c = 0 The quadratic formula provides a quick answer to all quadratic equations. However, no exact general solution (formula) exists for equations with exponents greater than 4.
  • 5. Finding roots… • Even if “exact” procedures existed, we are stuck with the problem that a computer can only represent a finite number of values… thus, we cannot “validate” our answer because it will not come out exactly • However we can say how accurate our solution is as compared to the “exact” solution
  • 6. Finding roots, continued • Transcendental equations: involving geometric functions (sin, cos), log, exp. These equations cannot be reduced to solution of a polynomial. • Convergence: we might imagine a “reasonable” procedure for finding solutions, but can we guarantee it terminates?
  • 7. Problem-dependent decisions • Approximation: since we cannot have exactness, we specify our tolerance to error • Convergence: we also specify how long we are willing to wait for a solution • Method: we choose a method easy to implement and yet powerful enough and general • Put a human in the loop: since no general procedure can find roots of complex equations, we let a human specify a neighbourhood of a solution
  • 8. Practical approach hand calculations • Choose method and initial guess wisely • Good idea to start with a crude graph. If you are looking for a single root you only need one positive and one negative value • If even a crude graph is difficult, generate a table of values and plot graph from that.
  • 9. Practical approach - example • Example e-x = sin(πx/2) • Solve for x. • Graph functions - the crossover points are solutions • This is equivalent to finding the roots of the difference function: f(x)= e-x - sin(πx/2)
  • 10. Graph
  • 11. Solution, continued • One crossover point at about 0.5, another at about 2.0 (there are very many of them …) • Compute values of both functions at 0.5 • Decrement/increment slightly – watch the sign of the difference-function!! • If there is an improvement continue, until you get closer. • Stop when you are “close enough”
  • 12. Tabulating the function Step x e-x sin(1/2πx) f(x)= e-x sin(1/2πx) 0 0.3 0.741 0.454 0.297 1 0.4 0.670 0.588 0.082 2 0.5 0.606 0.707 - 0.101 3 0.45 0.638 0.649 - 0.012 4 0.425 0.654 0.619 0.0347 5 0.4375 0.6456 0.6344 0.01126 6 0.44365 0.6417 0.6418 - 0.00014
  • 13. Bisection Method • The Bisection Method slightly modifies “educated guess” approach of hand calculation method. • Suppose we know a function has a root between a and b. (…and the function is continuous, … and there is only one root)
  • 14. Bisection Method • Keep in mind general approach in Computer Science: for complex problems we try to find a uniform simple systematic calculation • How can we express the hand calculation of the preceding in this way? • Hint: use an approach similar to the binary search…
  • 15. Bisection method… • Check if solution lies between a and b… F(a)*F(b) < 0 ? • Try the midpoint m: compute F(m) • If |F(m)| < tol select m as your approximate solution • Otherwise, if F(m) is of opposite sign to F(a) that is if F(a)*F(m) < 0, then b = m. • Else a = m.
  • 16. Bisection method… • This method converges to any pre-specified tolerance when a single root exists on a continuous function • Example Exercise: write a function that finds the square root of any positive number that does not require programmer to specify estimates
  • 17. Square root program • If the input c < 1, the root lies between c and 1. • Else, the root lies between 1 and c. • The (positive) square root function is continuous and has a single solution. c = x2 6 4 Example: F(x) = x2 - 4 2 0 -2 -4 -6 0 0.5 1 1.5 2 2.5 3 F(x) = x2 - c
  • 18. double Sqrt(double c, double tol) { double a,b, mid, f; // set initial boundaries of interval if (c < 1) { a = c; b = 1} 6 else { a = 1; b = c} 4 do { 2 mid = ( a + b ) / 2.0; 0 0 f = mid * mid - c; -2 -4 if ( f < 0 ) -6 a = mid; else b = mid; } while( fabs( f ) > tol ); return mid; } 0.5 1 1.5 2 2.5 3
  • 19. Program 13-1 (text; bisection) • • • • • Echos inputs Computes values at endpoints Checks whether a root exists If root exists, employs bisection method Convergence criterion: Root is found if size of current interval < ε (predefined tolerance) Note difference with the algorithm on the previous slide!!! • If root found within number of iterations, prints results, else prints failure…
  • 20. Problem with Bisection • Although it is guaranteed to converge under its assumptions, • Although we can predict in advance the number of iterations required for desired accuracy (b - a)/2n <ε −> n > log( (b - a ) / ε ) • Too slow! Computer Graphics uses square roots to compute distances, can’t spend 15-30 iterations on every one! • We want more like 1 or 2, equivalent to ordinary math operation.
  • 21. Improvement to Bisection • Regula Falsi, or Method of False Position. • Use the shape of the curve as a cue • Use a straight line between y values to select interior point • As curve segments become small, this closely approximates the root
  • 23. adjacent1 adjacent2 similar triangles fa*b-fb*a fa*b – fb*a The values needed for x are values already computed… (Different triangles used from text, but same idea)
  • 24. Method of false position • Pro: as the interval becomes small, the interior point generally becomes much closer to root • Con 1: if fa and fb become too close – overflow errors can occur • Con 2: can’t predict number of iterations to reach a give precision • Con 3: can be less precise than bisection – no strict precision guarantee
  • 25. fa a b fb Problem with Regula Falsi -- if the graph is convex down, the interpolated point will repeatedly appear in the larger segment….
  • 26. Therefore a problem arises if we use the size of the current interval as a criterion that the root is found fb a fa fx x b If we use the criterion abs(fx) < ε, this is not a problem. But this criterion can’t be always used (e.g. if function is very steep close to the root…)
  • 27. Another problem with Regular Falsi: if the function grows too fast  very slow convergence
  • 28. Modified Regula Falsi • Because Regula Falsi can be fatally slow some of the time (which is too often) • Want to clip interval from both ends • Trick: drop the line from fa or fb to some fraction of its height, artificially change slope to cut off more of other side • The root will flip between left and right interval
  • 29. Modified Regula Falsi • If the root is in the left segment [a, interior] – Draw line between (a, fa*0.5) and (interior, finterior) • Else (in the right segment [interior, b]) -- Draw line between (interior, finterior) and (b, fb*0.5) fb a interior2 interior3 fa interior1 b
  • 30. Secant Method Exactly like Regula Falsi, except: • No need to check for sign. • Begin with a, b, as usual • Compute interior point as usual • NEW: set a to b, and b to interior point • Loop until desired tolerance.
  • 31. Secant method • No animation ready yet: Intuition • It automatically flips back and forth, solving problem with unmodified regula falsi • Sometimes, both fa and fb are positive, but it quickly tracks secant lines to root
  • 32. Secant Illustration F(x) = x2 - 10 120 100 80 60 40 20 0 -20 1 2 3 1 (a=1, fa=-9) (b=10, fb=90)  int = 1.8, fint = -6.7 2 (a=10, fa=90) (b=1.8, fb= -6.7)  int = 0.88, fint = -9.22 3 (a=1.8, fa=-6.7) (b=0.88, fb=-9.22)  int = 4.25, fint = 8 4 (a=0.88, fa=-9.22) (b=4.25, fb=8)  Int =2.68, fint = -2.8 Etc… Series1 4 1 2 3 4 5 6 7 8 9 10 11 12
  • 33. Root finding algorithms The algorithms have the following declarations: double bisection(double c, int iterations, double tol); double regula(double c, int iterations, double tol); double regulaMod(double c, int iterations, double tol); double secant(double c, int iterations, double tol); i.e., they have the same kinds of inputs
  • 34. Called function All functions call this function: func(x, c) = x2 - c double func(double x, double c) { return x * x - c; } * Note that the root of this function is the square root of c. The functions call it as follows: func( current_x, square_of_x );
  • 35. Initializations All functions implementing the root-finding algorithm have the same initialization: double a, b; if ( c < 1 ) { a = c ; b= 1;} else { a = 1; b = c;}; double fa = func( a, c); double fb = func( b, c); The next slides illustrate the differences between algorithms.
  • 36. Code • Earlier code gave “square root” example with logic of bisection method. • Following programs, to fit on a slide, have no debug output, and have the same initializations (as on the next slide)
  • 37. double bisection(double c, int iterations, double tol) for ( int i = 0; i < iterations; i++) { double x = ( a + b ) / 2; double fx = func( x, c ); if ( fabs( fx ) < tol ) return x; if ( fa * fx < 0 ) { b = x; fb = fx; } BISECTION METHOD else { This is the heart of the a = x; algorithm. Compute the fa = fx; } midpoint and the value of } the function there; iterate return -1; half with root … on
  • 38. double regula (double c, int iterations, double tol)… for ( int i = 0; i < iterations; i++) { double x = ( fa*b - fb*a ) / ( fa - fb ); double fx = func( x, c ); if ( fabs( fx ) < tol ) return x; if ( fa * fx < 0 ) { b = x; fb = fx; } REGULA FALSI else { The main change from a = x; bisection is computing fa = fx; } an interior point that } more closely return -1; approximates the root….
  • 39. double regulaMod (double c, int iterations, double tol)… for ( int i = 0; i < iterations; i++) { double x = ( fa*b - fb*a ) / ( fa - fb ); double fx = func( x, c ); if ( fabs( fx ) < tol ) return x; if ( fa * fx < 0 ) { b = x; fb = fx; fa *= RELAX } MODIFIED REGULA FALSI else { a = x; The only change from fa = fx; ordinary regula is the fb *= RELAX } RELAXation factor } return -1;
  • 40. double secant(double c, int iterations, double tol)… for ( int i = 0; i < iterations; i++) { double x = ( fa*b - fb*a ) / ( fa - fb ); double fx = func( x, c ); if ( fabs( fx ) < tol ) return x; a = b; SECANT METHOD fa = fb; The change from ordinary b = x; regula is that the sign check is fb = fx; } dropped and points are just “shifted return -1; over”
  • 41. Actual performance • Actual code includes other output commands • Used 4 methods to compute roots of 4, 100, 1000000, 0.25 • Maximum allowable iterations 1000 • Tolerance = 1e-15 • RELAXation factor = 0.8
  • 42. Actual performance (tol = 1e-15) Method Inputs Bisection 4 1000000 Regula 4 1000000 Mod 4 1000000 Secant 4 1000000 100 0.25 100 0.25 100 0.25 100 0.25 Answers 2 10 1000 0.5 2 -1 -1 0.5 2 10 1000 0.5 2 10 1000 0.5 Iterations 51 54 61 47 32 1000 1000 30 21 28 46 19 7 10 20 6
  • 43. Important differences from text • Assumed all methods would be used to find square root of k between [1, c] or [c,1] by finding root of x2 – c. • All used closeness of fx to 0 as convergence criteria. Text uses different criteria for different algorithms
  • 44. double bisection(double c, int iterations, double tol) … for ( int i = 0; i < iterations; i++) { double x = ( a + b ) / 2; double fx = func( x, c ); if ( fabs( fx ) < tol ) return x; if ( fa * fx < 0 ) { b = x; Example…. fb = fx; } else { All four code examples a = x; used the closeness of fx fa = fx; } zero as convergence } return -1; criterion. to
  • 45. Convergence criterion… • Closeness of fx to zero not always a good criterion, consider a very flat function may be close to zero a considerable distance before the root
  • 46. Other convergence criteria • Width of the interval [a,b]. If this interval contains the root, guaranteed that the root is within this much accuracy • However, interval does not necessarily contain the root (secant method) • Text uses the width of the interval as the convergence criteria in the Bisection Method
  • 47. Convergence criteria… • Fractional size of search interval: (cur_a – cur_b) / ( a – b) • Used in modified falsi in text • Indicates that further search may not be productive. Does not guarantee small value of fx.
  • 48. Numerical Integration • In NA, take visual view of integration as area under the curve • Many integrals that occur in science or engineering practice do not have a closed form solution – must be solved using numerical integration
  • 49. Trapezoidal Rule • The area under the curve from [a, fa] to [b, fb] is initially approximated by a trapezoid: I1 = ( b – a ) * ( fa + fb ) / 2
  • 50. Simple trapezoid over large interval is prone to error. Divide interval into halves…
  • 51. fc I2 = ( b – a )/2 * ( fa + 2*fc + fb ) / 2 (Note that interior sides count twice, since they belong to 2 traps.)
  • 52. Further development… I2 = ( b – a ) / 2 * ( fa + 2*fc + fb ) / 2 = ( b – a ) /2 * ( f a + fb + 2* fc ) / 2 = ( b – a ) / 2* ( fa + fb ) /2 + (b – a) / 2* fc = I1 /2 + (b – a)/2 * fc Notice that (b-a)/2 is the new interval width and fc is the value of the function at all new interior points. If we call dk the new interval width at step k, and cut intervals by half: Ik = Ik-1/2 + dk Σ (all new interior f-values)
  • 53. Trapezoidal algorithm • Compute first trapezoid: I = (fa + fb ) * (b – a) /2 • New I = Old I / 2, length of interval by 2 • Compute sum of function value at all new interior points, times new interval length • Add this to New I. • Continue until no significant difference
  • 54. Simpson’s rule • • • • .. Another approach Rather than use straight line of best fit, Use parabola of best fit (curves) Converges more quickly