Você está na página 1de 34

Programming

And
Approximation
1. Structured Programming

Logical Representation
Roots of a quadratic equation
Modeling Of Falling Parachutist

gm
dv c
g v
Exact Solution
v (t )
c

1 e ( c / m )t

dt m
Approximation
Solution

c
v(ti 1 ) v(ti ) g v(ti )ti 1 ti
m
Pseudocode
2. Modular Programming

Computer programs can be divided into small subprograms, or modules, that


can be developed and tested separately modular programming.
MATLAB
SCILAB
OCTAVE
EXCEL
Example : Pseudocode for a function that solves a
differential equation using Eulers method.
3. Approximation

For many engineering problems, we cannot obtain analytical solutions.


solutions
results results that are close to the exact
Numerical methods yield approximate results,
analytical solution. We cannot exactly compute the errors associated with numerical
methods.
Only rarely given data are exact, since they originate from measurements. Therefore
there is probably error in the input information.
Algorithm itself usually introduces errors as well, e.g., unavoidable round-offs,
round etc
The output information will then contain error from both of these sources.
How confident we are in our approximate result?
how much error is present in our calculation and is it tolerable?
The question is how
3.1 Significant Figures

Numerical methods yield approximate results. We must, therefore, develop criteria to specify
how confident we are in our approximate result.
Although quantities such as , e,, or 7 represent specific quantities, they cannot be expresse
exactly by a limited number of digits. For example, = 3.141592653589793238462643... ad
infinitum. Because computers retain only a finite number of significant figures, such numbers
can never be represented exactly. The omission of the remaining significant figures is called
round-off error.
3.1 Significant Figures

Number of significant figures indicates precision. Significant digits of a number are


those that can be used with confidence, e.g., the number of certain digits plus one
estimated digit.

Speedometer
2 Certain digits .. 48 km/h
1 Estimated digit 0.9 km/h
3 Significant digits/figures 48.9 km/h

Odometer
6 certain digits
1 estimated digits
7 Significant digits 87,324.45 km
3.1 Significant Figures

Zeros are not always significant figures because they may be necessary just to locate a
decimal point.
53,800 How many significant figures?
5.38 x 104 3
5.380 x 104 4
5.3800 x 104 5
Zeros are sometimes used to locate the decimal point not significant figures.
0.00001753 4
0.0001753 4
0.001753 4
3.2 Accuracy and Precision
Accuracy.
How close is a computed or measured value to the true value
Precision (or reproducibility)
How close is a computed or measured value to previously computed
or measured values.
Inaccuracy (or bias).
A systematic deviation from the actual value.
Imprecision (or uncertainty).
Magnitude of scatter.
(a)Inaccurate and imprecise;
(b)Accurate and imprecise;
(c)Inaccurate and precise;
(d)Accurate and precise.
4. Numerical Error

Numerical errors arise from the use of approximations to represent exact


mathematical operations and quantities.
Numerical Errors :
Truncation errors, which result when approximations are used to represent
exact mathematical procedures,
Round-off errors, which result when numbers having limited significant figures
are used to represent exact numbers.
Definitions
True Value = Approximation + Error

Et = True value Approximation (+/-)


(+/

True error
true error
True fractional relative error
true value
true error
True percent relative error, t 100%
true value
True value will be known only when we deal with functions that can be solved analytically
(simple systems). In real world applications, we usually not know the answer a priori. An
alternative is to normalize the error using the best available estimate of the true value
Then
Approximate error
a 100%
Approximation
Iterative approach, example Newtons method

Current approximation - Previous approximation


a 100%
Current approximation

The sign : (+ / -)
When performing computations, we may not be concerned with the sign of the error, but we
are interested in whether the percent absolute value is lower than a prespecified percent
tolerance
Computations are repeated until stopping criterion is satisfied.

a s Pre-specified
specified % tolerance based
on the knowledge of your
solution

If the following criterion is met

s (0.5 10(2-n) )%
we can be assured that the result is correct to at least n significant figures.
xample

a s
Computer Algorithm for Iterative
Calculations function [fx,ea,iter] = IterMeth(x,es,maxit)
% Maclaurin series of exponential function
% [fx,ea,iter] = IterMeth(x,es,maxit)
Pseudocode % input:
% x = value at which series evaluated
% es = stopping criterion (default = 0.0001)
% maxit = maximum iterations (default = 50)
% output:
% fx = estimated value
% ea = approximate relative error (%)
% iter = number of iterations
% defaults:
if nargin<2|isempty(es),es=0.0001;end
MATLAB if nargin<3|isempty(maxit),maxit=50;end
% initialization
iter = 1; sol = 1; ea = 100;
% iterative calculation
while (1)
solold = sol;
sol = sol + x ^ iter / factorial(iter);
iter = iter + 1;
if sol~=0
ea=abs((sol - solold)/sol)*100;
end
if ea<=es | iter>=maxit,break,end
end
fx = sol;
end
5. Round-off
off Errors
Roundoff errors arise because digital computers cannot represent
some quantities exactly.
Numbers such as p, e, or 7 cannot be expressed by a fixed
number of significant figures.
Computers use a base-2 2 representation, they cannot precisely
represent certain exact base-10
10 numbers.
Fractional quantities are typically represented in computer using
floating point form, e.g.,
Integer part
exponent
m.be
mantissa Base of the number system
used
Integer Representation.

(a) decimal (base 10) and the (b) binary (base 2) systems work
The representation of the decimal integer -173 on a 16-bit
bit computer using the signed magnitude method.

Upper limit can be converted to a decimal integer

Thus, a 16-bit
bit computer word can store decimal integers ranging from -32,768 to 32,767
32-bit integers would range from 2,147,483,648 to +2,147,483,647
Floating-Point
Point Representation
Integer part
exponent
m.be
mantissa Base of the number system
used

Fractional quantities are typically represented in computers using floating-point


floating format.
156.78 0.15678x103 in a floating point base-10 system

1
0.029411765 Suppose only 4
34 decimal places to be stored
0 1
0.029410 m 1
2
Normalized to remove the leading zeroes. Multiply the mantissa by
10 and lower the exponent by 1
0.2941 x 10-1

Additional significant figure


is retained Chapter 3
1
m 1
b
Therefore
for a base-10 system 0.1 m<1
for a base-2 system 0.5 m<1
Floating point representation allows both fractions and very
large numbers to be expressed on the computer. However,
Floating point numbers take up more room.
Take longer to process than integer numbers.
Round-off
off errors are introduced because mantissa holds only a finite
number of significant figures.
6. Truncation Errors

Truncation errors are those that result from using an approximation in place of an exact
mathematical procedure.
Taylor Series

where
Approximation of a Function with a Taylor Series Expansion

Você também pode gostar